-
Notifications
You must be signed in to change notification settings - Fork 0
/
adv_diff_bench.py
280 lines (248 loc) · 13.6 KB
/
adv_diff_bench.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
from glob import glob
import logging
import os
from pyop2_bench import PyOP2Benchmark
from generate_mesh import generate_meshfile
from generate_triangle import generate_trianglefile
import parameters
import numpy
import pylab
class AdvDiffBenchmark(PyOP2Benchmark):
"""PyOP2 vs. Fluidity vs. DOLFIN benchmark."""
def dolfin(self, meshsize):
"""DOLFIN advection-diffusion benchmark (MPI parallel)"""
cmd = [self.mpicmd, "python dolfin_adv_diff.py %s" % meshsize]
return self.logged_call_with_time(cmd)
def fluidity(self, meshsize):
"""Fluidity advection-diffusion benchmark (sequential)"""
cmd = [self.flcmd, '-p %s/advection_diffusion.%s.flml' % (self.flmldir, meshsize)]
return self.logged_call_with_time(cmd)
def fluidity_mpi(self, meshsize):
"""Fluidity advection-diffusion benchmark (MPI parallel)"""
cmd = [self.mpicmd, self.flcmd, '-p %s/advection_diffusion.%s.flml'
% (self.flmldir, meshsize)]
return self.logged_call_with_time(cmd)
def _fluidity_pyop2_call(self, backend, meshsize, mpi=''):
cmd = [mpi, self.flcmd, '-p %s/ufl_advection_diffusion%s.%s.%s.flml'
% (self.flmldir, self.profile, backend, meshsize)]
time = self.flufl_call_with_time(cmd)
if self.profile:
pattern = 'ufl_advection_diffusion.%s.*.%%d.cprofile.part' % backend
outfile = self._path('ufl_advection_diffusion.%s.%s.%%d.cprofile' % (backend, meshsize))
self.logged_call([mpi, 'python concat.py', "'" + pattern + "'", outfile, 'mpi'])
return time
def fluidity_pyop2_seq(self, meshsize):
"""Fluidity-PyOP2 advection-diffusion benchmark (sequential backend)"""
return self._fluidity_pyop2_call('sequential', meshsize)
def fluidity_pyop2_openmp(self, meshsize):
"""Fluidity-PyOP2 advection-diffusion benchmark (OpenMP backend)"""
return self._fluidity_pyop2_call('openmp', meshsize)
def fluidity_pyop2_cuda(self, meshsize):
"""Fluidity-PyOP2 advection-diffusion benchmark (CUDA backend)"""
return self._fluidity_pyop2_call('cuda', meshsize)
def fluidity_pyop2_mpi(self, meshsize):
"""Fluidity-PyOP2 advection-diffusion benchmark (MPI parallel)"""
return self._fluidity_pyop2_call('sequential', meshsize, self.mpicmd)
def fluidity_pyop2_mpi_openmp(self, meshsize):
"""Fluidity-PyOP2 advection-diffusion benchmark (MPI + OpenMP parallel)"""
return self._fluidity_pyop2_call('openmp', meshsize, self.mpicmd)
def _pyop2_call(self, backend, meshsize):
if self.profile:
prof = '-m cProfile -o %s ' % self._path('pyop2_adv_diff.%s.cprofile' % backend)
else:
prof = ''
cmd = 'python %spyop2_adv_diff.py -m %s -b %s' % (prof, self.mesh % meshsize, backend)
return self.logged_call_with_time(cmd)
def pyop2_seq(self, meshsize):
"""PyOP2 advection-diffusion benchmark (sequential backend)"""
return self._pyop2_call('sequential', meshsize)
def pyop2_openmp(self, meshsize):
"""PyOP2 advection-diffusion benchmark (OpenMP backend)"""
return self._pyop2_call('openmp', meshsize)
def pyop2_cuda(self, meshsize):
"""PyOP2 advection-diffusion benchmark (CUDA backend)"""
return self._pyop2_call('cuda', meshsize)
def __init__(self, np=1, nruns=1, mode='min', message='', version=None, reference=None,
extrude=False, meshsize=None, params=None, mpicmd=None,
flcmd=None, profile=None):
params = params or ['version', 'meshsize']
# Execute for all combinations of these params
self.meshsize = meshsize or ['0.000008', '0.000004', '0.000002']
self.version = version or ['fluidity', 'fluidity_pyop2_seq', 'pyop2_seq']
# Compare timings against this parameter value
self.reference = ('version', reference or self.version[0])
super(AdvDiffBenchmark, self).__init__(params)
self.primed = []
self.log("Running versions: %s" % self.version)
self.log("Reference %s: %s" % self.reference)
self.np = np
self.nruns = nruns
self.mode = {'min': min,
'max': max,
'mean': numpy.mean,
'median': numpy.median}[mode]
self.message = message
self.mpicmd = mpicmd or 'mpiexec' if np > 1 else ''
self.flcmd = flcmd or '${FLUIDITY_DIR}/bin/fluidity'
self.profile = '.profile' if profile else ''
abspath = os.path.abspath(os.path.dirname(__file__))
self.meshdir = os.path.join(abspath, 'meshes', str(self.np)) if self.np > 1 else 'meshes'
self.flmldir = os.path.join(abspath, 'flmls', str(self.np)) if self.np > 1 else 'flmls'
if extrude:
self.mesh = os.path.join(self.meshdir, 'mesh.%s')
self.generate_mesh = generate_meshfile
else:
self.mesh = os.path.join(self.meshdir, 'square.%s')
self.generate_mesh = generate_trianglefile
self.plotmeta = {
'fluidity': ('k--o', 2, 'Fluidity (cores: 1)'),
'fluidity_mpi': ('g--s', 2, 'Fluidity (cores: %d)' % self.np),
'fluidity_pyop2_seq': ('r-d', 2, 'Fluidity-PyOP2 (backend: sequential)'),
'fluidity_pyop2_cuda': ('b-^', 2, 'Fluidity-PyOP2 (backend: cuda)'),
'fluidity_pyop2_openmp': ('c-v', 2, 'Fluidity-PyOP2 (backend: openmp)'),
'fluidity_pyop2_mpi': ('m-<', 2, 'Fluidity-PyOP2 (backend: mpi)'),
'fluidity_pyop2_mpi_openmp': ('y->', 2, 'Fluidity-PyOP2 (backend: mpi+openmp)'),
'pyop2_seq': ('g:1', 2, 'PyOP2 (backend: sequential)'),
'pyop2_openmp': ('r:2', 2, 'PyOP2 (backend: OpenMP)'),
'pyop2_cuda': ('b:3', 2, 'PyOP2 (backend: CUDA)'),
'dolfin': ('c-.*', 2, 'DOLFIN (cores: %d)' % self.np)
}
def create_input(self, reorder):
if not os.path.exists(self.meshdir):
os.makedirs(self.meshdir)
if not os.path.exists(self.flmldir):
os.makedirs(self.flmldir)
for s in self.meshsize:
mesh = self.mesh % s
# Generate mesh
self.log("Generating mesh %s" % mesh)
self.log(self.generate_mesh(mesh, s, capture=True, reorder=reorder,
move=self.np == 1, cwd='input'))
# Decompose the mesh if running in parallel
if self.np > 1:
if reorder:
cmd = '${HILBERT_DIR}/bin/flredecomp -i 1 -o %d reorder_mesh_0_checkpoint decomp'
self.logged_call([self.mpicmd, cmd % self.np], cwd='input')
for m in glob('decomp_CoordinateMesh_*'):
self.logged_call('mv %s %s%s' % (m, mesh, m[21:]), cwd='input')
self.logged_call("mv reorder_mesh_CoordinateMesh_0_checkpoint.ele %s.ele" % mesh, cwd='input')
self.logged_call("mv reorder_mesh_CoordinateMesh_0_checkpoint.edge %s.edge" % mesh, cwd='input')
self.logged_call("mv reorder_mesh_CoordinateMesh_0_checkpoint.node %s.node" % mesh, cwd='input')
else:
self.logged_call('${FLUIDITY_DIR}/bin/fldecomp -m triangle -n %s %s'
% (self.np, mesh), cwd='input')
self.logged_call('${FENICS_DIR}/bin/dolfin-convert %s.ele %s.xml'
% (mesh, os.path.join('meshes', s)))
def write_flml(f1, f2):
f2.write(f1.read() % {
'mesh': mesh,
'diffusivity': parameters.diffusivity,
'current_time': parameters.current_time,
'dt': parameters.dt,
'endtime': (parameters.endtime - parameters.dt),
'backend': backend
})
# Generate flml
for backend in ['sequential', 'openmp', 'cuda']:
tpl = os.path.join('input', 'ufl_advection_diffusion%s.flml.template')
with open(tpl % self.profile) as f1, \
open(os.path.join(self.flmldir, 'ufl_advection_diffusion%s.%s.%s.flml'
% (self.profile, backend, s)), 'w') as f2:
write_flml(f1, f2)
with open(os.path.join('input', 'advection_diffusion.flml.template')) as f1, \
open(os.path.join(self.flmldir, 'advection_diffusion.%s.flml' % s), 'w') as f2:
write_flml(f1, f2)
def dry_run(self, version, meshsize):
if version not in self.primed:
self.log("Performing dry run of %s using mesh size %sx%s"
% (version, meshsize, meshsize))
self.__getattribute__(version)(meshsize)
self.primed.append(version)
def run(self, version, meshsize):
self.dry_run(version, meshsize)
self.log("Running %s with mesh size %s" % (version, meshsize))
self.log("Taking %s of %d runs" % (self.mode.__name__, self.nruns))
runtimes = [self.__getattribute__(version)(meshsize) for i in range(self.nruns)]
t = self.mode(runtimes)
self.log("Run times: %s, %s: %d" % (runtimes, self.mode.__name__, t))
with open(self.mesh % meshsize + '.ele') as m:
elems = int(m.readline().split()[0])
if not elems in self.plotdata['elements']:
self.plotdata['elements'].append(elems)
self.plotdata[version].append(t)
return t
def time_all(self):
if self.message:
self.log(self.message + '\n')
self.log('PyOP2 version:')
self.logged_call('git --git-dir=%s/.git rev-parse HEAD' % os.environ['PYOP2_DIR'])
self.log('Fluidity version:')
self.logged_call('bzr revision-info -d %s' % os.environ['FLUIDITY_DIR'])
self.log('UFL version:')
self.logged_call('bzr revision-info -d %s' % os.environ['UFL_DIR'])
self.log('FFC version:')
self.logged_call('bzr revision-info -d %s' % os.environ['FFC_DIR'])
#self.log('DOLFIN version:')
#self.logged_call('bzr revision-info -d %s' % os.environ['DOLFIN_DIR'])
super(AdvDiffBenchmark, self).time_all()
def plot_result(self, format='svg'):
title = 'Benchmark of an advection-diffusion problem for 100 time steps'
title = self.message or 'Benchmark of an advection-diffusion problem for 100 time steps'
for fig, pl in zip(('linear', 'semilogx'), (pylab.plot, pylab.semilogx)):
self._plot('runtime_' + fig, pl, lambda x: x, 'upper left',
'Overall runtime in seconds', title, format)
self._plot('speedup_' + fig, pl, lambda x: x + '_speedup', 'lower right',
'Relative speedup over Fluidity baseline', title, format)
def get_parser(desc=AdvDiffBenchmark.__doc__):
import argparse
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-r', '--run', action='store_true', help='run benchmarks')
parser.add_argument('-p', '--plot', action='store_true', help='plot results')
parser.add_argument('-d', '--dump', action='store_true', help='Pickle dump')
parser.add_argument('-c', '--csv', action='store_true', help='Dump to CSV')
parser.add_argument('-v', '--versions', nargs='+', help='Versions to run')
parser.add_argument('-a', '--reference', help='Version to use as reference')
parser.add_argument('-l', '--load', help='Pickle load from file')
parser.add_argument('-q', '--quiet', help='Only print errors and warnings')
parser.add_argument('-e', '--extrude', action='store_true',
help='Use extruded structured unit square mesh')
parser.add_argument('-s', '--create-input', action='store_true',
help='Do not generate input files')
parser.add_argument('--reorder', action='store_true', default=True,
help='Reorder mesh (only when called with --create-input)')
parser.add_argument('-n', '--nruns', type=int, default=1,
help='Number of repetitions for each parameter combination')
parser.add_argument('--mode', choices=['min', 'max', 'mean', 'median'], default='min',
help='How to reduce run times of multiple runs (min/max/mean/median)')
parser.add_argument('-np', type=int, default=1,
help='Number of MPI process (Fluidity, DOLFIN)')
parser.add_argument('-m', '--message', default='',
help='Message, added to the log output')
parser.add_argument('--mpi-cmd', help='Command to run MPI processes')
parser.add_argument('--fluidity-cmd', help='Fluidity binary to run')
parser.add_argument('--python-profile', action='store_true',
help='Create a cProfile of the Python runs')
return parser
def main(args):
logging.getLogger().setLevel(logging.WARN if args.quiet else logging.INFO)
b = AdvDiffBenchmark(args.np, args.nruns, args.mode, args.message,
args.versions, args.reference, args.extrude,
mpicmd=args.mpi_cmd, flcmd=args.fluidity_cmd,
profile=args.python_profile)
if args.load and os.path.exists(args.load):
b.load(args.load)
if args.create_input:
b.create_input(args.reorder)
if args.run:
b.time_all()
b.sort_results()
b.print_result()
b.compute_speedup()
if args.dump:
b.dump()
if args.csv:
b.write_csv()
if args.plot:
b.plot_result()
if __name__ == '__main__':
main(get_parser().parse_args())