-
Notifications
You must be signed in to change notification settings - Fork 222
/
utils.py
378 lines (298 loc) · 11.3 KB
/
utils.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
from collections import defaultdict, namedtuple
from itertools import product
from devito.finite_differences import IndexDerivative
from devito.symbolics import (CallFromPointer, retrieve_indexed, retrieve_terminals,
search)
from devito.tools import DefaultOrderedDict, as_tuple, flatten, filter_sorted, split
from devito.types import (Dimension, DimensionTuple, Indirection, ModuloDimension,
StencilDimension)
__all__ = ['AccessMode', 'Stencil', 'IMask', 'detect_accesses', 'detect_io',
'pull_dims', 'unbounded', 'minimum', 'maximum', 'minmax_index',
'extrema', 'erange']
class AccessMode:
"""
A descriptor for access modes (read, write, ...).
"""
_modes = ('R', 'W', 'R/W', 'RR', 'WR', 'NA')
def __init__(self, is_read=False, is_write=False, mode=None):
if mode is None:
assert isinstance(is_read, bool) and isinstance(is_write, bool)
if is_read and is_write:
mode = 'R/W'
elif is_read:
mode = 'R'
elif is_write:
mode = 'W'
else:
mode = 'NA'
assert mode in self._modes
self.mode = mode
def __repr__(self):
return self.mode
def __eq__(self, other):
return isinstance(other, AccessMode) and self.mode == other.mode
@property
def is_read(self):
return self.mode in ('R', 'R/W', 'RR')
@property
def is_write(self):
return self.mode in ('W', 'R/W', 'WR')
@property
def is_read_only(self):
return self.is_read and not self.is_write
@property
def is_write_only(self):
return self.is_write and not self.is_read
@property
def is_read_write(self):
return self.is_read and self.is_write
@property
def is_read_reduction(self):
return self.mode == 'RR'
@property
def is_write_reduction(self):
return self.mode == 'WR'
@property
def is_reduction(self):
return self.is_read_reduction or self.is_write_reduction
class Stencil(DefaultOrderedDict):
"""
A mapping between Dimensions and symbolic expressions representing the
points of the stencil.
Typically the values are just integers.
Parameters
----------
entries : iterable of 2-tuples, optional
The Stencil entries.
"""
def __init__(self, items=None):
# Normalize input
items = [(d, set(as_tuple(v))) for d, v in as_tuple(items)]
super().__init__(set, items)
@classmethod
def union(cls, *dicts):
"""
Compute the union of a collection of Stencils.
"""
output = Stencil()
for i in dicts:
for k, v in i.items():
output[k] |= v
return output
class IMask(DimensionTuple):
"""
A mapper from Dimensions to data points or ranges.
"""
pass
def detect_accesses(exprs):
"""
Return a mapper `M : F -> S`, where F are Functions appearing in `exprs`
and S are Stencils. `M[f]` represents all data accesses to `f` within
`exprs`. Also map `M[None]` to all Dimensions used in `exprs` as plain
symbols, rather than as array indices.
"""
# Compute M : F -> S
mapper = defaultdict(Stencil)
for e in retrieve_indexed(exprs, deep=True):
f = e.function
for a, d0 in zip(e.indices, f.dimensions):
if isinstance(a, Indirection):
a = a.mapped
if isinstance(a, ModuloDimension) and a.parent.is_Stepping:
# Explicitly unfold SteppingDimensions-induced ModuloDimensions
mapper[f][a.root].update([a.offset - a.root])
elif isinstance(a, Dimension):
mapper[f][a].update([0])
elif a.is_Add:
dims = {i for i in a.free_symbols if isinstance(i, Dimension)}
if not dims:
continue
elif len(dims) > 1:
# There are two reasons we may end up here, 1) indirect
# accesses (e.g., a[b[x, y] + 1, y]) or 2) as a result of
# skewing-based optimizations, such as time skewing (e.g.,
# `x - time + 1`) or CIRE rotation (e.g., `x + xx - 4`)
d, others = split(dims, lambda i: d0 in i._defines)
if any(i.is_Indexed for i in a.args) or len(d) != 1:
# Case 1) -- with indirect accesses there's not much we can infer
continue
else:
# Case 2)
d, = d
_, o = split(others, lambda i: i.is_Custom)
off = sum(i for i in a.args if i.is_integer or i.free_symbols & o)
else:
d, = dims
# At this point, typically, the offset will be an integer.
# In some cases though it could be an expression, e.g.
# `db0 + time_m - 1` (from CustomDimensions due to buffering)
# or `x + o_x` (from MPI routines) or `time - ns` (from
# guarded accesses to TimeFunctions) or ... In all these cases,
# what really matters is the integer part of the offset, as
# any other symbols may resolve to zero at runtime, which is
# the base case scenario we fallback to
off = sum(i for i in a.args if i.is_integer)
# NOTE: `d in a.args` is too restrictive because of guarded
# accesses such as `time / factor - 1`
assert d in a.free_symbols
if (d.is_Custom or d.is_Default) and d.symbolic_size.is_integer:
# Explicitly unfold Default and CustomDimensions
mapper[f][d].update(range(off, d.symbolic_size + off))
else:
mapper[f][d].add(off)
# Compute M[None]
other_dims = set()
for e in as_tuple(exprs):
other_dims.update(i for i in e.free_symbols if isinstance(i, Dimension))
other_dims.update(e.implicit_dims)
other_dims = filter_sorted(other_dims)
mapper[None] = Stencil([(i, 0) for i in other_dims])
return mapper
def detect_io(exprs, relax=False):
"""
``{exprs} -> ({reads}, {writes})``
Parameters
----------
exprs : expr-like or list of expr-like
The searched expressions.
relax : bool, optional
If False, as by default, collect all Input objects, such as
Constants and Functions. Otherwise, also collect AbstractFunctions.
"""
exprs = as_tuple(exprs)
if relax is False:
rule = lambda i: i.is_Input
else:
rule = lambda i: i.is_Input or i.is_AbstractFunction
# Don't forget the nasty case with indirections on the LHS:
# >>> u[t, a[x]] = f[x] -> (reads={a, f}, writes={u})
roots = []
for i in exprs:
try:
roots.append(i.rhs)
roots.extend(list(i.lhs.indices))
roots.extend(list(i.conditionals.values()))
except AttributeError:
# E.g., CallFromPointer
roots.append(i)
reads = []
terminals = flatten(retrieve_terminals(i, deep=True) for i in roots)
for i in terminals:
candidates = set(i.free_symbols)
try:
candidates.update({i.function})
except AttributeError:
pass
for j in candidates:
try:
if rule(j):
reads.append(j)
except AttributeError:
pass
writes = []
for i in exprs:
try:
f = i.lhs.function
except AttributeError:
continue
try:
if rule(f):
writes.append(f)
except AttributeError:
# We only end up here after complex IET transformations which make
# use of composite types
assert isinstance(i.lhs, CallFromPointer)
f = i.lhs.base.function
if rule(f):
writes.append(f)
return filter_sorted(reads), filter_sorted(writes)
def pull_dims(exprs, flag=True):
"""
Extract all Dimensions from one or more expressions. If `flag=True`
(default), all of the ancestor and descendant Dimensions are extracted
as well.
"""
dims = set()
for e in as_tuple(exprs):
dims.update({i for i in e.free_symbols if isinstance(i, Dimension)})
if flag:
return set().union(*[d._defines for d in dims])
else:
return dims
# *** Utility functions for expressions that potentially contain StencilDimensions
def unbounded(expr):
"""
Retrieve all unbounded Dimensions in `expr`.
"""
# At the moment we only have logic to retrieve unbounded StencilDimensions,
# but in the future this might change
bound = set().union(*[i.dimensions for i in search(expr, IndexDerivative)])
sdims = search(expr, StencilDimension, mode='unique', deep=True)
return sdims - bound
Extrema = namedtuple('Extrema', 'm M')
def _relational(expr, callback, udims=None):
"""
Helper for `minimum`, `maximum`, and potential future utilities that share
a significant chunk of logic.
"""
if not udims:
udims = unbounded(expr)
# Resolution rule 1: StencilDimensions
sdims = [d for d in udims if d.is_Stencil]
if not sdims:
return expr
mapper = {e: callback(e) for e in sdims}
return expr.subs(mapper)
def minimum(expr, udims=None, ispace=None):
"""
Substitute the unbounded Dimensions in `expr` with their minimum point.
Unbounded Dimensions whose possible minimum value is not known are ignored.
"""
def callback(sd):
try:
return sd._min + ispace[sd].lower
except (TypeError, KeyError):
return sd._min
return _relational(expr, callback, udims)
def maximum(expr, udims=None, ispace=None):
"""
Substitute the unbounded Dimensions in `expr` with their maximum point.
Unbounded Dimensions whose possible maximum value is not known are ignored.
"""
def callback(sd):
try:
return sd._max + ispace[sd].upper
except (TypeError, KeyError):
return sd._max
return _relational(expr, callback, udims)
def extrema(expr, ispace=None):
"""
The minimum and maximum extrema assumed by `expr` once the unbounded
Dimensions are resolved.
"""
return Extrema(minimum(expr, ispace=ispace), maximum(expr, ispace=ispace))
def minmax_index(expr, d):
"""
Return the minimum and maximum indices along the `d` Dimension
among all Indexeds in `expr`.
"""
indices = set()
for i in retrieve_indexed(expr):
try:
indices.add(i.indices[d])
except KeyError:
pass
return Extrema(min(minimum(i) for i in indices),
max(maximum(i) for i in indices))
def erange(expr):
"""
All possible values that `expr` can assume once its unbounded Dimensions
are resolved.
"""
udims = unbounded(expr)
if not udims:
return (expr,)
sdims = [d for d in udims if d.is_Stencil]
ranges = [i.range for i in sdims]
mappers = [dict(zip(sdims, i)) for i in product(*ranges)]
return tuple(expr.subs(m) for m in mappers)