VEGA_AE / Scripts /Exp /ForkFlow /gen_forkflow.py
unknown
Initial
33264ad
raw
history blame
6.77 kB
# -*- encoding: utf-8 -*-
from asyncore import write
import My_Difflib
from distutils.command.config import config
import os, sys,string,re,glob
import html
import csv
import copy
from tqdm import tqdm
import pathlib
folder = str(pathlib.Path(__file__).parent.resolve())
Wrong_Dir = folder
Mips_Dir = folder + "/Mips_Code"
VEGA_Dir = folder + "/VEGA_Code"
Ans_Dir = folder + "/Std_Code"
count_wrong = 0
Mod_File_Func = {}
Mod_Result = {}
def deal_dir(src_dir):
global Mod_File_Func
global Mod_Result
if not os.path.exists(src_dir):
return False
filelists = os.listdir(src_dir)
for eachfile in filelists:
tem =eachfile
eachfile = src_dir + '/' +eachfile
if os.path.isdir(eachfile):
deal_dir(eachfile)
elif os.path.isfile(eachfile) and ".cpp" in eachfile:
module = eachfile.split("/")[-3]
if module not in Mod_Result.keys():
Mod_Result[module] = [0,0,0]
file = eachfile.split("/")[-2]
func = eachfile.split("/")[-1].replace(".cpp", "")
if (" ").join([module, file, func]) not in Mod_File_Func.keys():
Mod_File_Func[(" ").join([module, file, func])] = []
codes = ""
f2 = open(eachfile, encoding = "utf-8")
lines = f2.readlines()
for idx, l in enumerate(lines):
if idx == len(lines) - 1:
codes += l.replace(" ", "").replace("\n", "").lower().replace("mips","").replace("xcore", "").replace("pulp", "").replace("riscv", "")
else:
codes += l.replace(" ", "").lower().replace("mips","").replace("xcore", "").replace("pulp", "").replace("riscv", "")
f2.close()
if "/Mips" in eachfile:
Mod_File_Func[(" ").join([module, file, func])].append(["Mips", codes])
elif "/Std_Code/" in eachfile:
Mod_File_Func[(" ").join([module, file, func])].append(["Ans", codes])
elif "/VEGA" in eachfile:
Mod_File_Func[(" ").join([module, file, func])].append(["VEGA", codes])
return True
wrong_stmt = []
def get_wrong_list():
global wrong_stmt
with open(Wrong_Dir+"/wrong_list_all.csv", 'r', encoding='utf-8') as fcsv:
reader = csv.reader(fcsv)
for row in reader:
if row[0] == "idx":
continue
wrong_stmt.append(row[0].strip().lower() + " " + row[1].strip().lower() + " " + row[2].strip().lower())
def duplicate_data(tar):
global wrong_stmt
global Mod_File_Func
global Mod_Result
for kv in Mod_File_Func.items():
module = kv[0].split(" ")[0]
if len(kv[1]) != 3:
print(tar + " " + kv[0])
Ans = ""
Vega = ""
Mips = ""
for codes in kv[1]:
if codes[0] == "Mips":
Mips = codes[1]
#print(codes[1])
if codes[0] == "Ans":
Ans = codes[1]
if codes[0] == "VEGA":
Vega = codes[1]
total = len(Ans.split("\n"))
#print(Mod_Result)
Mod_Result[module][0] += total
vega_same = 0
vega_modi = 0
vega_add = 0
Mips_same = 0
Mips_modi = 0
Mips_add = 0
diff_vega = list(My_Difflib.Differ().compare(Ans.splitlines(), Vega.splitlines()))
for idx, dv in enumerate(diff_vega):
if dv[0] == '-':
if idx < len(diff_vega) - 1 and diff_vega[idx+1][0] == '?':
vega_modi += 1
else:
vega_add += 1
elif dv[0] == '+':
continue
elif dv[0] == '?':
continue
#vega_add -= 1
elif dv.strip().replace("\n", "") == '':
continue
else:
vega_same += 1
if kv[0].split(" ")[-2].strip().lower() + " " + kv[0].split(" ")[-1].strip().lower() + " " + tar.lower() not in wrong_stmt:
vega_same = total
vega_modi = 0
vega_add = 0
Mod_Result[module][2] += vega_same
if Mips.replace("\n", "") != "None":
diff_mips = list(My_Difflib.Differ().compare(Ans.splitlines(), Mips.splitlines()))
for dm in diff_mips:
if dm[0] == '-':
if idx < len(diff_mips) - 1 and diff_mips[idx+1][0] == '?':
Mips_modi += 1
else:
Mips_add += 1
elif dm[0] == '+':
continue
elif dm[0] == '?':
continue
#vega_add -= 1
elif dm.strip().replace("\n", "") == '':
continue
else:
Mips_same += 1
else:
Mips_add = total
Mod_Result[module][1] += Mips_same
with open(folder+"/Fig9.csv", 'a', encoding='utf-8', newline="") as f:
f_csv = csv.writer(f)
avg_vega = 0.0
avg_mips = 0.0
for kv in Mod_Result.items():
f_csv.writerow([tar.replace("PULP", "RI5CY"), kv[0], round(kv[1][1]*1.0 / kv[1][0], 3), round(kv[1][2]*1.0 / kv[1][0], 3)])
avg_vega += float(round(kv[1][2]*1.0 / kv[1][0], 3))
avg_mips += float(round(kv[1][1]*1.0 / kv[1][0], 3))
f_csv.writerow([tar.replace("PULP", "RI5CY"), "Avg", round(avg_mips / len(Mod_Result), 3), round(avg_vega / len(Mod_Result), 3)])
with open(folder+"/Table3.csv", 'a', encoding='utf-8', newline="") as f:
f_csv = csv.writer(f)
all_vega = 0
all_mips = 0
all_stmt = 0
for kv in Mod_Result.items():
f_csv.writerow([tar.replace("PULP", "RI5CY"), kv[0], kv[1][2], kv[1][0]-kv[1][2]])
all_vega += kv[1][2]
all_mips += kv[1][1]
all_stmt += kv[1][0]
f_csv.writerow([tar.replace("PULP", "RI5CY"), "All", all_vega, all_stmt - all_vega])
if __name__ == '__main__':
get_wrong_list()
with open(folder+"/Fig9.csv", 'w', encoding='utf-8', newline="") as f:
f_csv = csv.writer(f)
f_csv.writerow(["Target", "Module", "Fork_Acc", "VEGA_Acc"])
with open(folder+"/Table3.csv", 'w', encoding='utf-8', newline="") as f:
f_csv = csv.writer(f)
f_csv.writerow(["Target", "Module", "VEGA_Accurate_Lines", "VEGA_Manual_Lines"])
for tar in ["XCore", "PULP", "RISCV"]:
Mod_File_Func = {}
Mod_Result = {}
deal_dir(Mips_Dir+"/"+tar)
deal_dir(VEGA_Dir+"/"+tar)
deal_dir(Ans_Dir+"/"+tar)
duplicate_data(tar)