#!/usr/bin/env python3
import subprocess
import pandas
import os
import time
import json
import re

batchsize = [32]
epoch = 10 

perf_counters_multipliers = {'r5302c7': 1, 
                             'r5308c7': 4,
                             'r5320c7': 8}

def run(params, command):
    for key, val in params.items():
        command.append(f"--{key}={val}")
    print(command)
    proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    proc.wait()
    out = proc.stdout.read().decode()
    err = proc.stderr.read().decode()
    if proc.returncode == 0:
        output_dict = json.loads(out)
        return output_dict
    else:
        print(out)
        print(err)


def get_counters(params, output_dict):
    output_dict["flop_measured"] = 0
    for counter in perf_counters_multipliers:
        perf_command = ["perf", "stat" , "-e", counter, "python3", "-m", "benchmarker", "--no_cudnn_benchmark"]
        for key, val in params.items():
            perf_command.append(f"--{key}={val}")
        print(perf_command)
        proc = subprocess.Popen(perf_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        proc.wait()
        out = proc.stdout.read().decode()
        err = proc.stderr.read().decode()
        print(err)
        #if proc.returncode != 0:
        #print(out)
        #print(err)
        if err:
            match_exp = re.compile('[\d|\,]+\s+' + counter).search(err)
            if match_exp:
                match_list = match_exp.group().split()
                cntr_value = int(match_list[0].replace(',',''))
                output_dict["flop_measured"] += perf_counters_multipliers[counter]*cntr_value
    print(output_dict)

            

def main():
    params = {}
    params["framework"] = "pytorch"
    params["mode"] = "inference"

    specs = pandas.read_csv("./conv2d.csv")
    for size in batchsize: 
        params["batch_size"] = size
        for row in specs.iterrows():
            spec = row[1]
            params["problem"] = spec["conv_type"]
            prob_size = params["batch_size"] * 4

            if params["problem"] == 'conv2d':
                params["problem_size"] = (f"{prob_size},"
                                      f"{spec['in_channels']},"
                                      f"{spec['in_width']},"
                                      f"{spec['in_height']}")
        
            params["cnt_filters"] = spec["cnt_filters"]
            params["size_kernel"] = spec["width_kernel"]
            params["stride"] = spec["stride"]
            params["dilation"] = spec["dilation"]
            params["padding"] = spec["padding"]
            params["nb_epoch"] = epoch 
            #print(params)
         
            command = ["python3", "-m", "benchmarker", "--no_cudnn_benchmark"] 

            #Run benchmarker with params and get output as dict
            output_dict = run(params, command)
            print(output_dict)

            #Collect counters, add to the dict
            get_counters(params, output_dict)

if __name__ == "__main__":
    main()

@@ -254,16 +254,17 @@
 7': 8%7D%0A%0A
+%0A
 def run(
@@ -595,112 +595,25 @@
     
-output_dict = json.loads(out)%0A        return output_dict%0A    else:%0A        print(out)%0A        print(err)
+return %5Bout, err%5D
 %0A%0A%0Ad
@@ -659,32 +659,43 @@
    output_dict%5B%22
+problem%22%5D%5B%22
 flop_measured%22%5D 
@@ -868,333 +868,90 @@
     
-for key, val in params.items():%0A            perf_command.append(f%22--%7Bkey%7D=%7Bval%7D%22)%0A        print(perf_command)%0A        proc = subprocess.Popen(perf_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)%0A        proc.wait()%0A        out = proc.stdout.read().decode()%0A        err = proc.stderr.read().decode()%0A        print(err
+output_string, error_string = run(params, perf_command)%0A        print(error_string
 )%0A  
@@ -960,88 +960,23 @@
     
-#
 if 
-proc.returncode != 0:%0A        #print(out)%0A        #print(err)%0A        if err
+error_string
 :%0A  
@@ -1043,16 +1043,25 @@
 arch(err
+or_string
 )%0A      
@@ -1228,16 +1228,27 @@
 t_dict%5B%22
+problem%22%5D%5B%22
 flop_mea
@@ -1304,30 +1304,31 @@
 r_value%0A    
-print(
+return 
 output_dict)
@@ -1318,34 +1318,32 @@
 turn output_dict
-)%0A
 %0A            %0A%0Ad
@@ -2508,20 +2508,36 @@
  output_
-dict
+string, error_string
  = run(p
@@ -2544,32 +2544,84 @@
 arams, command)%0A
+            output_dict = json.loads(output_string)%0A
             prin
@@ -2694,16 +2694,32 @@
         
+ dict_with_ops =
  get_cou
@@ -2744,16 +2744,49 @@
 ut_dict)
+%0A            print(dict_with_ops)
 %0A%0Aif __n

refactor code