-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_algo.py
156 lines (130 loc) · 4.67 KB
/
exec_algo.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from subprocess import STDOUT, check_output
import os
import re
from test_nrj import csv_to_energy
def execute(bash_command, timeout, output_value="flops"):
try:
output = check_output(bash_command, timeout=timeout, shell=True)
output = output.decode("UTF-8")
if output_value == "flops":
result = re.search("flops:(.*) GFlops", output)
elif output_value == "points":
result = re.seatch("flops:(.*) MPoints/s", output)
result = result.group(1)
result = float(result.strip())
time = re.search('time:(.*) sec', output)
tmp = time.group(1)
time = float(tmp.strip())
# except subprocess.TimeoutExpired:
except Exception as e:
print("No need to explore this path: ", e)
result = -99
time = timeout
return result,time
def command(options, output_value="flops"):
command = (
options["filename"]
+ " "
+ options["size1"]
+ " "
+ options["size2"]
+ " "
+ options["size3"]
+ " "
+ options["num_thread"]
+ " 100"
+ " "
+ options["dim1"]
+ " "
+ options["dim2"]
+ " "
+ options["dim3"]
)
#command = command + " | grep " + output_value
return command
def command_nrj(options):
command1 = (
"/opt/cpu_monitor/cpu_monitor.x --csv --quiet --redirect -- "
+ options["filename"]
+ " "
+ options["size1"]
+ " "
+ options["size2"]
+ " "
+ options["size3"]
+ " "
+ options["num_thread"]
+ " 100"
+ " "
+ options["dim1"]
+ " "
+ options["dim2"]
+ " "
+ options["dim3"]
)
return command1
def execute_nrj(bash_command, timeout):
try:
output = check_output(bash_command, timeout=timeout, shell=True)
parse_output = "python test_nrj.py -f $(ls -t1 ./*.csv | head -n1) | grep all"
output = check_output(parse_output, timeout=timeout, shell=True)
output = output.decode("UTF-8")
result = re.search("all energy: (.*)", output)
output = result.group(1)
output = float(output.strip())
# except subprocess.TimeoutExpired:
except Exception as e:
print("#####No need to explore this path: ", e)
output = 1000
return 1/(output+1)
def mixed_exec(options, timeout, alpha, output_value="flops"):
bash_perf = command(options)
bash_nrj = command_nrj(options)
perf = execute(bash_perf, timeout, output_value)
nrj = execute_nrj(bash_nrj, timeout)
return alpha * perf + (1 - alpha) * nrj
def execute_all(bash_command, timeout):
try:
_ = check_output(bash_command, timeout=timeout, shell=True)
parse_output = "python test_nrj.py -f $(ls -t1 ./*.csv | head -n1) | grep all"
output = check_output(parse_output, timeout=timeout, shell=True)
output = output.decode("UTF-8")
result = re.search("all energy: (.*)", output)
output = result.group(1)
output = float(output.strip())
# except subprocess.TimeoutExpired:
except Exception as e:
print("#####No need to explore this path: ", e)
output = -99
return output
def mixed(options, timeout, alpha, output_value="GFlops"):
bash_perf = command(options, output_value)
# Create a new file with the command
with open("my_script.sh", "w") as f:
f.write("#!/bin/bash\n")
f.write('../'+bash_perf+' > result.txt')
# Make the file executable
os.chmod("my_script.sh", 0o755)
bash_command = 'cd results && /opt/cpu_monitor/cpu_monitor.x --csv --quiet --redirect -- ../my_script.sh && cd ..'
try:
_ = check_output(bash_command, timeout=timeout, shell=True)
parse_output = "python test_nrj.py -f $(ls -t1 results/*.csv | head -n1) | grep all"
output = check_output(parse_output, timeout=timeout, shell=True)
output = output.decode("UTF-8")
result = re.search("all energy: (.*)", output)
output = result.group(1)
output = float(output.strip())
with open('results/result.txt', 'r') as f:
for line in f:
if 'GFlops' in line:
score = line.rstrip('\n')
score = re.search(f"flops:(.*) {output_value}", score)
score = score.group(1)
score = float(score.strip())
output = alpha*(score/100) + (1-alpha)/(output*100)
# except subprocess.TimeoutExpired:
except Exception as e:
print("#####No need to explore this path: ", e)
output = -99
return output
# execute('cat test.txt | grep flops', 10)