-
Notifications
You must be signed in to change notification settings - Fork 3
/
fex.py
341 lines (292 loc) · 10.8 KB
/
fex.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
from __future__ import print_function
import logging
import os
import re
from argparse import ArgumentParser
from importlib import import_module
from subprocess import check_call, STDOUT, Popen, CalledProcessError
import coloredlogs
import cpuinfo
import platform
import config
from core.environment import Environment, set_all_environments
# config is needed everywhere
conf = config.Config()
def set_logging(verbose=False):
os.environ['COLOREDLOGS_LOG_FORMAT'] = '%(asctime)-15s %(levelname)-8s %(message)s'
coloredlogs.install(
level=logging.INFO if not verbose else logging.DEBUG,
datefmt="%m-%d %H:%M:%S"
)
logging.addLevelName(21, 'RUNNER')
logging.addLevelName(22, 'BUILDER')
logging.addLevelName(23, 'SCRIPT')
def get_arguments():
"""
Parse command line arguments
:return Namespace: parsed arguments
"""
parser = ArgumentParser(description='')
subparsers = parser.add_subparsers(help='sub-command help', dest='subparser_name')
parser.add_argument(
'-v', '--verbose',
choices=['1', '2', '3'],
required=False,
help='Verbosity level: defines how much information to show.'
'(-v 1 - [default] basic info;'
'-v 2 - not implemented;'
'-v 3 - full experiment description, including HW parameters, compilers and flags, etc.)'
)
parser.add_argument(
'-d', '--debug',
action='store_true',
required=False,
help='Debug mode: compile with debug info (but still with all optimizations enabled) and set helpful environmental variables.'
)
# parser for installing benchmarks
parser_install = subparsers.add_parser('install', help='download and install all benchmarks')
parser_install.add_argument(
'-n', '--names',
nargs='*',
help="List of program names to be installed"
)
# parser for processing logs
parser_collect = subparsers.add_parser('collect', help='collect statistics based on received results')
parser_collect.add_argument(
'-n', '--names',
nargs='*',
help='Names of experiments to get collect from'
)
parser_collect.add_argument(
'--stats',
type=str,
choices=list(conf.stats_action.keys()),
default='perf',
help='Statistics tool'
)
# parser for building plots
parser_plot = subparsers.add_parser('plot', help='build plot based on received results')
parser_plot.add_argument(
'-n', '--names',
nargs='*',
)
parser_plot.add_argument(
'-t', '--plot_type',
required=True,
help='Type of plot to build.'
)
parser_plot.add_argument(
'-f', '--file',
type=str,
default='',
help='Input csv file with results.'
)
# parser for running performance tests
parser_perf = subparsers.add_parser('run', help='Run an experiment')
parser_perf.add_argument(
'-n', '--names',
nargs='+',
help="Names of experiments to run"
)
parser_perf.add_argument(
'-r', '--num_runs',
type=str,
default='1',
help="How much times to run the experiments (results will be averaged on the collection stage)"
)
parser_perf.add_argument(
'-m', '--num_threads',
nargs='+',
default='1',
help='Maximum number of threads (multiple values possible)'
)
parser_perf.add_argument(
'-t', '--types',
required=True,
nargs='+',
help='Build type (multiple values possible)'
)
parser_perf.add_argument(
'--stats',
type=str,
choices=list(conf.stats_action.keys()),
default='perf',
help='Measurement tool'
)
parser_perf.add_argument(
'--no-build',
action='store_true',
required=False,
help='Don\'t build benchmarks (previous build is used, if any)'
)
parser_perf.add_argument(
'--no-run',
action='store_true',
required=False,
help='Don\'t run the experiments (only build)'
)
parser_perf.add_argument(
'--no-clean',
action='store_true',
required=False,
help='Don\'t delete the previous build'
)
parser_perf.add_argument(
'--singlethreaded_build',
action='store_true',
required=False,
help='Disable multithreading during the build stage'
)
parser_perf.add_argument(
'-b', '--benchmark_name',
default='',
help='Run only one benchmark from the benchmark suite'
)
parser_perf.add_argument(
'--timeout',
required=False,
help='Time limit for executing a single run'
)
parser_perf.add_argument(
'-i', '--input',
choices=['native', 'test'],
default="native",
help='Input type: native - normal run, test - fast run with small inputs'
)
args = parser.parse_args()
return args
class CLIEnvironment(Environment):
def __init__(self, cli_args, *args, **kwargs):
super(CLIEnvironment, self).__init__(*args, **kwargs)
# multithreading
if getattr(cli_args, "singlethreaded_build", ''):
self.forced_variables["BUILD_THREADS"] = '1'
else:
self.forced_variables["BUILD_THREADS"] = '8'
# execution parameters
if cli_args.subparser_name in ['run', 'collect']:
self.forced_variables.update({
'STATS_ACTION': conf.stats_action[cli_args.stats],
'STATS_COLLECT': cli_args.stats,
})
if cli_args.subparser_name == 'plot':
if cli_args.file == "" and len(cli_args.names) == 1:
cli_args.file = os.environ["DATA_PATH"] + '/results/' + cli_args.names[0] + '/raw.csv'
self.forced_variables.update({
'PLOT_TYPE': cli_args.plot_type,
'PLOT_FILE': cli_args.file,
})
if cli_args.subparser_name == 'run':
self.forced_variables.update({
'NUM_RUNS': cli_args.num_runs,
'NUM_THREADS': ' '.join(cli_args.num_threads),
'TYPES': ' '.join(cli_args.types),
'REBUILD': '' if cli_args.no_clean else '1',
'TIMEOUT': cli_args.timeout,
})
def exec_scripts(path, name_pattern):
"""
Runs scripts which match name_patten in path
:param str path:
:param str name_pattern:
"""
patten = re.compile(name_pattern)
for file in os.listdir(path):
if re.match(patten, file):
proc = Popen(path + "/" + file, stderr=STDOUT, shell=True)
out, err = proc.communicate()
return True
return False
def run_python_module(exp_name, file_name, benchmark_name=None):
try:
module = import_module("experiments.%s.%s" % (exp_name, file_name))
output = module.main(benchmark_name) if benchmark_name else module.main()
except ImportError as e:
logging.error("Probably, file experiments/%s/%s.py not found" % (exp_name, file_name))
raise e
return output
class Manager:
"""
Main management point
"""
def __init__(self, args):
logging.info("Creating a manager")
self.names = args.names
self.benchmark_name = args.benchmark_name if args.subparser_name == 'run' else ''
self.verbose = str(args.verbose) if args.verbose else ''
self.debug = str(args.debug) if args.debug else ''
if self.debug:
logging.warning("Debug mode is on. These measurements most probably will be incorrect!")
def set_configuration(self, args):
conf.input_type = getattr(args, "input", "native")
# if verbosity level is set to 3, also output all experimental parameters
if self.verbose == '3':
self.print_hw_parameters(args)
def set_environment(self, args):
cli_env = CLIEnvironment(args, self.debug, self.verbose)
cli_env.setup()
if getattr(args, "no_run", False):
set_all_environments(self.debug, self.verbose, 'build')
elif getattr(args, "no_build", False):
set_all_environments(self.debug, self.verbose, 'run')
else:
set_all_environments(self.debug, self.verbose, 'both')
def start(self, action):
"""
Do the specified action
"""
if action == 'install':
for name in self.names:
logging.info('Installing %s' % name)
check_call("mkdir -p %s/build/" % os.environ["PROJ_ROOT"], shell=True)
found = exec_scripts("install/compilers/", "%s.(sh|py)" % name)
if not found:
found = exec_scripts("install/benchmarks/", "%s.(sh|py)" % name)
if not found:
exec_scripts("install/dependencies/", "%s.(sh|py)" % name)
elif action == 'run':
for name in self.names:
logging.info('Running %s' % name)
self.run_benchmark(name)
elif action == 'collect':
for name in self.names:
run_python_module(exp_name=name, file_name='collect')
elif action == 'plot':
for name in self.names:
try:
run_python_module(exp_name=name, file_name='plot', benchmark_name=os.environ["PLOT_TYPE"])
except CalledProcessError as e:
logging.error(
"Could not build the plot (exit code = %d). Error message:" % e.returncode)
logging.error(e.output)
def run_benchmark(self, name):
# prepare the result directory
try:
os.makedirs(os.environ['DATA_PATH'] + '/results/' + name)
except os.error: # if directory already exist - ignore
pass
# run
run_python_module(exp_name=name, file_name='run', benchmark_name=self.benchmark_name)
# collect
logging.info("Collecting data")
run_python_module(exp_name=name, file_name='collect')
def print_hw_parameters(self, args):
msg = "Experiment parameters:\n"
info = cpuinfo.get_cpu_info()
msg += "CPU: {0} ({1} cores)\n".format(info['brand'], info['count']) + \
"Architecture: {0}\n".format(platform.machine()) +\
"L2 size: {0}\n".format(info['l2_cache_size']) +\
"Platform: {0}\n\n".format(platform.platform()) +\
"Environment variables:\n{0}\n\n".format(os.environ) +\
"Command line arguments:\n{0}\n\n".format(args.__dict__)
logging.info(msg)
def main():
args = get_arguments()
set_logging(args.verbose)
manager = Manager(args)
manager.set_environment(args)
manager.set_configuration(args)
manager.start(action=args.subparser_name)
if __name__ == '__main__':
main()