forked from cerati/p2r-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
executable file
·324 lines (281 loc) · 11.5 KB
/
build.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
#!/usr/bin/env python3
import re
import os
import sys
import glob
import json
import argparse
import itertools
import subprocess
import collections
prefix = "propagate-tor-test"
#prefix = "propagate-toz-test"
technologies = {
"tbb": {
"threads":["icc","gcc"],
},
"cuda_v2":{
"cuda":['nvcc']
},
"cuda_v3":{
"cuda":['nvcc']
},
"cuda_v4":{
"cuda":['nvcc']
},
"cuda":{
"cuda":['nvcc']
},
"acc.v3":{
"cuda":['nvc++'],
"cpu":['nvc++']
},
"pstl":{
"cpu":['gcc'], # add other compilers
'cuda': ['nvc++','nvc++_x86']
},
"hip":{
"hip":['hipcc'],
}
#"kokkos": {
# "serial": ["icc", "gcc"],
# "threads": ["icc", "gcc"],
# "cuda": ["nvcc"],
# "hip": ["hipcc"]
#}
}
cmds ={
#"tbb":{"threads":["srun","-n","1",'-c','40',"numactl", "--cpunodebind=1"]},
"tbb":{"threads":[]},
#"cuda":{"cuda":["srun","-n","1","-c","80","--exclusive","numactl","--cpunodebind=0"]}
"cuda":{"cuda":["srun","-n","1"]},
#"cuda_v2":{"cuda":["srun","-n","1","-c","80"]}
"cuda_v2":{"cuda":["srun","-n","1"]},
"cuda_v3":{"cuda":["srun","-n","1"]},
"cuda_v4":{"cuda":["srun","-n","1"]},
"acc.v3":{"cuda":["srun","-n","1"],
"cpu":["srun","-n","1"]},
"pstl":{"cuda":["srun","-n","1"],
"cpu":["srun","-n","1"]},
"cuda_v4":{"cuda":["srun","-n","1"]},
"hip":{"hip":[]},
}
# with default values
scanParameters = [
("ntrks", 8192),
("nevts", 100),
("NITER", 5),
("bsize", 32),
("nlayer", 20),
("nthreads", 1),
("num_streams", 1),
#("threadsperblock", 1000),
("threadsperblockx", 32),
#("threadsperblocky", 16),
#("blockspergrid", 40)
]
ScanPoint = collections.namedtuple("ScanPoint", [x[0] for x in scanParameters])
result_re = re.compile("done ntracks=(?P<ntracks>\d+) tot time=(?P<time>\S+) ")
def compilationCommand(compiler, technology, backend, target, source, scanPoint,opts):
cmd = []
if compiler == "gcc":
if technology=="pstl":
cmd.extend(["g++", "-Wall", "-Isrc", "-O3", "-fopenmp", "-march=native", "-std=c++17","-mavx512f",'-lm',"-lgomp","-ltbb"])
else:
cmd.extend(["g++", "-Wall", "-Isrc", "-O3", "-fopenmp", "-march=native", "-std=c++17","-ffast-math"])
if compiler == "icc":
cmd.extend(["icc", "-Wall", "-Isrc", "-O3", "-fopenmp", "-march=native",'-xHost','-qopt-zmm-usage=high'])
if compiler == "nvcc":
cmd.extend(["nvcc",'-arch=sm_70',"-Iinclude","-std=c++17",'-maxrregcount=64','-g','-lineinfo'])
if compiler == "nvc++":
cmd.extend(["nvc++","-Iinclude","-O2","-std=c++17","-stdpar=gpu","-gpu=cc70","-gpu=managed","-gpu=fma","-gpu=fastmath","-gpu=autocollapse","-gpu=loadcache:L1","-gpu=unroll"])
if compiler == "nvc++_x86":
cmd.extend(["nvc++","-Iinclude","-O2","-std=c++17","-stdpar=multicore"])
if technology=="acc.v3":
if backend=="cuda":
cmd.extend(["nvc++","-Iinclude","-std=c++17",'-acc','-fast','-gpu=fastmath','-gpu=cc70',"-DNUM_WORKERS=8","-Msafeptr"])
elif backend=="cpu":
cmd.extend(["nvc++","-Iinclude","-std=c++17",'-acc=multicore'
,'-fast','-Mfprelaxed',"-Msafeptr",'-Mvect=simd:256',"-DNUM_WORKERS=1"])
if compiler == "hipcc":
cmd.extend(["hipcc","-Iinclude","-std=c++17"])
cmd.extend(["-o", target, source])
if technology == "tbb" :
cmd.append("-ltbb")
cmd.extend(["-D{}={}".format(name, getattr(scanPoint, name)) for name in ScanPoint._fields])
if opts.noH2D:
cmd.extend(["-DEXCLUDE_H2D_TRANSFER"])
if opts.noD2H:
cmd.extend(["-DEXCLUDE_D2H_TRANSFER"])
return cmd
######
class ExeException(Exception):
def __init__(self, code):
super(ExeException, self).__init__()
self._code = code
def errorCode(self):
return self._code
def scanProduct(opts):
return itertools.product(*[getattr(opts, x[0]) for x in scanParameters])
def execute(command, verbose):
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
try:
(out, err) = p.communicate()
except KeyboardInterrupt:
try:
p.terminate()
except OSError:
pass
(out, err) = p.communicate()
if p.returncode != 0:
if not verbose:
print(" ".join(command))
print(out)
raise ExeException(p.returncode)
return out
def build(opts, src, compiler, technology, backend, scanPoint):
target = "{}_{}_{}_{}".format(prefix, technology,backend, compiler)
print("Building {} for {} with {} for {} as {}".format(src, technology, compiler, scanPoint, target))
cmd = compilationCommand(compiler, technology,backend, target, src, scanPoint, opts)
if opts.verbose or opts.dryRun:
print(" ".join(cmd))
if not opts.dryRun:
execute(cmd, opts.verbose)
return target
def throughput(log):
for line in log.split("\n"):
m = result_re.search(line)
if m:
return int(m.group("ntracks"))/float(m.group("time"))
raise Exception("No result in output")
def sysinfo(opts):
cmd = ["perl","/global/homes/k/kkwok/PPS/PPSwork/misc/sysinfo.pl"]
out = execute(cmd,opts.verbose)
out = out.replace('"','').strip().split("\n")
return out
def run(opts, exe, tech, backend, scanPoint):
print("Running {} for {}".format(exe, scanPoint))
#cmd = ["./"+exe]
cmd_prefix = cmds[tech][backend]
cmd = cmd_prefix+["./"+exe] if len(cmd_prefix)>0 else ["./"+exe]
if opts.verbose or opts.dryRun:
print(" ".join(cmd))
if opts.dryRun:
return
result = {}
for name in scanPoint._fields:
result.update({name:float(getattr(scanPoint,name))})
out = execute(cmd, opts.verbose)
if opts.verbose:
for line in out.split("\n"): print(line)
try:
result['throughput']=throughput(out)
return result
except Exception as e:
print("Caught exception, printout of the program", " ".join(cmd))
print(out)
raise
def main(opts):
fname_re = re.compile(prefix+"_(?P<tech>.*)\.(cpp|cu)")
sources = sorted(glob.glob("src/*.cu")+glob.glob("src/*.cpp"))
for source in sources:
m = fname_re.search(source)
if not m:
# raise Exception("Source file name {} does not follow the expected pattern".format(source))
continue
tech = m.group("tech")
print(source)
if len(opts.technologies) > 0 and tech not in opts.technologies:
print("Skipping", tech)
continue
backends = technologies[tech]
for backend,compilers in backends.items():
if len(opts.backends) > 0 and backend not in opts.backends:
print("Skipping", backend)
continue
for comp in compilers:
if len(opts.compilers) > 0 and comp not in opts.compilers:
print("Skipping", comp)
continue
data = dict(
backend=backend,
compiler=comp,
results=[]
)
#data["sysinfo"] = sysinfo(opts)
print(comp,backend)
outputJson = "result_{}.json".format("_".join(filter(None,[tech,backend,comp,opts.output])))
alreadyExists = set()
if not opts.overwrite and os.path.exists(outputJson):
with open(outputJson) as inp:
data = json.load(inp)
if not opts.append:
for res in data["results"]:
alreadyExists.add( tuple([res[k] for k in sorted(ScanPoint._fields) ]) )
for p in scanProduct(opts):
scanPoint = ScanPoint(*p)
scanPoint_tuple = tuple([getattr(scanPoint,name) for name in sorted(ScanPoint._fields)])
if scanPoint_tuple in alreadyExists and not opts.dryRun:
print('Alread found this point in result, skipping:', scanPoint_tuple)
continue
try:
exe = build(opts, source, comp, tech,backend, scanPoint)
except ExeException as e:
return e.errorCode()
if opts.build:
continue
try:
result = run(opts, exe, tech, backend, scanPoint)
data["results"].append(result)
except ExeException as e:
return e.errorCode()
if opts.dryRun:
continue
print("Throughput {} tracks/second".format(result['throughput']))
if not opts.dryRun:
with open(outputJson, "w") as out:
json.dump(data, out, indent=2)
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Build and run")
parser.add_argument("--dryRun", action="store_true",
help="Print out commands, don't actually run anything")
parser.add_argument("-b", "--build", action="store_true",
help="Build only (default is to build and run)")
parser.add_argument("-v", "--verbose", action="store_true",
help="Print out compilation commands and compiler outputs")
parser.add_argument("-c", "--compilers", type=str, default="",
help="Comma separated list of compilers, default is all compilers for each technology")
parser.add_argument("--backends", type=str, default="",
help="Comma separated list of backends, default is all backends for each technology")
parser.add_argument("-t", "--technologies", type=str, default="",
help="Comma separated list of technologies, default is all ({})".format(",".join(sorted(technologies.keys()))))
parser.add_argument("-o", "--output", type=str, default="",
help="Suffix of output JSON and log files. If the output JSON file exists, it will be updated (see also --overwrite) (default: '')")
parser.add_argument("--overwrite", action="store_true",
help="Overwrite the output JSON instead of updating it")
parser.add_argument("--append", action="store_true",
help="Append new (stream, threads) results insteads of ignoring already existing point")
parser.add_argument("--noH2D", action="store_true",
help="exclude H2D transfer in measured time")
parser.add_argument("--noD2H", action="store_true",
help="exclude D2H transfer in measured time")
for par, default in scanParameters:
parser.add_argument("--"+par, type=str, default=str(default),
help="Comma separated list of {} values (default {})".format(par, str(default)))
opts = parser.parse_args()
if opts.compilers == "":
opts.compilers = []
else:
opts.compilers = opts.compilers.split(",")
if opts.backends == "":
opts.backends = []
else:
opts.backends = opts.backends.split(",")
if opts.technologies == "":
opts.technologies = []
else:
opts.technologies = opts.technologies.split(",")
for par, default in scanParameters:
setattr(opts, par, getattr(opts, par).split(","))
sys.exit(main(opts))