-
Notifications
You must be signed in to change notification settings - Fork 222
/
definitions.py
384 lines (311 loc) · 13.5 KB
/
definitions.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
"""
Collection of passes for the declaration, allocation, movement and deallocation
of symbols and data.
"""
from collections import OrderedDict, namedtuple
from functools import singledispatch
from operator import itemgetter
import cgen as c
from devito.ir import (EntryFunction, List, LocalExpression, FindSymbols,
MapExprStmts, Transformer)
from devito.passes.iet.engine import iet_pass
from devito.passes.iet.langbase import LangBB
from devito.passes.iet.misc import is_on_device
from devito.symbolics import ccode
from devito.tools import as_mapper, filter_sorted, flatten
from devito.types import DeviceRM
__all__ = ['DataManager', 'DeviceAwareDataManager', 'Storage']
MetaSite = namedtuple('Definition', 'allocs frees pallocs pfrees')
class Storage(OrderedDict):
def __init__(self, *args, **kwargs):
super(Storage, self).__init__(*args, **kwargs)
self.defined = set()
def update(self, key, site, **kwargs):
if key in self.defined:
return
try:
metasite = self[site]
except KeyError:
metasite = self.setdefault(site, MetaSite([], [], [], []))
for k, v in kwargs.items():
getattr(metasite, k).append(v)
self.defined.add(key)
def map(self, key, k, v):
if key in self.defined:
return
self[k] = v
self.defined.add(key)
class DataManager(object):
lang = LangBB
"""
The language used to express data allocations, deletions, and host-device movements.
"""
def __init__(self, sregistry, *args):
"""
Parameters
----------
sregistry : SymbolRegistry
The symbol registry, to quickly access the special symbols that may
appear in the IET.
"""
self.sregistry = sregistry
def _alloc_object_on_low_lat_mem(self, site, obj, storage):
"""
Allocate a LocalObject in the low latency memory.
"""
storage.update(obj, site, allocs=c.Value(obj._C_typename, obj.name))
def _alloc_array_on_low_lat_mem(self, site, obj, storage):
"""
Allocate an Array in the low latency memory.
"""
shape = "".join("[%s]" % ccode(i) for i in obj.symbolic_shape)
alignment = self.lang['aligned'](obj._data_alignment)
value = "%s%s %s" % (obj.name, shape, alignment)
storage.update(obj, site, allocs=c.POD(obj.dtype, value))
def _alloc_scalar_on_low_lat_mem(self, site, expr, storage):
"""
Allocate a Scalar in the low latency memory.
"""
key = (site, expr.write) # Ensure a scalar isn't redeclared in the given site
storage.map(key, expr, LocalExpression(**expr.args))
def _alloc_array_on_high_bw_mem(self, site, obj, storage, *args):
"""
Allocate an Array in the high bandwidth memory.
"""
decl = "(*%s)%s" % (obj.name, "".join("[%s]" % i for i in obj.symbolic_shape[1:]))
decl = c.Value(obj._C_typedata, decl)
shape = "".join("[%s]" % i for i in obj.symbolic_shape)
size = "sizeof(%s%s)" % (obj._C_typedata, shape)
alloc = c.Statement(self.lang['alloc-host'](obj.name, obj._data_alignment, size))
free = c.Statement(self.lang['free-host'](obj.name))
storage.update(obj, site, allocs=(decl, alloc), frees=free)
def _alloc_object_array_on_low_lat_mem(self, site, obj, storage):
"""
Allocate an Array of Objects in the low latency memory.
"""
shape = "".join("[%s]" % ccode(i) for i in obj.symbolic_shape)
decl = "%s%s" % (obj.name, shape)
storage.update(obj, site, allocs=c.Value(obj._C_typedata, decl))
def _alloc_pointed_array_on_high_bw_mem(self, site, obj, storage):
"""
Allocate the following objects in the high bandwidth memory:
* The pointer array `obj`;
* The pointee Array `obj.array`
If the pointer array is defined over `sregistry.threadid`, that is a thread
Dimension, then each `obj.array` slice is allocated and freed individually
by the owner thread.
"""
# The pointer array
decl = "**%s" % obj.name
decl = c.Value(obj._C_typedata, decl)
size = 'sizeof(%s*)*%s' % (obj._C_typedata, obj.dim.symbolic_size)
alloc0 = c.Statement(self.lang['alloc-host'](obj.name, obj._data_alignment, size))
free0 = c.Statement(self.lang['free-host'](obj.name))
# The pointee Array
pobj = '%s[%s]' % (obj.name, obj.dim.name)
shape = "".join("[%s]" % i for i in obj.array.symbolic_shape)
size = "sizeof(%s%s)" % (obj._C_typedata, shape)
alloc1 = c.Statement(self.lang['alloc-host'](pobj, obj._data_alignment, size))
free1 = c.Statement(self.lang['free-host'](pobj))
if obj.dim is self.sregistry.threadid:
storage.update(obj, site, allocs=(decl, alloc0), frees=free0,
pallocs=(obj.dim, alloc1), pfrees=(obj.dim, free1))
else:
storage.update(obj, site, allocs=(decl, alloc0, alloc1), frees=(free0, free1))
def _dump_storage(self, iet, storage):
mapper = {}
for k, v in storage.items():
# Expr -> LocalExpr ?
if k.is_Expression:
mapper[k] = v
continue
# allocs/pallocs
allocs = flatten(v.allocs)
for tid, body in as_mapper(v.pallocs, itemgetter(0), itemgetter(1)).items():
header = self.lang.Region._make_header(tid.symbolic_size)
init = c.Initializer(c.Value(tid._C_typedata, tid.name),
self.lang['thread-num'])
allocs.append(c.Module((header, c.Block([init] + body))))
if allocs:
allocs.append(c.Line())
# frees/pfrees
frees = []
for tid, body in as_mapper(v.pfrees, itemgetter(0), itemgetter(1)).items():
header = self.lang.Region._make_header(tid.symbolic_size)
init = c.Initializer(c.Value(tid._C_typedata, tid.name),
self.lang['thread-num'])
frees.append(c.Module((header, c.Block([init] + body))))
frees.extend(flatten(v.frees))
if frees:
frees.insert(0, c.Line())
mapper[k] = k._rebuild(body=List(header=allocs, body=k.body, footer=frees),
**k.args_frozen)
processed = Transformer(mapper, nested=True).visit(iet)
return processed
@iet_pass
def place_definitions(self, iet, **kwargs):
"""
Create a new IET where all symbols have been declared, allocated, and
deallocated in one or more memory spaces.
Parameters
----------
iet : Callable
The input Iteration/Expression tree.
"""
storage = Storage()
refmap = FindSymbols().visit(iet).mapper
placed = list(iet.parameters)
for k, v in MapExprStmts().visit(iet).items():
if k.is_LocalExpression:
placed.append(k.write)
objs = []
elif k.is_Expression:
if k.is_definition:
site = v[-1] if v else iet
self._alloc_scalar_on_low_lat_mem(site, k, storage)
continue
objs = [k.write]
elif k.is_Dereference:
placed.append(k.pointee)
if k.pointer in placed:
objs = []
else:
objs = [k.pointer]
elif k.is_Call:
objs = list(k.functions)
if k.retobj is not None:
objs.append(k.retobj.function)
elif k.is_PointerCast:
placed.append(k.function)
objs = []
for i in objs:
if i in placed:
continue
try:
if i.is_LocalObject:
# LocalObject's get placed as close as possible to
# their first occurrence
site = iet
for n in v:
if i in refmap[n]:
break
site = n
self._alloc_object_on_low_lat_mem(site, i, storage)
elif i.is_Array:
# Arrays get placed at the top of the IET
if i._mem_heap:
self._alloc_array_on_high_bw_mem(iet, i, storage)
else:
self._alloc_array_on_low_lat_mem(iet, i, storage)
elif i.is_ObjectArray:
# ObjectArrays get placed at the top of the IET
self._alloc_object_array_on_low_lat_mem(iet, i, storage)
elif i.is_PointerArray:
# PointerArrays get placed at the top of the IET
self._alloc_pointed_array_on_high_bw_mem(iet, i, storage)
except AttributeError:
# E.g., a generic SymPy expression
pass
iet = self._dump_storage(iet, storage)
return iet, {}
@iet_pass
def map_onmemspace(self, iet, **kwargs):
"""
Create a new IET where certain symbols have been mapped to one or more
extra memory spaces. This may or may not be required depending on the
underlying architecture.
"""
return iet, {}
@iet_pass
def place_casts(self, iet):
"""
Create a new IET with the necessary type casts.
Parameters
----------
iet : Callable
The input Iteration/Expression tree.
"""
functions = FindSymbols().visit(iet)
need_cast = {i for i in functions if i.is_Tensor}
# Make the generated code less verbose by avoiding unnecessary casts
symbol_names = {i.name for i in FindSymbols('free-symbols').visit(iet)}
need_cast = {i for i in need_cast if i.name in symbol_names}
casts = tuple(self.lang.PointerCast(i) for i in iet.parameters if i in need_cast)
if casts:
casts = (List(body=casts, footer=c.Line()),)
iet = iet._rebuild(body=casts + iet.body)
return iet, {}
def process(self, graph):
"""
Apply the `map_on_memspace`, `place_definitions` and `place_casts` passes.
"""
self.map_onmemspace(graph)
self.place_definitions(graph)
self.place_casts(graph)
class DeviceAwareDataManager(DataManager):
def __init__(self, sregistry, options):
"""
Parameters
----------
sregistry : SymbolRegistry
The symbol registry, to quickly access the special symbols that may
appear in the IET.
options : dict
The optimization options.
Accepted: ['gpu-fit'].
* 'gpu-fit': an iterable of `Function`s that are guaranteed to fit
in the device memory. By default, all `Function`s except saved
`TimeFunction`'s are assumed to fit in the device memory.
"""
super().__init__(sregistry)
self.gpu_fit = options['gpu-fit']
def _alloc_array_on_high_bw_mem(self, site, obj, storage):
_storage = Storage()
super()._alloc_array_on_high_bw_mem(site, obj, _storage)
allocs = _storage[site].allocs + [self.lang._map_alloc(obj)]
frees = [self.lang._map_delete(obj)] + _storage[site].frees
storage.update(obj, site, allocs=allocs, frees=frees)
def _map_function_on_high_bw_mem(self, site, obj, storage, devicerm, read_only=False):
"""
Place a Function in the high bandwidth memory.
"""
alloc = self.lang._map_to(obj)
if read_only is False:
free = c.Collection([self.lang._map_update(obj),
self.lang._map_release(obj, devicerm=devicerm)])
else:
free = self.lang._map_delete(obj, devicerm=devicerm)
storage.update(obj, site, allocs=alloc, frees=free)
@iet_pass
def map_onmemspace(self, iet, **kwargs):
@singledispatch
def _map_onmemspace(iet):
return iet, {}
@_map_onmemspace.register(EntryFunction)
def _(iet):
# Special symbol which gives user code control over data deallocations
devicerm = DeviceRM()
# Collect written and read-only symbols
writes = set()
reads = set()
for i, v in MapExprStmts().visit(iet).items():
if not i.is_Expression:
# No-op
continue
if not any(isinstance(j, self.lang.DeviceIteration) for j in v):
# Not an offloaded Iteration tree
continue
if i.write.is_DiscreteFunction:
writes.add(i.write)
reads.update({r for r in i.reads if r.is_DiscreteFunction})
# Populate `storage`
storage = Storage()
for i in filter_sorted(writes):
if is_on_device(i, self.gpu_fit):
self._map_function_on_high_bw_mem(iet, i, storage, devicerm)
for i in filter_sorted(reads - writes):
if is_on_device(i, self.gpu_fit):
self._map_function_on_high_bw_mem(iet, i, storage, devicerm, True)
iet = self._dump_storage(iet, storage)
return iet, {'args': devicerm}
return _map_onmemspace(iet)