/
gpu_openacc.py
300 lines (225 loc) · 9.9 KB
/
gpu_openacc.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
from functools import partial, singledispatch
import cgen as c
from devito.core.gpu_openmp import (DeviceOpenMPNoopOperator, DeviceOpenMPIteration,
DeviceOmpizer, DeviceOpenMPDataManager)
from devito.exceptions import InvalidOperator
from devito.ir.equations import DummyEq
from devito.ir.iet import Call, ElementalFunction, FindSymbols, List, LocalExpression
from devito.logger import warning
from devito.mpi.distributed import MPICommObject
from devito.mpi.routines import MPICallable
from devito.passes.iet import optimize_halospots, mpiize, hoist_prodders, iet_pass
from devito.symbolics import Byref, DefFunction, Macro
from devito.tools import as_tuple, prod, timed_pass
from devito.types import Symbol
__all__ = ['DeviceOpenACCNoopOperator', 'DeviceOpenACCOperator',
'DeviceOpenACCCustomOperator']
# TODO: currently inhereting from the OpenMP Operators. Ideally, we should/could
# abstract things away so as to have a separate, language-agnostic superclass
class DeviceOpenACCIteration(DeviceOpenMPIteration):
@classmethod
def _make_construct(cls, **kwargs):
return 'acc parallel loop'
@classmethod
def _make_clauses(cls, **kwargs):
kwargs['chunk_size'] = False
clauses = super(DeviceOpenACCIteration, cls)._make_clauses(**kwargs)
partree = kwargs['nodes']
deviceptrs = [i.name for i in FindSymbols().visit(partree) if i.is_Array]
if deviceptrs:
clauses.append("deviceptr(%s)" % ",".join(deviceptrs))
return clauses
class DeviceAccizer(DeviceOmpizer):
lang = dict(DeviceOmpizer.__base__.lang)
lang.update({
'atomic': c.Pragma('acc atomic update'),
'map-enter-to': lambda i, j:
c.Pragma('acc enter data copyin(%s%s)' % (i, j)),
'map-enter-alloc': lambda i, j:
c.Pragma('acc enter data create(%s%s)' % (i, j)),
'map-present': lambda i, j:
c.Pragma('acc data present(%s%s)' % (i, j)),
'map-update': lambda i, j:
c.Pragma('acc exit data copyout(%s%s)' % (i, j)),
'map-release': lambda i, j:
c.Pragma('acc exit data delete(%s%s)' % (i, j)),
'map-exit-delete': lambda i, j:
c.Pragma('acc exit data delete(%s%s)' % (i, j)),
'map-pointers': lambda i:
c.Pragma('acc host_data use_device(%s)' % i)
})
_Iteration = DeviceOpenACCIteration
@classmethod
def _map_present(cls, f):
# TODO: currently this is unused, because we cannot yet distinguish between
# "real" Arrays and Functions that "acts as Arrays", created by the compiler
# to build support routines (e.g., the Sendrecv/Gather/Scatter MPI Callables).
# We should only use "#pragma acc present" for *real* Arrays -- that is
# temporaries that are born and die on the Device
return cls.lang['map-present'](f.name, ''.join('[0:%s]' % i
for i in cls._map_data(f)))
@classmethod
def _map_delete(cls, f):
return cls.lang['map-exit-delete'](f.name, ''.join('[0:%s]' % i
for i in cls._map_data(f)))
@classmethod
def _map_pointers(cls, functions):
return cls.lang['map-pointers'](','.join(f.name for f in functions))
def _make_parallel(self, iet):
iet, metadata = super(DeviceAccizer, self)._make_parallel(iet)
metadata['includes'] = ['openacc.h']
return iet, metadata
class DeviceOpenACCDataManager(DeviceOpenMPDataManager):
_Parallelizer = DeviceAccizer
def _alloc_array_on_high_bw_mem(self, site, obj, storage):
"""
Allocate an Array in the high bandwidth memory.
"""
size_trunkated = "".join("[%s]" % i for i in obj.symbolic_shape[1:])
decl = c.Value(obj._C_typedata, "(*%s)%s" % (obj.name, size_trunkated))
cast = "(%s (*)%s)" % (obj._C_typedata, size_trunkated)
size_full = prod(obj.symbolic_shape)
alloc = "%s acc_malloc(sizeof(%s[%s]))" % (cast, obj._C_typedata, size_full)
init = c.Initializer(decl, alloc)
free = c.Statement('acc_free(%s)' % obj.name)
storage.update(obj, site, allocs=init, frees=free)
@iet_pass
def initialize(iet, **kwargs):
"""
Initialize the OpenACC environment.
"""
@singledispatch
def _initialize(iet):
# TODO: we need to pick the rank from `comm_shm`, not `comm`,
# so that we have nranks == ngpus (as long as the user has launched
# the right number of MPI processes per node given the available
# number of GPUs per node)
comm = None
for i in iet.parameters:
if isinstance(i, MPICommObject):
comm = i
break
device_nvidia = Macro('acc_device_nvidia')
body = Call('acc_init', [device_nvidia])
if comm is not None:
rank = Symbol(name='rank')
rank_decl = LocalExpression(DummyEq(rank, 0))
rank_init = Call('MPI_Comm_rank', [comm, Byref(rank)])
ngpus = Symbol(name='ngpus')
call = DefFunction('acc_get_num_devices', device_nvidia)
ngpus_init = LocalExpression(DummyEq(ngpus, call))
devicenum = Symbol(name='devicenum')
devicenum_init = LocalExpression(DummyEq(devicenum, rank % ngpus))
set_device_num = Call('acc_set_device_num', [devicenum, device_nvidia])
body = [rank_decl, rank_init, ngpus_init, devicenum_init,
set_device_num, body]
init = List(header=c.Comment('Begin of OpenACC+MPI setup'),
body=body,
footer=(c.Comment('End of OpenACC+MPI setup'), c.Line()))
iet = iet._rebuild(body=(init,) + iet.body)
return iet
@_initialize.register(ElementalFunction)
@_initialize.register(MPICallable)
def _(iet):
return iet
iet = _initialize(iet)
return iet, {}
class DeviceOpenACCNoopOperator(DeviceOpenMPNoopOperator):
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
sregistry = kwargs['sregistry']
# Distributed-memory parallelism
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# GPU parallelism via OpenACC offloading
DeviceAccizer(sregistry, options).make_parallel(graph)
# Symbol definitions
data_manager = DeviceOpenACCDataManager(sregistry)
data_manager.place_ondevice(graph)
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
# Initialize OpenACC environment
if options['mpi']:
initialize(graph)
return graph
class DeviceOpenACCOperator(DeviceOpenACCNoopOperator):
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
sregistry = kwargs['sregistry']
# Distributed-memory parallelism
optimize_halospots(graph)
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# GPU parallelism via OpenACC offloading
DeviceAccizer(sregistry, options).make_parallel(graph)
# Misc optimizations
hoist_prodders(graph)
# Symbol definitions
data_manager = DeviceOpenACCDataManager(sregistry)
data_manager.place_ondevice(graph)
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
# Initialize OpenACC environment
if options['mpi']:
initialize(graph)
return graph
class DeviceOpenACCCustomOperator(DeviceOpenACCOperator):
_known_passes = ('optcomms', 'openacc', 'mpi', 'prodders')
_known_passes_disabled = ('blocking', 'openmp', 'denormals', 'simd')
assert not (set(_known_passes) & set(_known_passes_disabled))
@classmethod
def _make_passes_mapper(cls, **kwargs):
options = kwargs['options']
sregistry = kwargs['sregistry']
accizer = DeviceAccizer(sregistry, options)
return {
'optcomms': partial(optimize_halospots),
'openacc': partial(accizer.make_parallel),
'mpi': partial(mpiize, mode=options['mpi']),
'prodders': partial(hoist_prodders)
}
@classmethod
def _build(cls, expressions, **kwargs):
# Sanity check
passes = as_tuple(kwargs['mode'])
for i in passes:
if i not in cls._known_passes:
if i in cls._known_passes_disabled:
warning("Got explicit pass `%s`, but it's unsupported on an "
"Operator of type `%s`" % (i, str(cls)))
else:
raise InvalidOperator("Unknown pass `%s`" % i)
return super(DeviceOpenACCCustomOperator, cls)._build(expressions, **kwargs)
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
sregistry = kwargs['sregistry']
passes = as_tuple(kwargs['mode'])
# Fetch passes to be called
passes_mapper = cls._make_passes_mapper(**kwargs)
# Call passes
for i in passes:
try:
passes_mapper[i](graph)
except KeyError:
pass
# Force-call `mpi` if requested via global option
if 'mpi' not in passes and options['mpi']:
passes_mapper['mpi'](graph)
# GPU parallelism via OpenACC offloading
if 'openacc' not in passes:
passes_mapper['openacc'](graph)
# Symbol definitions
data_manager = DeviceOpenACCDataManager(sregistry)
data_manager.place_ondevice(graph)
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
# Initialize OpenACC environment
if options['mpi']:
initialize(graph)
return graph