3 # To add new kernel please add a .cl file to kernels directory
4 # the database name will be the part of the file name up to first '.' character
5 # the trailing characters are a tag to allow multiple primitive implementations
7 from __future__ import print_function
13 class OpenCL2CHeaders(object):
15 def __init__(self, kernels_folder, out_path, out_file_name):
16 self.kernels_folder = os.path.abspath(kernels_folder)
17 self.out_path = os.path.abspath(out_path)
18 self.out_file_name = out_file_name
19 self.include_files = {}
22 res = '// This file is autogenerated by primitive_db_gen.py, all changes to this file will be undone\n\n'
23 filelist = glob.glob(os.path.join(self.kernels_folder, "*.cl"))
24 for filename in filelist:
26 print('processing {}'.format(filename))
27 res += self.cl_file_to_str(filename)
31 out_file_name = os.path.join(self.out_path, self.out_file_name)
32 #with open(out_file_name, 'r') as out_file:
33 # old_content = out_file.read()
34 #if old_content != res:
35 #print('Replacing old DB')
36 with open(out_file_name, 'w') as out_file:
39 def append_file_content(self, filename, origin_file):
42 with open(filename) as f:
43 content += f.readlines()
45 if line.startswith('#include'):
46 include_file_name = line.strip().split('"')[1].strip()
47 full_path_include = os.path.abspath(os.path.join(os.path.dirname(filename), include_file_name))
48 if full_path_include not in self.include_files[origin_file]:
49 self.include_files[origin_file][full_path_include] = True
50 res += self.append_file_content(full_path_include, origin_file)
53 res += '{}\n'.format(line.rstrip())
56 def cl_file_to_str(self, filename):
57 name = ntpath.basename(filename)
58 self.include_files[filename] = {}
59 #kernel_name = name[:name.find('.')]
60 kernel_name = name[:name.find('.cl')]
61 res = '{{"{}",\n(std::string) R"__krnl(\n'.format(kernel_name)
62 content = self.append_file_content(filename, filename)
65 for i, line in enumerate(content.split('\n')):
66 if i % max_lines == 0:
67 res += ')__krnl"\n + R"__krnl('
70 res += ')__krnl"}},\n\n'.format(kernel_name, self.append_file_content(filename, filename))
76 ap = argparse.ArgumentParser()
77 ap.add_argument('-kernels', required=True, metavar='PATH', help='The absolute path to OpenCL kernels folder')
78 ap.add_argument('-out_path', required=True, metavar='PATH', help='The absolute path to dump file')
79 ap.add_argument('-out_file_name', required=True, metavar='PATH', help='dump file name')
80 args = ap.parse_args()
82 converter = OpenCL2CHeaders(args.kernels, args.out_path, args.out_file_name)
85 if __name__ == '__main__':