Changeset 826 for soft/giet_vm/applications/rosenfeld/scripts/run_simus.py
- Timestamp:
- Jul 13, 2017, 11:01:58 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/rosenfeld/scripts/run_simus.py
r823 r826 5 5 # python 2 as it will execute on computation servers 6 6 7 # TODO:8 # Can we do something about assert in perf eval?7 # FIXME? 8 # When deactivating assert for perf eval, exec times are slower... 9 9 10 10 from __future__ import print_function … … 29 29 base_logs_dir = "logs" # Log of execution, i.e. what is printed on screen 30 30 base_data_dir = "data" # For timing information extracted 31 base_rand_img_dir = "random_images" # For storing random images if using dsk32 31 33 32 … … 43 42 # - With check_results, num_app_runs should be used, so as to have a number of checks equals to the number of runs, 44 43 # because only one check per application run is performed 45 num_app_runs = 1 46 num_internal_runs = 20# Number of times the image is processed inside the application44 num_app_runs = 1 # Number of times the application is launched per configuration 45 num_internal_runs = 10 # Number of times the image is processed inside the application 47 46 check_results = False 48 47 eval_perf = True 49 48 use_valgrind = False 50 49 use_rand_images = True 51 threads = [1, 2, 4, 8, 16, 32, 64] 52 #threads = [1, 2] 50 threads = [1, 4, 16, 64] 53 51 use_dsk = True 54 52 # Using dsk will store generated random images, otherwise they are re-generated at each run to save disk space 53 granularities = [1, 4, 16] 54 img_size = 2048 55 55 56 56 # Configurations 57 57 configs = [ 58 58 #{'SLOW':'1', 'FAST':'0', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'}, 59 #{'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'},59 {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'0', 'ARSP':'0'}, 60 60 #{'SLOW':'1', 'FAST':'0', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'}, 61 #{'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'},61 {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'0', 'ARSP':'0'}, 62 62 {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'0'}, 63 #{'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'0'}, 64 #{'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'1'}, 63 {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'0'}, 64 {'SLOW':'0', 'FAST':'1', 'FEATURES':'0', 'PARMERGE':'1', 'ARSP':'1'}, 65 {'SLOW':'0', 'FAST':'1', 'FEATURES':'1', 'PARMERGE':'1', 'ARSP':'1'}, 65 66 ] 66 67 … … 68 69 # Other parameters which shouldn't be changed 69 70 rand_seed = 7 70 granularity = 1 # constant for now71 img_size = 204872 71 73 72 check_pyconf_file(pyconf_file) … … 83 82 os.mkdir(dsk_dir) 84 83 except NameError: 85 print("*** Warning: variable dsk_dir is not defined in %s file; using current directory for storing output files" % (short_path(pyconf_file)))84 print("*** Warning: variable dsk_dir is not defined in file %s; using current directory for storing output files" % (short_path(pyconf_file))) 86 85 use_dsk = False 87 86 except OSError: 88 87 print("*** Warning: Impossible to create directory %s; using current directory for storing output files" % (dsk_dir)) 89 88 use_dsk = False 89 90 if use_rand_images: 91 try: 92 rand_img_dir 93 if not os.path.exists(rand_img_dir): 94 print("mkdir %s", rand_img_dir) 95 os.mkdir(rand_img_dir) 96 except NameError: 97 print("*** Error: variable rand_img_dir, containing the path the directory of the random images (either pre-existing or to be generated), is not defined in file %s" % (short_path(pyconf_file))) 98 sys.exit(1) 99 except OSError: 100 print("*** Error: Impossible to create directory %s" % (rand_img_dir)) 101 sys.exit(1) 90 102 91 103 … … 95 107 logs_dir = os.path.join(dsk_dir, base_logs_dir) 96 108 data_dir = os.path.join(dsk_dir, base_data_dir) 97 rand_img_dir = os.path.join(dsk_dir, base_rand_img_dir)98 109 else: 99 110 output_dir = os.path.join(scripts_path, base_output_dir) … … 110 121 if check_results and num_internal_runs != 1: 111 122 print("*** Warning: using check_results with num_internal_runs != 1\n") 112 113 114 115 def gen_random_image(filename, x, y, granularity, density, seed):116 random.seed(seed)117 img = [[0 for a in range(x)] for b in range(y)]118 for i in range(0, x, granularity):119 for j in range(0, y, granularity):120 r = random.random();121 if r < density:122 px = 255123 else:124 px = 0125 126 for di in range(0, granularity):127 for dj in range(0, granularity):128 if i + di < x and j + dj < y:129 img[i + di][j + dj] = px;130 131 f = open(filename, 'wb')132 f.write("P5\n%d %d\n255\n" % (x, y))133 for j in range(0, y):134 bimg = bytearray(img[j])135 f.write(bimg)136 f.close()137 123 138 124 … … 186 172 my_mkdir(data_dir) 187 173 188 if use_dsk and not os.path.exists(rand_img_dir):189 my_mkdir(rand_img_dir)190 191 192 174 193 175 … … 196 178 197 179 for config in configs: 198 img_idx = 0199 180 fconfig = frozenset(config.iteritems()) 200 181 perf_array[fconfig] = {} … … 210 191 my_chdir(scripts_path) 211 192 212 while not use_rand_images and img_idx != len(images) or use_rand_images and img_idx != 101: 213 # Compute image and stat filenames 214 if use_rand_images: 215 if use_dsk: 193 for granularity in granularities: 194 perf_array[fconfig][granularity] = {} 195 img_idx = 0 196 while not use_rand_images and img_idx != len(images) or use_rand_images and img_idx != 101: 197 # Compute image and stat filenames 198 if use_rand_images: 216 199 random_img_file = get_random_img_file(img_idx, img_size, img_size, granularity, rand_seed) 217 200 random_img_file = os.path.join(rand_img_dir, random_img_file) 201 if not os.path.isfile(random_img_file): 202 # We generate the random image if it does not exist 203 print("# Generating random image %s with granularity = %d and density = %d" % (random_img_file, granularity, img_idx)) 204 gen_random_image(random_img_file, img_size, img_size, granularity, float(img_idx) / 100, rand_seed) 205 image = random_img_file 218 206 else: 219 random_img_file = "random.pgm" 220 if not (use_dsk and os.path.isfile(random_img_file)): 221 # We re-generate the random image if not using the dsk or if it does not exist 222 print("# Generating random image %s with density = %d" % (random_img_file, img_idx)) 223 gen_random_image(random_img_file, img_size, img_size, 1, float(img_idx) / 100, rand_seed) 224 225 image = random_img_file 226 else: 227 image = images[img_idx] 228 img_basename = os.path.splitext(os.path.basename(image))[0] 229 perf_array[fconfig][img_basename] = {} 230 ref_bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.bmp") 231 ref_statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.txt") 232 233 for nthreads in threads: 234 perf_array[fconfig][img_basename][nthreads] = {} 235 for run in range(num_app_runs): 236 if not os.path.exists(ref_bmpfile): 237 bmpfile = ref_bmpfile 238 else: 239 bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".bmp") 240 if os.path.exists(bmpfile): 241 os.remove(bmpfile) 242 243 if not os.path.exists(ref_statfile): 244 statfile = ref_statfile 245 else: 246 statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".txt") 247 248 cmd = [] 249 if use_valgrind: 250 cmd.append('valgrind') 251 252 cmd.extend([short_path(binary_file), '-n', str(nthreads), '-i', short_path(image)]) 253 254 if num_internal_runs > 1: 255 cmd.extend(['-r', str(num_internal_runs)]) 256 257 if check_results: 258 cmd.extend(['-o', short_path(bmpfile), '-g']) 259 260 if check_results and features: 261 cmd.append('-d') 262 263 config_keys = config.keys() 264 logfile = get_filename(logs_dir, nthreads, config, features, img_basename) 265 output = print_and_popen(cmd, logfile) 266 outlines = output.splitlines() 267 268 # if performance evaluation, get timing measurements 269 # Only the last application run is considered 207 image = images[img_idx] 208 img_basename = os.path.splitext(os.path.basename(image))[0] 209 perf_array[fconfig][granularity][img_basename] = {} 210 ref_bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.bmp") 211 ref_statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + "_ref.txt") 212 213 for nthreads in threads: 214 perf_array[fconfig][granularity][img_basename][nthreads] = {} 215 for run in range(num_app_runs): 216 if not os.path.exists(ref_bmpfile): 217 # Generating the reference file if it does not exist 218 bmpfile = ref_bmpfile 219 else: 220 bmpfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".bmp") 221 if os.path.exists(bmpfile): 222 os.remove(bmpfile) 223 224 if not os.path.exists(ref_statfile): 225 statfile = ref_statfile 226 else: 227 statfile = os.path.join(output_dir, os.path.splitext(os.path.basename(image))[0] + ".txt") 228 if os.path.exists(statfile): 229 os.remove(statfile) 230 231 cmd = [] 232 if use_valgrind: 233 cmd.append('valgrind') 234 235 cmd.extend([short_path(binary_file), '-n', str(nthreads), '-i', short_path(image)]) 236 237 if num_internal_runs > 1: 238 cmd.extend(['-r', str(num_internal_runs)]) 239 240 if check_results: 241 cmd.extend(['-o', short_path(bmpfile), '-g']) 242 243 if check_results and features: 244 cmd.append('-d') 245 246 config_keys = config.keys() 247 logfile = get_filename(logs_dir, nthreads, config, features, img_basename) 248 output = print_and_popen(cmd, logfile) 249 outlines = output.splitlines() 250 251 # if performance evaluation, get timing measurements 252 # Only the last application run is considered 253 if eval_perf: 254 for line in outlines: 255 tokens = line.split() 256 if len(tokens) == 0: 257 continue 258 tag = tokens[0] 259 pattern = re.compile('\[THREAD_STEP_([0-9]+)\]'); 260 match = pattern.match(tag) 261 if match: 262 step = match.group(1) 263 value = tokens[len(tokens) - 1] 264 perf_array[fconfig][granularity][img_basename][nthreads][step] = int(value) 265 266 267 # Checking against reference output image 268 if check_results and bmpfile != ref_bmpfile: 269 print("diff %s %s" % (short_path(bmpfile), short_path(ref_bmpfile))) 270 if not filecmp.cmp(bmpfile, ref_bmpfile): 271 print("*** Error: files %s and %s differ" % (short_path(bmpfile), short_path(ref_bmpfile))) 272 sys.exit(1) 273 274 # Checking for valgrind errors 275 if use_valgrind: 276 if not "== ERROR SUMMARY: 0 errors from 0 contexts" in output: 277 print("*** Error: Valgrind error") 278 sys.exit(1) 279 if not "== All heap blocks were freed -- no leaks are possible" in output: 280 print("*** Error: Valgrind detected a memory leak") 281 sys.exit(1) 282 283 # Extracting features for correctness verification 284 if check_results and features: 285 stat_array = {} 286 in_stats = False 287 index = 0 288 for line in outlines: 289 if "[STATS]" in line: 290 in_stats = True 291 continue 292 if "[/STATS]" in line: 293 in_stats = False 294 break 295 if in_stats: 296 tokens = line.split() 297 assert(len(tokens) == 8) 298 stat_array[index] = {} 299 for j in range(len(tokens)): 300 stat_array[index][j] = tokens[j] 301 index += 1 302 303 # Dump stat array in stat file 304 file = open(statfile, 'w') 305 for i in range(len(stat_array)): 306 for j in range(8): # 8 is the number of features per element 307 file.write("%s " % stat_array[i][j]) 308 file.write("\n"); 309 file.close() 310 311 # Comparison to reference 312 if statfile != ref_statfile: 313 print("diff %s %s" % (short_path(statfile), short_path(ref_statfile))) 314 if not filecmp.cmp(statfile, ref_statfile): 315 print("*** Error: feature files %s and %s differ" % (short_path(statfile), short_path(ref_statfile))) 316 sys.exit(1) 317 318 # End of the num_runs simus 270 319 if eval_perf: 271 for line in outlines: 272 tokens = line.split() 273 if len(tokens) == 0: 274 continue 275 tag = tokens[0] 276 pattern = re.compile('\[THREAD_STEP_([0-9]+)\]'); 277 match = pattern.match(tag) 278 if match: 279 step = match.group(1) 280 value = tokens[len(tokens) - 1] 281 perf_array[fconfig][img_basename][nthreads][step] = int(value) 282 283 284 # Checking against reference output image 285 if check_results and bmpfile != ref_bmpfile: 286 print("diff %s %s" % (short_path(bmpfile), short_path(ref_bmpfile))) 287 if not filecmp.cmp(bmpfile, ref_bmpfile): 288 print("*** Error: files %s and %s differ" % (short_path(bmpfile), short_path(ref_bmpfile))) 289 sys.exit(1) 290 291 # Checking for valgrind errors 292 if use_valgrind: 293 if not "== ERROR SUMMARY: 0 errors from 0 contexts" in output: 294 print("*** Error: Valgrind error") 295 sys.exit(1) 296 if not "== All heap blocks were freed -- no leaks are possible" in output: 297 print("*** Error: Valgrind detected a memory leak") 298 sys.exit(1) 299 300 # Extracting features for correctness verification 301 if check_results and features: 302 stat_array = {} 303 in_stats = False 304 index = 0 305 for line in outlines: 306 if "[STATS]" in line: 307 in_stats = True 308 continue 309 if "[/STATS]" in line: 310 in_stats = False 311 break 312 if in_stats: 313 tokens = line.split() 314 assert(len(tokens) == 8) 315 stat_array[index] = {} 316 for j in range(len(tokens)): 317 stat_array[index][j] = tokens[j] 318 index += 1 319 320 # Dump stat array in stat file 321 file = open(statfile, 'w') 322 for i in range(len(stat_array)): 323 for j in range(8): 324 file.write("%s " % stat_array[i][j]) 325 file.write("\n"); 326 file.close() 327 328 # Comparison to reference 329 if statfile != ref_statfile: 330 print("diff %s %s" % (short_path(statfile), short_path(ref_statfile))) 331 if not filecmp.cmp(statfile, ref_statfile): 332 print("*** Error: feature files %s and %s differ" % (short_path(statfile), short_path(ref_statfile))) 333 sys.exit(1) 334 335 # End of the num_runs simus 336 if eval_perf: 337 if use_rand_images: 338 idx = img_idx 339 else: 340 idx = None 341 datafile = get_filename(data_dir, nthreads, config, features, img_basename) 342 file = open(datafile, 'w') 343 for step in sorted(perf_array[fconfig][img_basename][nthreads].keys()): 344 # Average time for each step 345 file.write("[STEP_%s] %d\n" % (step, perf_array[fconfig][img_basename][nthreads][step])) 346 347 img_idx += 1 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 320 datafile = get_filename(data_dir, nthreads, config, features, img_basename) 321 file = open(datafile, 'w') 322 for step in sorted(perf_array[fconfig][granularity][img_basename][nthreads].keys()): 323 # Average time for each step 324 file.write("[STEP_%s] %d\n" % (step, perf_array[fconfig][granularity][img_basename][nthreads][step])) 325 326 img_idx += 1 327 #end image list (101 rand or explicit list) 328 #end granularity 329 #end config 330 331 332 333 334 335 336 337 338 339 340 341 342 343
Note: See TracChangeset
for help on using the changeset viewer.