-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
135 lines (109 loc) · 4.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from subprocess import Popen, PIPE
import os
import config
# removes a file if it is present
def removeFile(file):
if file not in os.listdir(os.curdir):
return "File "+file+" wasn't present: ignoring"
out, err = Popen('rm '+file, stdout=PIPE, stderr=PIPE, shell=True).communicate()
out = out.strip()
err = err.strip()
if err != '' or out != '':
raise Exception(err+'\n'+out)
return "File "+file+" was removed"
# returns only if file is compiled successfully
# provided the exec is absent when it is called
def compileFile(file):
if file not in os.listdir(os.curdir):
err = "File "+file+" is absent, can't proceed"
raise Exception(err)
file_name = file.split('.')[0]
out, err = Popen('gcc -o '+file_name+' '+file+' -fopenmp', stdout=PIPE, stderr=PIPE, shell=True).communicate()
out = out.strip()
err = err.strip()
if file_name not in os.listdir(os.curdir):
print out
print err
err = "File "+file+" wasn't compiled"
raise Exception(err)
return "File "+file+" was compiled"
if __name__ == "__main__":
# remove the previous execs and recompile
try:
print removeFile('serial')
print removeFile('parallel')
print compileFile('serial.c')
print compileFile('parallel.c')
except Exception as e:
print e, ":exiting"
exit()
print "*"*80
reuse = None
probSizeIndx, procNumIndx, rid = None, None, None
runs = None
while type(runs) != int or runs <= 0:
try:
runs = int(raw_input('Number of runs: '))
except:
pass
print "Warning: if configs have changed, don't reuse!"
while reuse != 'n' and reuse != 'y':
reuse = raw_input('Reuse previous data? (y/n): ')
if reuse == 'n':
# don't reuse data, fresh start
print "Removing data.txt"
print removeFile('data.txt')
probSizeIndx, procNumIndx, rid = 0, 0, 0
elif reuse == 'y':
if 'data.txt' not in os.listdir(os.curdir):
print 'data.txt is absent: ignoring'
probSizeIndx, procNumIndx, rid = 0, 0, 0
else:
with open('data.txt', 'r') as f:
for line in f.readlines():
try:
n, p, r, a, e = line.split(',')
n, p, r = int(n), int(p), int(r)
probSizeIndx = config.prob.index(n)+1
procNumIndx = config.procs.index(p)
rid = r
if rid == runs:
print "data.txt has all the required runs, delete additional data"
exit()
except:
print "Configs are inconsistent with data.txt: exiting"
exit()
for runId in range(rid, runs):
print "*"*80
print "Run #:", runId+1
for p in range(procNumIndx, len(config.procs)):
print "*"*40
print "Processors:", config.procs[p]
for n in range(probSizeIndx, len(config.prob)):
print "Problem Size:", config.prob[n]
if config.procs[p] == 0:
cmd = './serial '+str(config.prob[n])+' ' + str(config.procs[p])
else:
cmd = './parallel '+str(config.prob[n])+' ' + str(config.procs[p])
process = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
out, err = process.communicate()
if process.returncode != 0:
print "Exit code:", process.returncode, "exiting"
print err.strip()
print out.strip()
exit()
else:
out = out.strip()
algName,algApp,N,P,e2eX,e2eY,algX,algY = out.split(',')
# below code is from 'text_to_csv.py from original letshpc'
algTime = float(algX) + float(algY) / 1e9
e2eTime = float(e2eX) + float(e2eY) / 1e9
###########################################################
algTime = str(algTime)
e2eTime = str(e2eTime)
dataPoint = ','.join([N,P,str(runId),algTime,e2eTime])
with open('data.txt', 'a') as f:
f.write(dataPoint+'\n')
# next run should begin at 0s
probSizeIndx = 0
procNumIndx = 0