-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_benchmark.py
321 lines (257 loc) · 12 KB
/
run_benchmark.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python3
from pynrp.virtual_coach import VirtualCoach
from datetime import datetime
import ruamel.yaml as yaml
import subprocess
import requests
import logging
import signal
import time
import sys
import os
import math
import shutil
import helpers
import ast
class BenchmarkRunner:
def __init__(self, rundir):
self.nodelist = helpers.expand_nodelist()
logger.info("Nodes in allocation: %s", self.nodelist)
self.jobstep = -1
self.running = False
self.working_dir = os.getcwd()
self.rundir = rundir
def start_nest(self, ntasks):
"""
Starts the NEST container with the given number of tasks,
two tasks on each node.
:param ntasks: Number of NEST tasks to run in total
"""
values = {
'n_nodes_nest': math.ceil(float(ntasks)/2),
'n_tasks_nest': ntasks,
'nodezero': self.nodelist[0],
'jobid': os.environ.get("SLURM_JOB_ID"),
'account': os.environ.get("SLURM_JOB_ACCOUNT"),
}
values['working_dir'] = os.getcwd()
logger.info("Starting NEST")
logger.info(" NEST tasks : %s", ntasks)
with open(self.working_dir + "/misc/nest.sh.tpl", 'r') as infile:
with open(f'{self.ntasks_rundir}/nest.sh', 'w') as outfile:
outfile.write(f"{infile.read()}".format(**values))
subprocess.Popen(["bash", f"{self.ntasks_rundir}/nest.sh"])
self.jobstep += 1
time.sleep(180) # Give the NEST container some time to start
def stop_nest(self):
"""
Stops NEST on all nodes and retrieves info data
"""
job_step_id = f"{os.environ.get('SLURM_JOB_ID')}.{self.jobstep}"
logger.info("Canceling NEST: %s", job_step_id)
logger.info(" Obtaining metadata from NEST")
nest_info = self.get_nest_info()
time.sleep(10)
subprocess.call(["scancel", job_step_id])
logger.info(" Called scancel, sleeping 400 seconds")
time.sleep(400) # Wait for the job to die
with open(f'{self.ntasks_rundir}/metadata.yaml', 'w') as outfile:
logger.info(" Obtaining metadata from sacct")
run_info = self.get_sacct_info(job_step_id)
run_info.update(nest_info)
outfile.write(yaml.dump(run_info, default_flow_style=False))
logger.info(" Cancelling complete")
def get_sacct_info(self, job_step_id):
"""
Collects benchmark relevant data from sacct
:param job_step_id: ID of the current job
"""
fmt = "--format=Elapsed,AveRSS,MaxRSS,ConsumedEnergy"
sacct_cmd = ["sacct", "-j", job_step_id, "-p", "--noheader", fmt]
output = subprocess.check_output(sacct_cmd)
output = output.decode("utf-8").split("|")[:-1]
elapsed = datetime.strptime(output[0], "%H:%M:%S") - datetime(1900, 1, 1)
return {
"sacct_elapsed": elapsed.total_seconds(),
"sacct_averss": float(output[1].replace("K", "").replace("M", "000")),
"sacct_maxrss": float(output[2].replace("K", "").replace("M", "000")),
"sacct_consumedenergy": float(output[3].replace("K", "").replace("M", "000"))
}
def get_nest_info(self):
"""
Collects benchmark relevant info from NEST
"""
url = f"http://{self.nodelist[1]}:5000"
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post(f'{url}/api/GetKernelStatus', json={}, headers=headers)
return {
"nest_time_create": response.json()["time_construction_create"],
"nest_time_connect": response.json()["time_construction_connect"],
"nest_time_last_simulate": response.json()["time_simulate"],
"nest_num_nodes": response.json()["network_size"],
"nest_num_connections": response.json()["num_connections"],
"nest_num_processes": response.json()["num_processes"],
"nest_local_num_threads": response.json()["local_num_threads"],
"nest_time_simulated": response.json()["biological_time"],
}
def run(self):
"""
Runs the benchmark
"""
if 'n_tasks' not in config:
n_nodes_max = int(os.environ.get("SLURM_NNODES"))
n_tasks = [2**x for x in range(22) if 2**x < n_nodes_max*2]
else:
n_tasks = config['n_tasks']
for n in n_tasks:
logger.info(f"Running {config['testcase']} benchmark step with {n} NEST tasks")
self.ntasks_rundir = f"{self.rundir}/{n:02d}_ntasks"
os.makedirs(self.ntasks_rundir)
self.start_nest(n)
getattr(self, f"run_{config['testcase']}")(n)
self.stop_nest()
logger.info("Benchmarks done!")
def run_nrp_benchmark(self, experiment_path):
"""
Runs a benchmark experiment in the NRP
:param experiment_path: Experiment path of the experiment that shall be
imported and run.
"""
vc = VirtualCoach(
f"http://{config['nrp_frontend_ip']}",
oidc_username=secrets['hbp_username'],
oidc_password=secrets['hbp_password'],
)
# Import Experiment
self.running = True
response_result = vc.import_experiment(experiment_path)
dict_content = ast.literal_eval(response_result.content.decode("UTF-8"))
self.experiment = dict_content['destFolderName']
vc.print_cloned_experiments()
time.sleep(30)
# Launch Experiment
self.tic = time.time()
self.sim = vc.launch_experiment(self.experiment, server='148.187.148.198-port-8080', profiler='cle_step')
self.sim.register_status_callback(self.stop_cb)
self.sim.start()
while self.running:
time.sleep(0.5)
time.sleep(20)
self.retrieve_nrp_profiler_data(self.experiment)
# Delete Experiment after run
vc.delete_cloned_experiment(self.experiment)
def stop_cb(self, status):
"""
NRP callback of the VirtualCoach as a helper executed when experiment
status changes.
:param status: New status of the experiment
"""
if status['state'] == 'stopped' or status['state'] == 'halted':
with open(f'{self.ntasks_rundir}/total_time.dat', "w+") as logfile:
logfile.write(str(time.time() - self.tic))
self.running = False
def retrieve_nrp_profiler_data(self, experiment_id):
"""
Retrieves the experiment profiler data after a benchmark run.
:param experiment_id: Experiment ID that data shall be retrieved from.
"""
vc = VirtualCoach(
f"http://{config['nrp_frontend_ip']}",
oidc_username=secrets['hbp_username'],
oidc_password=secrets['hbp_password'],
)
file_path = os.path.join(self.ntasks_rundir,"cle_time_profile_0.csv")
logger.info("Saving CLE profiler data from experiment: {} to {}".format(experiment_id, file_path))
cle_step_data = vc.get_last_run_file(experiment_id, 'profiler', 'cle_time_profile_0.csv')
with open(file_path, "wb") as f_data:
f_data.write(cle_step_data)
# vc.print_experiment_run_files('experiment_name_id', 'profiler', 0)
# TODO: Store profiler data in f'{self.ntasks_rundir}' and delete the experiment
# vc.get_last_run_file('experiment_name_id', 'profiler', 'cle_time_profile.csv')
# vc.delete_cloned_experiment(self.experiment)
# self.experiment = None
# vc.print_experiment_runs_files('experiment_name_id', 'profiler')
# vc.get_experiment_run_file('experiment_name_id', 'profiler', 0, 'cle_time_profile.csv')
# vc.print_last_run_files('experiment_name_id', 'profiler')
# vc.get_last_run_file('experiment_name_id', 'profiler', 'cle_time_profile.csv')
def run_hpcbench_baseline(self, ntasks):
"""
Baseline benchmark using only NEST Server and no NRP.
This variant of the benchmark uses the same brain simulation script as
the HPC benchmarks below, but instead of running the brain simulation
by means of an experiment in the NRP, it sends the script directly to
NEST Server.
We consider this the baseline benchmark to compare all other runs of
the HPC benchmarks to.
:param ntasks: NEST tasks to run the benchmark on
"""
simtime = 20.0
logger.info(f'Running hpcbench_base with {ntasks} processes, 2 per node')
url = f"http://{self.nodelist[1]}:5000"
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
requests.post(f'{url}/api/ResetKernel', json={}, headers=headers)
tic = time.time()
data = {'source': open('Experiments/HPC_benchmark/0_hpcbench_baseline.py').read()}
requests.post(f'{url}/exec', json=data, headers=headers)
with open(f'{self.ntasks_rundir}/exec_time.dat', "w") as logfile:
logfile.write(str(time.time() - tic))
sim_times = []
data = {'t': simtime}
for cycle in range(config['n_cycles_nest']):
tic = time.time()
requests.post(f'{url}/api/Simulate', json=data, headers=headers)
sim_times.append(time.time() - tic)
with open(f'{self.ntasks_rundir}/step_time.dat', "w") as logfile:
d
logfile.write(f'brainstep\n')
for t in sim_times:
logfile.write(f"{t}\n")
with open(f'{self.ntasks_rundir}/total_time.dat', "w") as logfile:
logfile.write(str(sum(sim_times)))
logger.info(f' Done after t={sum(sim_times)}')
def run_hpcbench_notf(self, ntasks):
"""
HPC benchmark via NRP without transfer functions.
:param ntasks: NEST tasks to run the benchmark on
"""
logger.info(f"Running NRP - HPC benchmark without TF with {ntasks} processes, 2 per node")
experiment_path = os.path.join(self.working_dir, "Experiments/HPC_benchmark/1_nrpexperiment_scale20-nvp1152-withoutTF")
self.run_nrp_benchmark(experiment_path)
def run_hpcbench_readspikes(self, ntasks):
"""
HPC benchmark via NRP with simple transfer function.
The transfer function records and reads spikes from a certain
subpopulation of the model. To have some load on the NRP side, we use the
husky robot as body model.
:param ntasks: NEST tasks to run the benchmark on
"""
logger.info(f"Running NRP - HPC benchmark with TF with {ntasks} processes, 2 per node")
experiment_path = os.path.join(self.working_dir, "Experiments/HPC_benchmark/2_nrpexperiment_scale20-nvp1152-withTF")
self.run_nrp_benchmark(experiment_path)
def run_robobrain(self, ntasks):
"""
RoboBrain benchmark experiment in the NRP consising of a musculoskeletal
rodent model with 8 muscles and a brain model with 1Million+ Neurons.
"""
logger.info(f"Running NRP - RoboBrain benchmark with TF with {ntasks} processes, 2 per node")
# remove old log files
logger.info(f"RoboBrain: Removing log folder")
robobrain_log_path = os.path.join(self.working_dir, "Experiments/RoboBrain_benchmark/1_nrpexperiment_robobrain_mouse/resources/log")
shutil.rmtree(robobrain_log_path)
logger.info(f"RoboBrain: Recreating log folder")
os.mkdir(robobrain_log_path)
experiment_path = os.path.join(self.working_dir, "Experiments/RoboBrain_benchmark/1_nrpexperiment_robobrain_mouse")
self.run_nrp_benchmark(experiment_path)
pass
if __name__ == '__main__':
if len(sys.argv) != 2:
"Usage: run_benchmarks.py <configfile>"
FORMAT = '[%(asctime)-15s - %(user)-8s] %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger('BenchmarkRunner')
config = helpers.get_config(sys.argv[1])
rundir = sys.argv[2]
secrets = helpers.get_secrets()
logger.info('BF in here')
runner = BenchmarkRunner(rundir)
runner.run()