forked from ARM-software/ComputeLibrary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tempschedv3.py
209 lines (197 loc) · 6.99 KB
/
tempschedv3.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import os
import time
import subprocess
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import json
import argparse
from matplotlib.lines import Line2D
from os import environ
from itertools import combinations
pp = "./build/release/examples/"
graph = "graph_resnet50"
cp = ["--target=NEON", "--threads=4"]
gp = ["--target=CL"]
sm = ["taskset", "-c", "0-3"]
bg = ["taskset", "-c", "4-7"]
targets = {
"cpu_big": lambda g: bg + [pp + g] + cp,
"cpu_small": lambda g: sm + [pp + g] + cp,
"gpu": lambda g: [pp + g] + gp
}
T = 5
dt = 0.1
sdt = 0.5
TL = 80000
N = 50
def get_temp():
with open('/sys/class/thermal/thermal_zone0/temp') as f:
temp = int(f.readline())
return temp
def profile_temp():
env = dict(os.environ)
env['LD_LIBRARY_PATH'] = './build/release'
cmds = list((target, targets[target](graph)) for target in targets)
cmd_combis = []
for i in range(1, len(cmds) + 1):
cmd_combis += list(combinations(cmds, i))
samples = dict()
for _ in range(10):
for cmd_combi in cmd_combis:
time.sleep(T)
running = []
for target_cmd in cmd_combi:
running.append(subprocess.Popen(target_cmd[1], env=env))
y = []
while None in (p.poll() for p in running):
y.append(get_temp())
time.sleep(dt)
y.append(get_temp())
max_dt = max(y) - y[0]
combi_targets = [x[0] for x in cmd_combi]
key = '_'.join(combi_targets)
if key not in samples:
samples[key] = []
samples[key].append(max_dt)
print(key, max_dt)
with open('tempschedv3_samples.json', 'w') as f:
json.dump(samples, f)
def profile_time():
env = dict(os.environ)
env['LD_LIBRARY_PATH'] = './build/release'
samples = dict()
for _ in range(10):
for target in targets:
time.sleep(T)
start = time.time()
subprocess.Popen(targets[target](graph), env=env)
end = time.time()
if target not in samples:
samples[target] = []
samples[target].append(end - start)
print(target, end-start)
with open('tempschedv3_samples2.json', 'w') as f:
json.dump(samples, f)
def plot_temp(xs, ys, xs2, ys2):
fig = plt.figure()
# Plot Lines
for x, y in zip(xs, ys):
plt.plot(x, y, color='r')
for x2, y2 in zip(xs2, ys2):
plt.plot(x2, y2, color='y')
# Plot Threshold
plt.axhline(TL, linestyle='--', color='r')
# Plot Labels
plt.xlabel('Time')
plt.ylabel('Temperature')
plt.title('Execution ' + graph)
# Plot Legend
custom_lines = [
Line2D([0], [0], color='r', linestyle='--'),
Line2D([0], [0], color='r'),
Line2D([0], [0], color='y')]
plt.legend(custom_lines, ['Threshold', 'Executing', 'Sleeping'])
# Show Plot
plt.savefig('tempsched_plots/schedv3.png')
def run_sched():
env = dict(os.environ)
env['LD_LIBRARY_PATH'] = './build/release'
cmds = list(targets[target](graph) for target in targets)
xs = []
ys = []
xs2 = []
ys2 = []
t=0
n=0
ps = dict()
with open('tempschedv3_samples.json') as f:
temp_samples = json.load(f)
with open('tempschedv3_samples2.json') as f:
time_samples = json.load(f)
for target in time_samples:
time_samples[target] = sum(time_samples[target]) / len(time_samples[target])
for target_combi in temp_samples:
temp_samples[target_combi] = sum(temp_samples[target_combi]) / len(temp_samples[target_combi])
min_dT = min(temp_samples.values())
# sleep to cool down
print('Sleeping 5 sec')
time.sleep(T)
while n < N:
if get_temp() + min_dT > TL:
x2 = []
y2 = []
for _ in range(int(sdt/dt)):
x2.append(t)
y2.append(get_temp())
time.sleep(dt)
t+=dt
x2.append(t)
y2.append(get_temp())
xs2.append(x2)
ys2.append(y2)
else:
x = []
y = []
running = dict()
for target in targets:
running[target] = False
while get_temp() + min_dT < TL and n < N:
dT = TL - get_temp() # temperature difference
min_t = 99999999 # time taken
min_combi = "none"
for target_combi in temp_samples:
valid = True
# check temperature difference
if temp_samples[target_combi] < dT:
# measure sum of execution times of individual targets
tmes = 0
for target in targets:
# check if combination also includes running targets
if running[target] and target not in target_combi:
valid=False
break
# we can add running target's sample vaue but it will be constant across comparison
if target in target_combi:
tmes += time_samples[target]
if not valid:
continue
# take maximum performance
if tmes < min_t:
min_t = tmes
min_combi = target_combi
for target, cmd in zip(targets, cmds):
if not target in ps:
ps[target] = None
# if empty process or not running
if ps[target] is None or ps[target].poll() is not None:
if running[target] and ps[target] is not None:
running[target] = False
n+=1
print('Completed', target, n)
if n > N:
break
elif target in min_combi:
running[target] = True
ps[target] = subprocess.Popen(cmd, env=env)
x.append(t)
y.append(get_temp())
time.sleep(dt)
t+=dt
x.append(t)
y.append(get_temp())
xs.append(x)
ys.append(y)
plot_temp(xs, ys, xs2, ys2)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--profile-temp', default=False, help="Profile Temperature")
parser.add_argument('--profile-time', default=False, help="Profile Time")
parser.add_argument('--run-sched', default=False, help="Run Scheduler")
args = parser.parse_args()
if args.profile_temp:
profile_temp()
if args.profile_time:
profile_time()
if args.run_sched:
run_sched()