blob: 673c37831307abad497657e200907a80b917172f [file] [log] [blame]
Brian Silverman3fec6482020-01-19 17:56:20 -08001from __future__ import print_function
2
3import json
4import sys
5import subprocess
6import os
7import threading
8
9from bazel_tools.tools.python.runfiles import runfiles
10
11def main(params):
12 r = runfiles.Create()
13 generator = r.Rlocation('org_frc971/y2020/vision/sift/fast_gaussian_generator')
14
15 ruledir = sys.argv[2]
16 target_cpu = sys.argv[3]
17
18 target = {
19 'armhf-debian': 'arm-32-linux-no_asserts',
20 'k8': 'x86-64-linux-no_asserts',
21 }[target_cpu]
22
23 commands = []
24
Jim Ostrowski38bb70b2020-02-21 20:46:10 -080025 amd64_debian_sysroot = r.Rlocation('amd64_debian_sysroot/usr/lib/x86_64-linux-gnu/libc.so.6').rsplit('/', 4)[0]
Brian Silverman3fec6482020-01-19 17:56:20 -080026 env = os.environ.copy()
27 env['LD_LIBRARY_PATH'] = ':'.join([
Jim Ostrowski38bb70b2020-02-21 20:46:10 -080028 amd64_debian_sysroot + '/lib/x86_64-linux-gnu',
29 amd64_debian_sysroot + '/lib',
30 amd64_debian_sysroot + '/usr/lib/x86_64-linux-gnu',
31 amd64_debian_sysroot + '/usr/lib',
Brian Silverman3fec6482020-01-19 17:56:20 -080032 ])
33
34 all_header = [
35 '#ifndef Y2020_VISION_SIFT_FAST_GAUSSIAN_ALL_H_',
36 '#define Y2020_VISION_SIFT_FAST_GAUSSIAN_ALL_H_',
37 '#include "HalideBuffer.h"',
38 ]
39
40 for cols, rows in params['sizes']:
41 for sigma, sigma_name, filter_width in params['sigmas']:
42 name = "fast_gaussian_%dx%d_%s" % (cols, rows, sigma_name)
43
44 commands.append([
45 generator,
46 '-g', 'gaussian_generator',
47 '-o', ruledir,
48 '-f', name,
49 '-e', 'o,h,html',
50 'target=%s-no_runtime' % target,
51 'cols=%s' % cols,
52 'rows=%s' % rows,
53 'sigma=%s' % sigma,
54 'filter_width=%s' % filter_width,
55 ])
56 all_header += [
57 '#include "y2020/vision/sift/%s.h"' % name,
58 ]
59
60 name = "fast_gaussian_subtract_%dx%d_%s" % (cols, rows, sigma_name)
61
62 commands.append([
63 generator,
64 '-g', 'gaussian_and_subtract_generator',
65 '-o', ruledir,
66 '-f', name,
67 '-e', 'o,h,html',
68 'target=%s-no_runtime' % target,
69 'cols=%s' % cols,
70 'rows=%s' % rows,
71 'sigma=%s' % sigma,
72 'filter_width=%s' % filter_width,
73 ])
74 all_header += [
75 '#include "y2020/vision/sift/%s.h"' % name,
76 ]
77
78 name = 'fast_subtract_%dx%d' % (cols, rows)
79 commands.append([
80 generator,
81 '-g', 'subtract_generator',
82 '-o', ruledir,
83 '-f', name,
84 '-e', 'o,h,html',
85 'target=%s-no_runtime' % target,
86 'cols=%s' % cols,
87 'rows=%s' % rows,
88 ])
89 all_header += [
90 '#include "y2020/vision/sift/%s.h"' % name,
91 ]
92 commands.append([
93 generator,
94 '-r', 'fast_gaussian_runtime',
95 '-o', ruledir,
96 '-e', 'o',
97 'target=%s' % target,
98 ])
99
100 all_header += [
101 'namespace frc971 {',
102 'namespace vision {',
103 '// 0 is success. 1 is non-implemented size. Negative is a Halide error.',
104 'inline int DoGeneratedFastGaussian(',
105 ' Halide::Runtime::Buffer<const int16_t, 2> input,',
106 ' Halide::Runtime::Buffer<int16_t, 2> output,',
107 ' double sigma) {',
108 ]
109
110 for sigma, sigma_name, filter_width in params['sigmas']:
111 for cols, rows in params['sizes']:
112 name = "fast_gaussian_%dx%d_%s" % (cols, rows, sigma_name)
113 all_header += [
114 ' if (input.dim(0).extent() == %s' % cols,
115 ' && input.dim(1).extent() == %s' % rows,
116 ' && sigma == %s) {' % sigma,
117 ' return %s(input, output);' % name,
118 ' }',
119 ]
120
121 all_header += [
122 ' return 1;',
123 '}',
124 'inline int DoGeneratedFastGaussianAndSubtract(',
125 ' Halide::Runtime::Buffer<const int16_t, 2> input,',
126 ' Halide::Runtime::Buffer<int16_t, 2> blurred,',
127 ' Halide::Runtime::Buffer<int16_t, 2> difference,',
128 ' double sigma) {',
129 ]
130
131 for sigma, sigma_name, filter_width in params['sigmas']:
132 for cols, rows in params['sizes']:
133 name = "fast_gaussian_subtract_%dx%d_%s" % (cols, rows, sigma_name)
134 all_header += [
135 ' if (input.dim(0).extent() == %s' % cols,
136 ' && input.dim(1).extent() == %s' % rows,
137 ' && sigma == %s) {' % sigma,
138 ' return %s(input, blurred, difference);' % name,
139 ' }',
140 ]
141
142 all_header += [
143 ' return 1;',
144 '}',
145 'inline int DoGeneratedFastSubtract('
146 ' Halide::Runtime::Buffer<const int16_t, 2> input_a,',
147 ' Halide::Runtime::Buffer<const int16_t, 2> input_b,',
148 ' Halide::Runtime::Buffer<int16_t, 2> output) {',
149 ]
150 for cols, rows in params['sizes']:
151 name = 'fast_subtract_%dx%d' % (cols, rows)
152 all_header += [
153 ' if (input_a.dim(0).extent() == %s' % cols,
154 ' && input_a.dim(1).extent() == %s) {' % rows,
155 ' return %s(input_a, input_b, output);' % name,
156 ' }',
157 ]
158 all_header += [
159 ' return 1;',
160 '}',
161 '} // namespace vision',
162 '} // namespace frc971',
163 '#endif // Y2020_VISION_SIFT_FAST_GAUSSIAN_ALL_H_',
164 ]
165
166 with open(os.path.join(ruledir, 'fast_gaussian_all.h'), 'w') as f:
167 f.writelines([line + '\n' for line in all_header])
168
169 commands_lock = threading.Lock()
170 success = [True]
171
172 def run_commands():
173 while True:
174 with commands_lock:
175 if not commands:
176 return
177 if not success[0]:
178 return
179 command = commands.pop()
180 try:
181 subprocess.check_call(command, env=env)
182 except:
183 with commands_lock:
184 success[0] = False
185 raise
186 threads = [threading.Thread(target=run_commands) for _ in range(4)]
187 for thread in threads:
188 thread.start()
189 for thread in threads:
190 thread.join()
191 if not success[0]:
192 sys.exit(1)
193
194if __name__ == '__main__':
195 main(json.loads(sys.argv[1]))