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