-
Notifications
You must be signed in to change notification settings - Fork 221
/
parpragma.py
564 lines (449 loc) · 20.5 KB
/
parpragma.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import numpy as np
import cgen as c
from sympy import And, Max
from devito.data import FULL
from devito.ir import (Conditional, DummyEq, Dereference, Expression, ExpressionBundle,
FindSymbols, FindNodes, ParallelTree, Prodder, List, Transformer,
IsPerfectIteration, filter_iterations, retrieve_iteration_tree,
VECTORIZED)
from devito.passes.iet.engine import iet_pass
from devito.passes.iet.langbase import LangBB, LangTransformer, DeviceAwareMixin
from devito.symbolics import INT, ccode
from devito.tools import as_tuple, prod
from devito.types import Symbol, NThreadsBase
__all__ = ['PragmaSimdTransformer', 'PragmaShmTransformer',
'PragmaDeviceAwareTransformer', 'PragmaLangBB']
class PragmaTransformer(LangTransformer):
"""
Abstract base class for LangTransformers that parallelize Iterations
as well as manage data allocation with pragmas.
"""
pass
class PragmaSimdTransformer(PragmaTransformer):
"""
Abstract base class for PragmaTransformers capable of emitting SIMD-parallel IETs.
"""
@property
def simd_reg_size(self):
return self.platform.simd_reg_size
@iet_pass
def make_simd(self, iet):
mapper = {}
for tree in retrieve_iteration_tree(iet):
candidates = [i for i in tree if i.is_ParallelRelaxed]
# As long as there's an outer level of parallelism, the innermost
# PARALLEL Iteration gets vectorized
if len(candidates) < 2:
continue
candidate = candidates[-1]
# Only fully-parallel Iterations will be SIMD-ized (ParallelRelaxed
# might not be enough then)
if not candidate.is_Parallel:
continue
# This check catches cases where an iteration appears as the vectorizable
# candidate in tree A but has actually less priority over a candidate in
# another tree B.
#
# Example:
#
# for (i = ... ) (End of tree A - i is the candidate for tree A)
# Expr1
# for (j = ...) (End of tree B - j is the candidate for tree B)
# Expr2
# ...
if not IsPerfectIteration(depth=candidates[-2]).visit(candidate):
continue
# Add SIMD pragma
indexeds = FindSymbols('indexeds').visit(candidate)
aligned = {i.name for i in indexeds if i.function.is_DiscreteFunction}
if aligned:
simd = self.lang['simd-for-aligned']
simd = as_tuple(simd(','.join(sorted(aligned)), self.simd_reg_size))
else:
simd = as_tuple(self.lang['simd-for'])
pragmas = candidate.pragmas + simd
# Add VECTORIZED property
properties = list(candidate.properties) + [VECTORIZED]
mapper[candidate] = candidate._rebuild(pragmas=pragmas, properties=properties)
iet = Transformer(mapper).visit(iet)
return iet, {}
class PragmaShmTransformer(PragmaSimdTransformer):
"""
Abstract base class for PragmaTransformers capable of emitting SIMD-parallel
and shared-memory-parallel IETs.
"""
def __init__(self, sregistry, options, platform):
"""
Parameters
----------
sregistry : SymbolRegistry
The symbol registry, to access the symbols appearing in an IET.
options : dict
The optimization options. Accepted: ['par-collapse-ncores',
'par-collapse-work', 'par-chunk-nonaffine', 'par-dynamic-work', 'par-nested']
* 'par-collapse-ncores': use a collapse clause if the number of
available physical cores is greater than this threshold.
* 'par-collapse-work': use a collapse clause if the trip count of the
collapsable Iterations is statically known to exceed this threshold.
* 'par-chunk-nonaffine': coefficient to adjust the chunk size in
non-affine parallel Iterations.
* 'par-dynamic-work': use dynamic scheduling if the operation count per
iteration exceeds this threshold. Otherwise, use static scheduling.
* 'par-nested': nested parallelism if the number of hyperthreads per core
is greater than this threshold.
platform : Platform
The underlying platform.
"""
key = lambda i: i.is_ParallelRelaxed and not i.is_Vectorized
super().__init__(key, sregistry, platform)
self.collapse_ncores = options['par-collapse-ncores']
self.collapse_work = options['par-collapse-work']
self.chunk_nonaffine = options['par-chunk-nonaffine']
self.dynamic_work = options['par-dynamic-work']
self.nested = options['par-nested']
@property
def ncores(self):
return self.platform.cores_physical
@property
def nhyperthreads(self):
return self.platform.threads_per_core
@property
def nthreads(self):
return self.sregistry.nthreads
@property
def nthreads_nested(self):
return self.sregistry.nthreads_nested
@property
def nthreads_nonaffine(self):
return self.sregistry.nthreads_nonaffine
@property
def threadid(self):
return self.sregistry.threadid
def _select_candidates(self, candidates):
assert candidates
if self.ncores < self.collapse_ncores:
return candidates[0], []
mapper = {}
for n0, root in enumerate(candidates):
collapsable = []
for n, i in enumerate(candidates[n0+1:], n0+1):
# The Iteration nest [root, ..., i] must be perfect
if not IsPerfectIteration(depth=i).visit(root):
break
# Loops are collapsable only if none of the iteration variables appear
# in initializer expressions. For example, the following two loops
# cannot be collapsed
#
# for (i = ... )
# for (j = i ...)
# ...
#
# Here, we make sure this won't happen
if any(j.dim in i.symbolic_min.free_symbols for j in candidates[n0:n]):
break
# Also, we do not want to collapse SIMD-vectorized Iterations
if i.is_Vectorized:
break
# Would there be enough work per parallel iteration?
nested = candidates[n+1:]
if nested:
try:
work = prod([int(j.dim.symbolic_size) for j in nested])
if work < self.collapse_work:
break
except TypeError:
pass
collapsable.append(i)
# Give a score to this candidate, based on the number of fully-parallel
# Iterations and their position (i.e. outermost to innermost) in the nest
score = (
int(root.is_ParallelNoAtomic),
int(len([i for i in collapsable if i.is_ParallelNoAtomic]) >= 1),
int(len([i for i in collapsable if i.is_ParallelRelaxed]) >= 1),
-(n0 + 1) # The outermost, the better
)
mapper[(root, tuple(collapsable))] = score
# Retrieve the candidates with highest score
root, collapsable = max(mapper, key=mapper.get)
return root, list(collapsable)
def _make_reductions(self, partree):
if not any(i.is_ParallelAtomic for i in partree.collapsed):
return partree
exprs = [i for i in FindNodes(Expression).visit(partree) if i.is_Increment]
reduction = [i.output for i in exprs]
if all(i.is_Affine for i in partree.collapsed) or \
all(not i.is_Indexed for i in reduction):
# Implement reduction
mapper = {partree.root: partree.root._rebuild(reduction=reduction)}
else:
# Make sure the increment is atomic
mapper = {i: i._rebuild(pragmas=self.lang['atomic']) for i in exprs}
partree = Transformer(mapper).visit(partree)
return partree
def _make_threaded_prodders(self, partree):
mapper = {i: self.Prodder(i) for i in FindNodes(Prodder).visit(partree)}
partree = Transformer(mapper).visit(partree)
return partree
def _make_partree(self, candidates, nthreads=None):
assert candidates
# Get the collapsable Iterations
root, collapsable = self._select_candidates(candidates)
ncollapse = 1 + len(collapsable)
# Prepare to build a ParallelTree
if all(i.is_Affine for i in candidates):
bundles = FindNodes(ExpressionBundle).visit(root)
sops = sum(i.ops for i in bundles)
if sops >= self.dynamic_work:
schedule = 'dynamic'
else:
schedule = 'static'
if nthreads is None:
# pragma ... for ... schedule(..., 1)
nthreads = self.nthreads
body = self.HostIteration(schedule=schedule, ncollapse=ncollapse,
**root.args)
else:
# pragma ... parallel for ... schedule(..., 1)
body = self.HostIteration(schedule=schedule, parallel=True,
ncollapse=ncollapse, nthreads=nthreads,
**root.args)
prefix = []
else:
# pragma ... for ... schedule(..., expr)
assert nthreads is None
nthreads = self.nthreads_nonaffine
chunk_size = Symbol(name='chunk_size')
body = self.HostIteration(ncollapse=ncollapse, chunk_size=chunk_size,
**root.args)
niters = prod([root.symbolic_size] + [j.symbolic_size for j in collapsable])
value = INT(Max(niters / (nthreads*self.chunk_nonaffine), 1))
prefix = [Expression(DummyEq(chunk_size, value, dtype=np.int32))]
# Create a ParallelTree
partree = ParallelTree(prefix, body, nthreads=nthreads)
return root, partree
def _make_parregion(self, partree, parrays):
if not any(i.is_ParallelPrivate for i in partree.collapsed):
return self.Region(partree)
# Vector-expand all written Arrays within `partree`, since at least
# one of the parallelized Iterations requires thread-private Arrays
# E.g. a(x, y) -> b(tid, x, y), where `tid` is the ThreadID Dimension
vexpandeds = []
for n in FindNodes(Expression).visit(partree):
i = n.write
if not (i.is_Array or i.is_TempFunction):
continue
elif i in parrays:
pi = parrays[i]
else:
pi = parrays.setdefault(i, i._make_pointer(dim=self.threadid))
vexpandeds.append(VExpanded(i, pi))
if vexpandeds:
init = self.lang['thread-num'](retobj=self.threadid)
prefix = List(body=[init] + vexpandeds + list(partree.prefix),
footer=c.Line())
partree = partree._rebuild(prefix=prefix)
return self.Region(partree)
def _make_guard(self, parregion):
return parregion
def _make_nested_partree(self, partree):
# Apply heuristic
if self.nhyperthreads <= self.nested:
return partree
# Note: there might be multiple sub-trees amenable to nested parallelism,
# hence we loop over all of them
#
# for (i = ... ) // outer parallelism
# for (j0 = ...) // first source of nested parallelism
# ...
# for (j1 = ...) // second source of nested parallelism
# ...
mapper = {}
for tree in retrieve_iteration_tree(partree):
outer = tree[:partree.ncollapsed]
inner = tree[partree.ncollapsed:]
# Heuristic: nested parallelism is applied only if the top nested
# parallel Iteration iterates *within* the top outer parallel Iteration
# (i.e., the outer is a loop over blocks, while the nested is a loop
# within a block)
candidates = []
for i in inner:
if self.key(i) and any((j.dim.root is i.dim.root) for j in outer):
candidates.append(i)
elif candidates:
# If there's at least one candidate but `i` doesn't honor the
# heuristic above, then we break, as the candidates must be
# perfectly nested
break
if not candidates:
continue
# Introduce nested parallelism
subroot, subpartree = self._make_partree(candidates, self.nthreads_nested)
mapper[subroot] = subpartree
partree = Transformer(mapper).visit(partree)
return partree
def _make_parallel(self, iet):
mapper = {}
parrays = {}
for tree in retrieve_iteration_tree(iet, mode='superset'):
# Get the parallelizable Iterations in `tree`
candidates = filter_iterations(tree, key=self.key)
if not candidates:
continue
# Outer parallelism
root, partree = self._make_partree(candidates)
if partree is None or root in mapper:
continue
# Nested parallelism
partree = self._make_nested_partree(partree)
# Handle reductions
partree = self._make_reductions(partree)
# Atomicize and optimize single-thread prodders
partree = self._make_threaded_prodders(partree)
# Wrap within a parallel region
parregion = self._make_parregion(partree, parrays)
# Protect the parallel region if necessary
parregion = self._make_guard(parregion)
mapper[root] = parregion
iet = Transformer(mapper).visit(iet)
# The new arguments introduced by this pass
args = [i for i in FindSymbols().visit(iet) if isinstance(i, (NThreadsBase))]
for n in FindNodes(VExpanded).visit(iet):
args.extend([(n.pointee, True), n.pointer])
return iet, {'args': args, 'includes': [self.lang['header']]}
@iet_pass
def make_parallel(self, iet):
return self._make_parallel(iet)
class PragmaDeviceAwareTransformer(DeviceAwareMixin, PragmaShmTransformer):
"""
Abstract base class for PragmaTransformers capable of emitting SIMD-parallel,
shared-memory-parallel, and device-parallel IETs.
"""
def __init__(self, sregistry, options, platform):
super().__init__(sregistry, options, platform)
self.gpu_fit = options['gpu-fit']
self.par_tile = options['par-tile']
self.par_disabled = options['par-disabled']
def _make_threaded_prodders(self, partree):
if isinstance(partree.root, self.DeviceIteration):
# no-op for now
return partree
else:
return super()._make_threaded_prodders(partree)
def _make_partree(self, candidates, nthreads=None):
"""
Parallelize the `candidates` Iterations. In particular:
* A PARALLEL Iteration writing (reading) a mapped Array while
reading (writing) a host Function (that is, all Functions `f`
such that `is_on_device(f)` gives False) is parallelized
on the host. These are essentially the Iterations that initialize
or dump the Devito-created buffers.
* All other PARALLEL Iterations (typically, the majority) are
offloaded to the device.
"""
assert candidates
root, collapsable = self._select_candidates(candidates)
if self._is_offloadable(root):
body = self.DeviceIteration(gpu_fit=self.gpu_fit,
ncollapse=len(collapsable) + 1,
**root.args)
partree = ParallelTree([], body, nthreads=nthreads)
return root, partree
elif not self.par_disabled:
# Resort to host parallelism
return super()._make_partree(candidates, nthreads)
else:
return root, None
def _make_parregion(self, partree, *args):
if isinstance(partree.root, self.DeviceIteration):
# no-op for now
return partree
else:
return super()._make_parregion(partree, *args)
def _make_guard(self, parregion, *args):
partrees = FindNodes(ParallelTree).visit(parregion)
if not any(isinstance(i.root, self.DeviceIteration) for i in partrees):
return super()._make_guard(parregion, *args)
cond = []
# There must be at least one iteration or potential crash
if not parregion.is_Affine:
trees = retrieve_iteration_tree(parregion.root)
tree = trees[0][:parregion.ncollapsed]
cond.extend([i.symbolic_size > 0 for i in tree])
# SparseFunctions may occasionally degenerate to zero-size arrays. In such
# a case, a copy-in produces a `nil` pointer on the device. To fire up a
# parallel loop we must ensure none of the SparseFunction pointers are `nil`
symbols = FindSymbols().visit(parregion)
sfs = [i for i in symbols if i.is_SparseFunction]
if sfs:
size = [prod(f._C_get_field(FULL, d).size for d in f.dimensions) for f in sfs]
cond.extend([i > 0 for i in size])
# Combine all cond elements
if cond:
parregion = List(body=[Conditional(And(*cond), parregion)])
return parregion
def _make_nested_partree(self, partree):
if isinstance(partree.root, self.DeviceIteration):
# no-op for now
return partree
else:
return super()._make_nested_partree(partree)
class PragmaLangBB(LangBB):
@classmethod
def _make_sections_from_imask(cls, f, imask):
sections = cls._make_symbolic_sections_from_imask(f, imask)
sections = ['[%s:%s]' % (ccode(start), ccode(size)) for start, size in sections]
sections = ''.join(sections)
return sections
@classmethod
def _map_to(cls, f, imask=None, queueid=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-enter-to'](f.name, sections)
_map_to_wait = _map_to
@classmethod
def _map_alloc(cls, f, imask=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-enter-alloc'](f.name, sections)
@classmethod
def _map_present(cls, f, imask=None):
return
@classmethod
def _map_wait(cls, queueid=None):
try:
return cls.mapper['map-wait'](queueid)
except KeyError:
# Not all languages may provide an explicit wait construct
return None
@classmethod
def _map_update(cls, f, imask=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-update'](f.name, sections)
@classmethod
def _map_update_host(cls, f, imask=None, queueid=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-update-host'](f.name, sections)
_map_update_host_async = _map_update_host
@classmethod
def _map_update_device(cls, f, imask=None, queueid=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-update-device'](f.name, sections)
_map_update_device_async = _map_update_device
@classmethod
def _map_release(cls, f, imask=None, devicerm=None):
sections = cls._make_sections_from_imask(f, imask)
return cls.mapper['map-release'](f.name, sections,
(' if(%s)' % devicerm.name) if devicerm else '')
@classmethod
def _map_delete(cls, f, imask=None, devicerm=None):
sections = cls._make_sections_from_imask(f, imask)
# This ugly condition is to avoid a copy-back when, due to
# domain decomposition, the local size of a Function is 0, which
# would cause a crash
items = []
if devicerm is not None:
items.append(devicerm.name)
items.extend(['(%s != 0)' % i for i in cls._map_data(f)])
cond = ' if(%s)' % ' && '.join(items)
return cls.mapper['map-exit-delete'](f.name, sections, cond)
# Utils
class VExpanded(Dereference):
pass