Advertisement
Guest User

exp runner

a guest
May 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. from subprocess import Popen, PIPE
  2.  
  3. def flatten_list_of_strings(args):
  4.     output = ''
  5.     for s in args:
  6.         output += s
  7.         output += ' '
  8.     return output
  9.  
  10. def str_to_num(s):
  11.     if '.' in s:
  12.         return float(s)
  13.     else:
  14.         return int(s)
  15.  
  16. class ExpRunner():
  17.  
  18.     def __init__(self, filter_name, input_image_path, implementation, output_image_dir, verbose=False):
  19.         self.tp_exec = '../build/tp2'
  20.         self.filter_name = filter_name
  21.         self.exp_results = {'filter_name': filter_name, 'implementation': implementation}
  22.         self.run_args = [self.tp_exec,
  23.                          filter_name,
  24.                          input_image_path,
  25.                          "-i", implementation,
  26.                          "-o", output_image_dir]
  27.         self.verbose = verbose
  28.        
  29.     def exp_keys(self):
  30.         '''
  31.        ["Comienzo",
  32.                "Fin",
  33.                "# de ciclos insumidos totales",  
  34.                "# de ciclos insumidos por llamada"]
  35.        '''
  36.         return ["# de ciclos insumidos totales"] # por ahora solo nos interesa esta
  37.  
  38.     def run_executable(self,run_args):
  39.         p = Popen(run_args, stdout=PIPE, bufsize=1)
  40.         with p.stdout:
  41.             for line in iter(p.stdout.readline, b''):
  42.                 line = line.decode("utf-8")
  43.                 print(line)
  44.                 try:
  45.                     key, val = line[:-1].split(':')
  46.                     key = key.strip(" ")
  47.                     if(key in self.exp_keys()):
  48.                         self.exp_results[key] = str_to_num(val)
  49.                 except:
  50.                     pass
  51.         p.wait() # wait for the subprocess to exit
  52.  
  53.     def print_help(self):
  54.         self.run_executable([self.tp_exec, "-h"])
  55.         return
  56.            
  57.     def run(self):
  58.         if self.verbose:
  59.             print("Run command:")
  60.             print(flatten_list_of_strings(self.run_args))
  61.             print
  62.         self.run_executable(self.run_args)
  63.         return dict(self.exp_results)
  64.  
  65.  
  66. from shutil import copyfile
  67. from subprocess import check_call
  68.  
  69. class MultipleAsmImplementationsExpRunner(ExpRunner):
  70.    
  71.     def setup_asm_implementation(self, asm_implementation):
  72.         filters_path = "../filters/"
  73.         dest_implementation = "asm"
  74.        
  75.         src_asm = filters_path + self.filter_name + '_' + asm_implementation + ".asm"
  76.         print(src_asm)
  77.         dst_asm = filters_path + self.filter_name + '_' + dest_implementation + ".asm"
  78.         print(dst_asm)
  79.         copyfile(src_asm, dst_asm)
  80.        
  81.     def make_executable(self):
  82.         check_call(["make"], cwd="../")
  83.    
  84.     def run(self, asm_implementation):
  85.         self.setup_asm_implementation(asm_implementation)
  86.         self.make_executable()
  87.         super().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement