|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# script to run various implementations of the convolution filter |
| 4 | +# to compare their performance. |
| 5 | +# The script has the following input parameters: |
| 6 | +# --input_x: width of the input matrix |
| 7 | +# --input_y: height of the input matrix |
| 8 | +# --kernel_x: width of the kernel matrix |
| 9 | +# --kernel_y: height of the kernel matrix |
| 10 | +# --iterations: number of iterations to run |
| 11 | +# --implementation: which implementation to run |
| 12 | + |
| 13 | +import argparse |
| 14 | +import numpy as np |
| 15 | +import timeit |
| 16 | +import sys |
| 17 | + |
| 18 | +def main(): |
| 19 | + parser = argparse.ArgumentParser(description='Run convolution filter') |
| 20 | + parser.add_argument('--input_x', type=int, default=100, |
| 21 | + help='width of the input matrix') |
| 22 | + parser.add_argument('--input_y', type=int, default=100, |
| 23 | + help='height of the input matrix') |
| 24 | + parser.add_argument('--kernel_x', type=int, default=9, |
| 25 | + help='width of the kernel matrix') |
| 26 | + parser.add_argument('--kernel_y', type=int, default=9, |
| 27 | + help='height of the kernel matrix') |
| 28 | + parser.add_argument('--iterations', type=int, default=1, |
| 29 | + help='number of iterations to run') |
| 30 | + parser.add_argument('--implementation', type=str, |
| 31 | + choices=['python', 'cython', 'cython_indexed', 'cython_no_checks'], |
| 32 | + default='python', |
| 33 | + help='which implementation to run') |
| 34 | + parser.add_argument('--check', action='store_true', |
| 35 | + help='check the results') |
| 36 | + args = parser.parse_args() |
| 37 | + |
| 38 | + # Create input and kernel matrices |
| 39 | + input = np.random.rand(args.input_x, args.input_y) |
| 40 | + kernel = np.random.rand(args.kernel_x, args.kernel_y) |
| 41 | + |
| 42 | + # Run the requested implementation |
| 43 | + if args.implementation == 'python': |
| 44 | + from convolution import convolve |
| 45 | + elif args.implementation == 'cython': |
| 46 | + from convolution_cython import convolve |
| 47 | + elif args.implementation == 'cython_indexed': |
| 48 | + from convolution_cython_indexed import convolve |
| 49 | + elif args.implementation == 'cython_no_checks': |
| 50 | + from convolution_cython_no_checks import convolve |
| 51 | + |
| 52 | + # Measure the execution time |
| 53 | + time = timeit.timeit(lambda: convolve(input, kernel), number=args.iterations) |
| 54 | + print(f'Time: {time/args.iterations} s per iteration, {args.iterations} iterations') |
| 55 | + |
| 56 | + # Check the results |
| 57 | + if args.check: |
| 58 | + from convolution import convolve as convolve_python |
| 59 | + result_python = convolve_python(input, kernel) |
| 60 | + result = convolve(input, kernel) |
| 61 | + if np.allclose(result_python, result): |
| 62 | + print('Results are correct') |
| 63 | + else: |
| 64 | + print('Results are incorrect') |
| 65 | + sys.exit(1) |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + main() |
0 commit comments