make_burst_kernels.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Generate burst laminographic backprojection OpenCL kernels."""
  2. import argparse
  3. IDX_TO_VEC_ELEM = dict(zip(range(10), range(10)))
  4. IDX_TO_VEC_ELEM[10] = 'a'
  5. IDX_TO_VEC_ELEM[11] = 'b'
  6. IDX_TO_VEC_ELEM[12] = 'c'
  7. IDX_TO_VEC_ELEM[13] = 'd'
  8. IDX_TO_VEC_ELEM[14] = 'e'
  9. IDX_TO_VEC_ELEM[15] = 'f'
  10. def fill_compute_template(tmpl, num_items, index):
  11. """Fill the template doing the pixel computation and texture fetch."""
  12. operation = '+' if index else ''
  13. access = '.s{}'.format(IDX_TO_VEC_ELEM[index]) if num_items > 1 else ''
  14. return tmpl.format(index, access, operation)
  15. def fill_kernel_template(input_tmpl, compute_tmpl, kernel_tmpl, num_items):
  16. """Construct the whole kernel."""
  17. vector_length = num_items if num_items > 1 else ''
  18. computes = '\n'.join([fill_compute_template(compute_tmpl, num_items, i)
  19. for i in range(num_items)])
  20. inputs = '\n'.join([input_tmpl.format(i) for i in range(num_items)])
  21. return kernel_tmpl.format(num_items, inputs, vector_length, computes)
  22. def parse_args():
  23. """Parse command line arguments."""
  24. parser = argparse.ArgumentParser()
  25. parser.add_argument('filename', type=str, help='File name with the kernel template')
  26. parser.add_argument('burst', type=int,
  27. help='Number of projections processed by one kernel invocation')
  28. return parser.parse_args()
  29. def main():
  30. """execute program."""
  31. args = parse_args()
  32. allowed_bursts = [2 ** i for i in range(5)]
  33. if args.burst not in allowed_bursts:
  34. raise ValueError('Specified burst mode `{}` must be one of `{}`'.format(args.burst,
  35. allowed_bursts))
  36. in_tmpl = "read_only image2d_t projection_{},"
  37. comp_tmpl, ker_tmpl = open(args.filename, 'r').read().split('\n%nl\n')
  38. print fill_kernel_template(in_tmpl, comp_tmpl, ker_tmpl, args.burst)
  39. if __name__ == '__main__':
  40. main()