-
Notifications
You must be signed in to change notification settings - Fork 223
/
cpu.py
334 lines (248 loc) · 10.3 KB
/
cpu.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
from functools import partial
from devito.core.operator import OperatorCore
from devito.exceptions import InvalidOperator
from devito.ir.clusters import Toposort
from devito.passes.clusters import (Blocking, Lift, cire, cse, eliminate_arrays,
extract_increments, factorize, fuse, optimize_pows)
from devito.passes.iet import (DataManager, Ompizer, avoid_denormals, mpiize,
optimize_halospots, loop_wrapping, hoist_prodders,
relax_incr_dimensions)
from devito.tools import as_tuple, generator, timed_pass
__all__ = ['CPU64NoopOperator', 'CPU64Operator', 'CPU64OpenMPOperator',
'Intel64Operator', 'Intel64OpenMPOperator', 'Intel64FSGOperator',
'Intel64FSGOpenMPOperator',
'PowerOperator', 'PowerOpenMPOperator',
'ArmOperator', 'ArmOpenMPOperator',
'CustomOperator']
class CPU64NoopOperator(OperatorCore):
BLOCK_LEVELS = 1
"""
Loop blocking depth. So, 1 => "blocks", 2 => "blocks" and "sub-blocks",
3 => "blocks", "sub-blocks", and "sub-sub-blocks", ...
"""
CIRE_REPEATS_INV = 1
"""
Number of CIRE passes to detect and optimize away Dimension-invariant expressions.
"""
CIRE_REPEATS_SOPS = 2
"""
Number of CIRE passes to detect and optimize away redundant sum-of-products.
"""
@classmethod
def _normalize_kwargs(cls, **kwargs):
options = kwargs['options']
options['blocklevels'] = options['blocklevels'] or cls.BLOCK_LEVELS
options['cire-repeats'] = {
'invariants': options.pop('cire-repeats-inv') or cls.CIRE_REPEATS_INV,
'sops': options.pop('cire-repeats-sops') or cls.CIRE_REPEATS_SOPS
}
return kwargs
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
# Distributed-memory parallelism
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# Shared-memory parallelism
if options['openmp']:
ompizer = Ompizer()
ompizer.make_parallel(graph)
# Symbol definitions
data_manager = DataManager()
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
return graph
class CPU64Operator(CPU64NoopOperator):
@classmethod
@timed_pass(name='specializing.Clusters')
def _specialize_clusters(cls, clusters, **kwargs):
"""
Optimize Clusters for better runtime performance.
"""
options = kwargs['options']
platform = kwargs['platform']
# To create temporaries
counter = generator()
template = lambda: "r%d" % counter()
# Toposort+Fusion (the former to expose more fusion opportunities)
clusters = Toposort().process(clusters)
clusters = fuse(clusters)
# Hoist and optimize Dimension-invariant sub-expressions
clusters = cire(clusters, template, 'invariants', options, platform)
clusters = Lift().process(clusters)
# Blocking to improve data locality
clusters = Blocking(options).process(clusters)
# Reduce flops (potential arithmetic alterations)
clusters = extract_increments(clusters, template)
clusters = cire(clusters, template, 'sops', options, platform)
clusters = factorize(clusters)
clusters = optimize_pows(clusters)
# Reduce flops (no arithmetic alterations)
clusters = cse(clusters, template)
# The previous passes may have created fusion opportunities, which in
# turn may enable further optimizations
clusters = fuse(clusters)
clusters = eliminate_arrays(clusters, template)
return clusters
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
platform = kwargs['platform']
# Flush denormal numbers
avoid_denormals(graph)
# Distributed-memory parallelism
optimize_halospots(graph)
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# Lower IncrDimensions so that blocks of arbitrary shape may be used
relax_incr_dimensions(graph, counter=generator())
# SIMD-level parallelism
ompizer = Ompizer()
ompizer.make_simd(graph, simd_reg_size=platform.simd_reg_size)
# Misc optimizations
hoist_prodders(graph)
# Symbol definitions
data_manager = DataManager()
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
return graph
class CPU64OpenMPOperator(CPU64Operator):
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
platform = kwargs['platform']
# Flush denormal numbers
avoid_denormals(graph)
# Distributed-memory parallelism
optimize_halospots(graph)
if options['mpi']:
mpiize(graph, mode=options['mpi'])
# Lower IncrDimensions so that blocks of arbitrary shape may be used
relax_incr_dimensions(graph, counter=generator())
# SIMD-level parallelism
ompizer = Ompizer()
ompizer.make_simd(graph, simd_reg_size=platform.simd_reg_size)
# Shared-memory parallelism
ompizer.make_parallel(graph)
# Misc optimizations
hoist_prodders(graph)
# Symbol definitions
data_manager = DataManager()
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
return graph
Intel64Operator = CPU64Operator
Intel64OpenMPOperator = CPU64OpenMPOperator
class Intel64FSGOperator(Intel64Operator):
"""
Operator with performance optimizations tailored "For Small Grids" (FSG).
"""
@classmethod
@timed_pass(name='specializing.Clusters')
def _specialize_clusters(cls, clusters, **kwargs):
options = kwargs['options']
platform = kwargs['platform']
# To create temporaries
counter = generator()
template = lambda: "r%d" % counter()
# Toposort+Fusion (the former to expose more fusion opportunities)
clusters = Toposort().process(clusters)
clusters = fuse(clusters)
# Hoist and optimize Dimension-invariant sub-expressions
clusters = cire(clusters, template, 'invariants', options, platform)
clusters = Lift().process(clusters)
# Reduce flops (potential arithmetic alterations)
clusters = extract_increments(clusters, template)
clusters = cire(clusters, template, 'sops', options, platform)
clusters = factorize(clusters)
clusters = optimize_pows(clusters)
# Reduce flops (no arithmetic alterations)
clusters = cse(clusters, template)
# The previous passes may have created fusion opportunities, which in
# turn may enable further optimizations
clusters = fuse(clusters)
clusters = eliminate_arrays(clusters, template)
# Blocking to improve data locality
clusters = Blocking(options).process(clusters)
return clusters
class Intel64FSGOpenMPOperator(Intel64FSGOperator, CPU64OpenMPOperator):
_specialize_iet = CPU64OpenMPOperator._specialize_iet
PowerOperator = CPU64Operator
PowerOpenMPOperator = CPU64OpenMPOperator
ArmOperator = CPU64Operator
ArmOpenMPOperator = CPU64OpenMPOperator
class CustomOperator(CPU64Operator):
_known_passes = ('blocking', 'denormals', 'optcomms', 'wrapping', 'openmp',
'mpi', 'simd', 'prodders', 'topofuse', 'toposort', 'fuse')
@classmethod
def _make_clusters_passes_mapper(cls, **kwargs):
options = kwargs['options']
return {
'toposort': Toposort().process,
'fuse': fuse,
'blocking': Blocking(options).process,
# Pre-baked composite passes
'topofuse': lambda i: fuse(Toposort().process(i))
}
@classmethod
def _make_iet_passes_mapper(cls, **kwargs):
options = kwargs['options']
platform = kwargs['platform']
ompizer = Ompizer()
return {
'denormals': avoid_denormals,
'optcomms': optimize_halospots,
'wrapping': loop_wrapping,
'blocking': partial(relax_incr_dimensions, counter=generator()),
'openmp': ompizer.make_parallel,
'mpi': partial(mpiize, mode=options['mpi']),
'simd': partial(ompizer.make_simd, simd_reg_size=platform.simd_reg_size),
'prodders': hoist_prodders
}
@classmethod
def _build(cls, expressions, **kwargs):
# Sanity check
passes = as_tuple(kwargs['mode'])
if any(i not in cls._known_passes for i in passes):
raise InvalidOperator("Unknown passes `%s`" % str(passes))
return super(CustomOperator, cls)._build(expressions, **kwargs)
@classmethod
@timed_pass(name='specializing.Clusters')
def _specialize_clusters(cls, clusters, **kwargs):
passes = as_tuple(kwargs['mode'])
# Fetch passes to be called
passes_mapper = cls._make_clusters_passes_mapper(**kwargs)
# Call passes
for i in passes:
try:
clusters = passes_mapper[i](clusters)
except KeyError:
pass
return clusters
@classmethod
@timed_pass(name='specializing.IET')
def _specialize_iet(cls, graph, **kwargs):
options = kwargs['options']
passes = as_tuple(kwargs['mode'])
# Fetch passes to be called
passes_mapper = cls._make_iet_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)
# Force-call `openmp` if requested via global option
if 'openmp' not in passes and options['openmp']:
passes_mapper['openmp'](graph)
# Symbol definitions
data_manager = DataManager()
data_manager.place_definitions(graph)
data_manager.place_casts(graph)
return graph