Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / kernel_selector / core / common / primitive_db_gen.py
1 #!/usr/bin/python
2
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
6
7 from __future__ import print_function
8 import os
9 import argparse
10 import glob
11 import ntpath
12
13 class OpenCL2CHeaders(object):
14
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 = {}
20
21     def convert(self):
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:
25             #try:
26                 print('processing {}'.format(filename))
27                 res += self.cl_file_to_str(filename)
28             #except:
29             #    pass
30
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:
37             out_file.write(res)
38
39     def append_file_content(self, filename, origin_file):
40         res = ""
41         content = []
42         with open(filename) as f:
43             content += f.readlines()
44         for line in content:
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)
51                     res += "\n"
52                 continue
53             res += '{}\n'.format(line.rstrip())
54         return res
55
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)
63         max_lines = 200
64
65         for i, line in enumerate(content.split('\n')):
66             if i % max_lines == 0:
67                 res += ')__krnl"\n + R"__krnl('
68             res += line + '\n'
69
70         res += ')__krnl"}},\n\n'.format(kernel_name, self.append_file_content(filename, filename))
71
72         return res
73
74
75 def main():
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()
81
82     converter = OpenCL2CHeaders(args.kernels, args.out_path, args.out_file_name)
83     converter.convert()
84
85 if __name__ == '__main__':
86     main()