avoiding repetitive tuner
[satune.git] / src / analyzer / tunerloganalyzer.py
1 import re
2 import argparse
3 import sys
4 import os
5
6
7 class AutoTunerArgParser:
8         def __init__(self):
9                 self.parser = argparse.ArgumentParser(description='Parsing the output log of the CSolver auto tuner ...')
10                 self.parser.add_argument('--folder', '-f', metavar='bin', type=str, nargs=1,help='output log of running the autotuner ...')
11                 self.parser.add_argument('--number', '-n', metavar='122', type=int, nargs=1,help='Number of runs ...')
12                 self.args = self.parser.parse_args()
13
14         def getFolder(self):
15                 return self.args.folder[0]
16
17         def getRunNumber(self):
18                 return self.args.number[0]
19
20 TUNABLEHEADER = ["DECOMPOSEORDER", "MUSTREACHGLOBAL", "MUSTREACHLOCAL", "MUSTREACHPRUNE", "OPTIMIZEORDERSTRUCTURE",
21                 "ORDERINTEGERENCODING", "PREPROCESS", "NODEENCODING", "EDGEENCODING", "MUSTEDGEPRUNE", "ELEMENTOPT",
22                 "ENCODINGGRAPHOPT", "ELEMENTOPTSETS", "PROXYVARIABLE", "MUSTVALUE", "NAIVEENCODER", "VARIABLEORDER",
23                 "PROBLEM","SATTIME", "EXECTIME","TUNERNUMBER"]
24
25 configs = {"EXECTIME": "-",
26                 "SATTIME":"-",
27                 "TESTCASE":"-",
28                 "PREPROCESS" : "-",
29                 "ELEMENTOPT" : "-",
30                 "ELEMENTOPTSETS" : "-",
31                 "PROXYVARIABLE" : "-",
32                 "#SubGraph" : "-",
33                 "NODEENCODING" : "-",
34                 "EDGEENCODING" : "-",
35                 "NAIVEENCODER" :"-",
36                 "ENCODINGGRAPHOPT" : "-"
37                 }
38
39 REGEXES = {"EXECTIME": "CSOLVER solve time: (.*)",
40                 "SATTIME":"SAT Solving time: (.*)",
41                 "TESTCASE": "deserializing (.+) ...",
42                 "PREPROCESS" : "Param PREPROCESS = (.*)range=\[0,1\]",
43                 "ELEMENTOPT" : "Param ELEMENTOPT = (.*)range=\[0,1\]",
44                 "ELEMENTOPTSETS" : "Param ELEMENTOPTSETS = (.*)range=\[0,1\]",
45                 "PROXYVARIABLE" : "Param PROXYVARIABLE = (.*)range=\[1,5\]",
46                 "#SubGraph" : "#SubGraph = (.*)",
47                 "NODEENCODING" : "Param NODEENCODING = (.*)range=\[0,3\](.*)",
48                 "EDGEENCODING" : "Param EDGEENCODING = (.*)range=\[0,2\](.*)",
49                 "NAIVEENCODER" : "Param NAIVEENCODER = (.*)range=\[1,3\](.*)",
50                 "ENCODINGGRAPHOPT" : "Param ENCODINGGRAPHOPT = (.*)range=\[0,1\]"
51                 }
52
53 def printHeader(file):
54         global TUNABLEHEADER
55         mystr=""
56         for header in TUNABLEHEADER:
57                  mystr+=str(header)+","
58         print >>file, mystr
59
60 def dump(file, row):
61         global TUNABLEHEADER
62         mystr=""
63         for i in range(len(TUNABLEHEADER)):
64                 mystr += row[TUNABLEHEADER[i]]+ ","
65         print "mystr is:"+ mystr
66         print >>file, mystr
67
68 def loadTunerInfo(row, filename):
69         with open(filename) as f:
70                 for line in f:
71                         numbers = re.findall('\d+',line)
72                         numbers = map(int,numbers)
73                         row[TUNABLEHEADER[numbers[3]]] = row[TUNABLEHEADER[numbers[3]]] + str(numbers[7])
74
75 def loadSolverTime(row, filename):
76         global REGEXES
77         global configs
78         with open(filename) as f:
79                 for line in f:
80                         for regex in REGEXES:
81                                 p = re.compile(REGEXES[regex])
82                                 token = p.search(line)
83                                 if token is not None:
84                                         if regex == "TESTCASE":
85                                                 configs[regex] = re.search(REGEXES[regex], line).group(1)
86                                         else:
87                                                 configs[regex] = re.findall("\d+\.?\d*", line)[0]
88         row["SATTIME"] = configs["SATTIME"]
89         row["EXECTIME"] = configs["EXECTIME"]
90
91 def loadProblemName(row,filename):
92         with open(filename) as f:
93                 row["PROBLEM"] = f.readline().replace("\n","")
94 def loadTunerNumber(row, filename):
95         with open(filename) as f:
96                 row["TUNERNUMBER"] = f.readline().replace("\n","")
97 def analyzeLogs(file):
98         global configs
99         argprocess = AutoTunerArgParser()
100         printHeader(file)
101         rows = []
102         for i in range(argprocess.getRunNumber()):
103                 row = {"DECOMPOSEORDER" : "",
104                         "MUSTREACHGLOBAL" : "",
105                         "MUSTREACHLOCAL" : "",
106                         "MUSTREACHPRUNE" : "", 
107                         "OPTIMIZEORDERSTRUCTURE" : "",
108                         "ORDERINTEGERENCODING" : "",
109                         "PREPROCESS" : "",
110                         "NODEENCODING" : "",
111                         "EDGEENCODING" : "",
112                         "MUSTEDGEPRUNE" : "",
113                         "ELEMENTOPT" : "",
114                         "ENCODINGGRAPHOPT" : "", 
115                         "ELEMENTOPTSETS" : "", 
116                         "PROXYVARIABLE" : "", 
117                         "MUSTVALUE" : "", 
118                         "NAIVEENCODER" : "", 
119                         "VARIABLEORDER" : "",
120                         "PROBLEM":"",
121                         "SATTIME":"",
122                         "EXECTIME": "",
123                         "TUNERNUMBER":""
124                 }
125                 loadTunerNumber(row, argprocess.getFolder() + "/tunernum" + str(i))
126                 loadTunerInfo(row, argprocess.getFolder()+"/tuner"+str(i)+"used")
127                 loadSolverTime(row, argprocess.getFolder()+"/log"+str(i))
128                 loadProblemName(row, argprocess.getFolder()+"/problem"+str(i))
129                 dump(file, row)
130                 rows.append(row)
131         return rows
132
133 def tunerNumberAnalysis(file, rows):
134         global TUNABLEHEADER
135         tunercount = {}
136         tunernumber = {}
137         for row in rows:
138                 mystr=""
139                 for i in range(18):
140                         mystr+=row[TUNABLEHEADER[i]]
141                 if mystr not in tunercount:
142                         tunercount.update({mystr : 1})
143                         tunernumber.update({mystr : str(row["TUNERNUMBER"])})
144                 else :
145                         tunercount[mystr] += 1
146                         tunernumber[mystr] += "-" + str(row["TUNERNUMBER"])
147
148         problems = set(map(lambda x: x["PROBLEM"], rows))
149         print "Number of repititive tuners"
150         for key in tunercount:
151                 if tunercount[key] > 1:
152                         print key + "(ids:" + tunernumber[key]  + ") = #" + str(tunercount[key])
153
154
155 def main():
156         file = open("tuner.csv", "w")
157         rows = analyzeLogs(file)
158         tunerNumberAnalysis(file, rows)
159         file.close()
160         return
161
162 if __name__ == "__main__":
163         main()