1 | #!/usr/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | from __future__ import print_function |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import re |
---|
8 | import subprocess |
---|
9 | import shutil |
---|
10 | import filecmp |
---|
11 | |
---|
12 | #images = ['../../images/boulons.pgm', '../../images/cadastre.pgm', '../../images/alea1.pgm', '../../images/alea2.pgm', '../../images/alea3.pgm'] |
---|
13 | images = ['../../images/alea1.pgm'] |
---|
14 | config_file = "include/config.h" |
---|
15 | |
---|
16 | use_valgrind = False |
---|
17 | infinite_mode = True |
---|
18 | |
---|
19 | threads = [64] |
---|
20 | binary = "./appli.elf" |
---|
21 | outimages_dir = "output_images" |
---|
22 | log_dir = "logs" |
---|
23 | |
---|
24 | configs = [ |
---|
25 | # { |
---|
26 | # 'SLOW':'1', |
---|
27 | # 'FAST':'0', |
---|
28 | # 'FEATURES':'0', |
---|
29 | # 'PARMERGE':'0' |
---|
30 | # }, |
---|
31 | # { |
---|
32 | # 'SLOW':'0', |
---|
33 | # 'FAST':'1', |
---|
34 | # 'FEATURES':'0', |
---|
35 | # 'PARMERGE':'0' |
---|
36 | # }, |
---|
37 | # { |
---|
38 | # 'SLOW':'1', |
---|
39 | # 'FAST':'0', |
---|
40 | # 'FEATURES':'1', |
---|
41 | # 'PARMERGE':'0' |
---|
42 | # }, |
---|
43 | # { |
---|
44 | # 'SLOW':'0', |
---|
45 | # 'FAST':'1', |
---|
46 | # 'FEATURES':'1', |
---|
47 | # 'PARMERGE':'0' |
---|
48 | # }, |
---|
49 | { |
---|
50 | 'SLOW':'0', |
---|
51 | 'FAST':'1', |
---|
52 | 'FEATURES':'1', |
---|
53 | 'PARMERGE':'1' |
---|
54 | } |
---|
55 | ] |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | def my_mkdir(dirname): |
---|
60 | try: |
---|
61 | print("mkdir %s" % (dirname)) |
---|
62 | os.mkdir(dirname) |
---|
63 | except OSError: |
---|
64 | print("*** Error: impossible to create directory %s" % (dirname), file=sys.stderr) |
---|
65 | sys.exit(1) |
---|
66 | |
---|
67 | def my_chdir(dirname): |
---|
68 | print("cd %s" % (dirname)); |
---|
69 | os.chdir(dirname) |
---|
70 | |
---|
71 | def my_cp(src, dst): |
---|
72 | print("cp %s %s" % (src, dst)) |
---|
73 | shutil.copy(src, dst) |
---|
74 | |
---|
75 | def print_and_call(cmd): |
---|
76 | print(subprocess.list2cmdline(cmd)) |
---|
77 | retval = subprocess.call(cmd) |
---|
78 | return retval |
---|
79 | |
---|
80 | def print_and_popen(cmd, outfile): |
---|
81 | print(subprocess.list2cmdline(cmd), end = "") |
---|
82 | print(" >", outfile) |
---|
83 | output = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT).communicate()[0] |
---|
84 | return output |
---|
85 | |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | def update_config_file(config): |
---|
90 | if os.path.isfile(config_file): |
---|
91 | print("# Updating file %s" % (config_file)) |
---|
92 | f = open(config_file, "r") |
---|
93 | lines = f.readlines() |
---|
94 | f.close() |
---|
95 | |
---|
96 | f = open(config_file, "w") |
---|
97 | |
---|
98 | for line in lines: |
---|
99 | line_with_key = False |
---|
100 | for key in config.keys(): |
---|
101 | if "#define %s" % (key) in line: |
---|
102 | f.write("#define %s %s\n" % (key, config[key])) |
---|
103 | line_with_key = True |
---|
104 | break |
---|
105 | if not line_with_key: |
---|
106 | f.write(line) |
---|
107 | |
---|
108 | f.close() |
---|
109 | else: |
---|
110 | print("# Creating file %s" % (config_file)) |
---|
111 | f = open(config_file, "w") |
---|
112 | f.write("\n") |
---|
113 | for key in config.keys(): |
---|
114 | f.write("#define %s %s\n" % (key, config[key])) |
---|
115 | f.write("\n") |
---|
116 | f.close() |
---|
117 | |
---|
118 | |
---|
119 | root_dir = os.getcwd() |
---|
120 | |
---|
121 | |
---|
122 | if not os.path.exists(outimages_dir): |
---|
123 | my_mkdir(outimages_dir) |
---|
124 | |
---|
125 | if not os.path.exists(log_dir): |
---|
126 | my_mkdir(log_dir) |
---|
127 | |
---|
128 | |
---|
129 | stat_array = {} |
---|
130 | finished = False |
---|
131 | |
---|
132 | for config in configs: |
---|
133 | update_config_file(config) |
---|
134 | cmd = ['make'] |
---|
135 | print_and_call(cmd) |
---|
136 | for image in images: |
---|
137 | ref_bmpfile = os.path.join(outimages_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.bmp") |
---|
138 | ref_statfile = os.path.join(outimages_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.txt") |
---|
139 | for nthreads in threads: |
---|
140 | while not finished: |
---|
141 | if not os.path.exists(ref_bmpfile): |
---|
142 | bmpfile = ref_bmpfile |
---|
143 | else: |
---|
144 | bmpfile = os.path.join(outimages_dir, os.path.splitext(os.path.basename(image))[0] + ".bmp") |
---|
145 | |
---|
146 | if not os.path.exists(ref_statfile): |
---|
147 | statfile = ref_statfile |
---|
148 | else: |
---|
149 | statfile = os.path.join(outimages_dir, os.path.splitext(os.path.basename(image))[0] + ".txt") |
---|
150 | |
---|
151 | cmd = [] |
---|
152 | if use_valgrind: |
---|
153 | cmd.append('valgrind') |
---|
154 | |
---|
155 | cmd.extend([binary, '-n', str(nthreads), '-i', image, '-o', bmpfile, '-g']) |
---|
156 | |
---|
157 | if config['FEATURES'] == '1': |
---|
158 | cmd.append('-d') |
---|
159 | |
---|
160 | config_keys = config.keys() |
---|
161 | logfile = os.path.join(log_dir, os.path.splitext(os.path.basename(image))[0] + "_" + str(nthreads) + "_" + "_".join(map(lambda x:'%s_%s' % (x, config[x]), config_keys)) + ".txt") |
---|
162 | output = print_and_popen(cmd, logfile) |
---|
163 | |
---|
164 | # Write simulation results to log file |
---|
165 | file = open(logfile, 'w') |
---|
166 | file.write(output) |
---|
167 | file.close() |
---|
168 | |
---|
169 | if bmpfile != ref_bmpfile: |
---|
170 | if filecmp.cmp(bmpfile, ref_bmpfile): |
---|
171 | print("# Files %s and %s are identical" % (bmpfile, ref_bmpfile)) |
---|
172 | else: |
---|
173 | print("*** Error: files %s and %s differ" % (bmpfile, ref_bmpfile)) |
---|
174 | sys.exit(1) |
---|
175 | |
---|
176 | if use_valgrind: |
---|
177 | if not "== ERROR SUMMARY: 0 errors from 0 contexts" in output: |
---|
178 | print("*** Error: Valgrind error") |
---|
179 | sys.exit(1) |
---|
180 | if not "== All heap blocks were freed -- no leaks are possible" in output: |
---|
181 | print("*** Error: Valgrind detected a memory leak") |
---|
182 | sys.exit(1) |
---|
183 | |
---|
184 | if config['FEATURES'] == '1': |
---|
185 | stat_array = {} |
---|
186 | in_stats = False |
---|
187 | outlines = output.splitlines() |
---|
188 | index = 0 |
---|
189 | for line in outlines: |
---|
190 | #print("bla i") |
---|
191 | #print(line) |
---|
192 | if "[STATS]" in line: |
---|
193 | in_stats = True |
---|
194 | continue |
---|
195 | if "[/STATS]" in line: |
---|
196 | in_stat = False |
---|
197 | break |
---|
198 | if in_stats: |
---|
199 | tokens = line.split() |
---|
200 | #print("#len : %d" % len(tokens)) |
---|
201 | #print(tokens[0]) |
---|
202 | assert(len(tokens) == 8) |
---|
203 | stat_array[index] = {} |
---|
204 | for j in range(len(tokens)): |
---|
205 | stat_array[index][j] = tokens[j] |
---|
206 | index += 1 |
---|
207 | |
---|
208 | |
---|
209 | # Dump stat array in stat file |
---|
210 | file = open(statfile, 'w') |
---|
211 | for i in range(len(stat_array)): |
---|
212 | for j in range(8): |
---|
213 | file.write("%s " % stat_array[i][j]) |
---|
214 | file.write("\n"); |
---|
215 | file.close() |
---|
216 | |
---|
217 | # Comparison to reference |
---|
218 | if statfile != ref_statfile: |
---|
219 | if filecmp.cmp(statfile, ref_statfile): |
---|
220 | print("# Feature files %s and %s are identical" % (statfile, ref_statfile)) |
---|
221 | else: |
---|
222 | print("*** Error: feature files %s and %s differ" % (statfile, ref_statfile)) |
---|
223 | sys.exit(1) |
---|
224 | |
---|
225 | if not infinite_mode: |
---|
226 | finished = True |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|