-
Notifications
You must be signed in to change notification settings - Fork 222
/
gpu.py
157 lines (118 loc) · 4.68 KB
/
gpu.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
import cgen as c
from devito.core.operator import OperatorCore
from devito.data import FULL
from devito.ir.support import COLLAPSED
from devito.passes import (DataManager, Ompizer, ParallelTree, optimize_halospots,
mpiize, hoist_prodders)
__all__ = ['DeviceOffloadingOperator']
class OffloadingOmpizer(Ompizer):
COLLAPSE_NCORES = 1
"""
Always collapse when possible.
"""
COLLAPSE_WORK = 1
"""
Always collapse when possible.
"""
lang = dict(Ompizer.lang)
lang.update({
'par-for-teams': lambda i:
c.Pragma('omp target teams distribute parallel for collapse(%d)' % i),
'map-enter-to': lambda i, j:
c.Pragma('omp target enter data map(to: %s%s)' % (i, j)),
'map-enter-alloc': lambda i, j:
c.Pragma('omp target enter data map(alloc: %s%s)' % (i, j)),
'map-exit-from': lambda i, j:
c.Pragma('omp target exit data map(from: %s%s)' % (i, j)),
'map-exit-delete': lambda i, j:
c.Pragma('omp target exit data map(delete: %s%s)' % (i, j)),
})
def __init__(self, key=None):
if key is None:
key = lambda i: i.is_ParallelRelaxed
super(OffloadingOmpizer, self).__init__(key=key)
@classmethod
def _map_data(cls, f):
if f.is_Array:
return f.symbolic_shape
else:
return tuple(f._C_get_field(FULL, d).size for d in f.dimensions)
@classmethod
def _map_to(cls, f):
return cls.lang['map-enter-to'](f.name, ''.join('[0:%s]' % i
for i in cls._map_data(f)))
@classmethod
def _map_alloc(cls, f):
return cls.lang['map-enter-alloc'](f.name, ''.join('[0:%s]' % i
for i in cls._map_data(f)))
@classmethod
def _map_from(cls, f):
return cls.lang['map-exit-from'](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)))
def _make_threaded_prodders(self, partree):
# no-op for now
return partree
def _make_partree(self, candidates, nthreads=None):
"""
Parallelize the `candidates` Iterations attaching suitable OpenMP pragmas
for GPU offloading.
"""
assert candidates
root = candidates[0]
# Get the collapsable Iterations
collapsable = self._find_collapsable(root, candidates)
ncollapse = 1 + len(collapsable)
# Prepare to build a ParallelTree
omp_pragma = self.lang['par-for-teams'](ncollapse)
# Create a ParallelTree
body = root._rebuild(pragmas=root.pragmas + (omp_pragma,),
properties=root.properties + (COLLAPSED(ncollapse),))
partree = ParallelTree([], body, nthreads=nthreads)
collapsed = [partree] + collapsable
return root, partree, collapsed
def _make_parregion(self, partree):
# no-op for now
return partree
def _make_guard(self, partree, *args):
# no-op for now
return partree
def _make_nested_partree(self, partree):
# no-op for now
return partree
class OffloadingDataManager(DataManager):
def _alloc_array_on_high_bw_mem(self, obj, storage):
if obj in storage._high_bw_mem:
return
decl = c.Comment("no-op")
alloc = OffloadingOmpizer._map_alloc(obj)
free = OffloadingOmpizer._map_delete(obj)
storage._high_bw_mem[obj] = (decl, alloc, free)
def _map_function_on_high_bw_mem(self, obj, storage):
if obj in storage._high_bw_mem:
return
decl = c.Comment("no-op")
alloc = OffloadingOmpizer._map_to(obj)
free = OffloadingOmpizer._map_from(obj)
storage._high_bw_mem[obj] = (decl, alloc, free)
class DeviceOffloadingOperator(OperatorCore):
@classmethod
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
# Distributed-memory parallelism
optimize_halospots(graph)
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# Shared-memory parallelism
if options['openmp']:
OffloadingOmpizer().make_parallel(graph)
# Misc optimizations
hoist_prodders(graph)
# Symbol definitions
data_manager = OffloadingDataManager()
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
return graph