-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathtest_visitors.py
397 lines (328 loc) · 11.4 KB
/
test_visitors.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
385
386
387
388
389
390
391
392
393
394
395
396
397
import cgen as c
from sympy import Mod
import pytest
from devito import Grid, Eq, Function, TimeFunction, Operator, sin
from devito.ir.equations import DummyEq
from devito.ir.iet import (Block, Expression, Callable, FindNodes, FindSections,
FindSymbols, IsPerfectIteration, Transformer,
Conditional, printAST, Iteration, MapNodes, Call)
from devito.types import SpaceDimension, Array
@pytest.fixture(scope="module")
def dims():
x, y, z = Grid((3, 3, 3)).dimensions
return {'i': SpaceDimension(name='i'),
'j': SpaceDimension(name='j'),
'k': SpaceDimension(name='k'),
'l': SpaceDimension(name='l'),
's': SpaceDimension(name='s'),
'q': SpaceDimension(name='q'),
'x': x, 'y': y, 'z': z}
@pytest.fixture(scope="module")
def iters(dims):
return [lambda ex: Iteration(ex, dims['i'], (0, 3, 1)),
lambda ex: Iteration(ex, dims['j'], (0, 5, 1)),
lambda ex: Iteration(ex, dims['k'], (0, 7, 1)),
lambda ex: Iteration(ex, dims['s'], (0, 4, 1)),
lambda ex: Iteration(ex, dims['q'], (0, 4, 1)),
lambda ex: Iteration(ex, dims['l'], (0, 6, 1)),
lambda ex: Iteration(ex, dims['x'], (0, 5, 1)),
lambda ex: Iteration(ex, dims['y'], (0, 7, 1)),
lambda ex: Iteration(ex, dims['z'], (0, 7, 1))]
@pytest.fixture(scope="module")
def exprs(dims):
a = Array(name='a', shape=(3,), dimensions=(dims["i"],)).indexify()
b = Array(name='b', shape=(3,), dimensions=(dims["i"],)).indexify()
return [Expression(DummyEq(a, a + b + 5.)),
Expression(DummyEq(a, b - a)),
Expression(DummyEq(a, 4 * (b * a))),
Expression(DummyEq(a, (6. / b) + (8. * a)))]
@pytest.fixture(scope="module")
def block1(exprs, iters):
# Perfect loop nest:
# for i
# for j
# for k
# expr0
return iters[0](iters[1](iters[2](exprs[0])))
@pytest.fixture(scope="module")
def block2(exprs, iters):
# Non-perfect simple loop nest:
# for i
# expr0
# for j
# for k
# expr1
return iters[0]([exprs[0], iters[1](iters[2](exprs[1]))])
@pytest.fixture(scope="module")
def block3(exprs, iters):
# Non-perfect non-trivial loop nest:
# for i
# for s
# expr0
# for j
# for k
# expr1
# expr2
# for p
# expr3
return iters[0]([iters[3](exprs[0]),
iters[1](iters[2]([exprs[1], exprs[2]])),
iters[4](exprs[3])])
@pytest.fixture(scope="module")
def block4(exprs, iters, dims):
# Non-perfect loop nest due to conditional
# for i
# if i % 2 == 0
# for j
return iters[0](Conditional(Eq(Mod(dims['i'], 2), 0), iters[1](exprs[0])))
def test_printAST(block1, block2, block3, block4):
str1 = printAST(block1)
assert str1 in """
<Iteration i::i::(0, 3, 1)>
<Iteration j::j::(0, 5, 1)>
<Iteration k::k::(0, 7, 1)>
<Expression a[i] = a[i] + b[i] + 5.0>
"""
str2 = printAST(block2)
assert str2 in """
<Iteration i::i::(0, 3, 1)>
<Expression a[i] = a[i] + b[i] + 5.0>
<Iteration j::j::(0, 5, 1)>
<Iteration k::k::(0, 7, 1)>
<Expression a[i] = -a[i] + b[i]>
"""
str3 = printAST(block3)
assert str3 in """
<Iteration i::i::(0, 3, 1)>
<Iteration s::s::(0, 4, 1)>
<Expression a[i] = a[i] + b[i] + 5.0>
<Iteration j::j::(0, 5, 1)>
<Iteration k::k::(0, 7, 1)>
<Expression a[i] = -a[i] + b[i]>
<Expression a[i] = 4*a[i]*b[i]>
<Iteration q::q::(0, 4, 1)>
<Expression a[i] = 8.0*a[i] + 6.0/b[i]>
"""
str4 = printAST(block4)
assert str4 in """
<Iteration i::i::(0, 3, 1)>
<If Eq(Mod(i, 2), 0)>
<Iteration j::j::(0, 5, 1)>
<Expression a[i] = a[i] + b[i] + 5.0>
"""
def test_create_cgen_tree(block1, block2, block3):
assert str(Callable('foo', block1, 'void', ()).ccode) == """\
void foo()
{
for (int i = 0; i <= 3; i += 1)
{
for (int j = 0; j <= 5; j += 1)
{
for (int k = 0; k <= 7; k += 1)
{
a[i] = a[i] + b[i] + 5.0F;
}
}
}
}"""
assert str(Callable('foo', block2, 'void', ()).ccode) == """\
void foo()
{
for (int i = 0; i <= 3; i += 1)
{
a[i] = a[i] + b[i] + 5.0F;
for (int j = 0; j <= 5; j += 1)
{
for (int k = 0; k <= 7; k += 1)
{
a[i] = -a[i] + b[i];
}
}
}
}"""
assert str(Callable('foo', block3, 'void', ()).ccode) == """\
void foo()
{
for (int i = 0; i <= 3; i += 1)
{
for (int s = 0; s <= 4; s += 1)
{
a[i] = a[i] + b[i] + 5.0F;
}
for (int j = 0; j <= 5; j += 1)
{
for (int k = 0; k <= 7; k += 1)
{
a[i] = -a[i] + b[i];
a[i] = 4*a[i]*b[i];
}
}
for (int q = 0; q <= 4; q += 1)
{
a[i] = 8.0F*a[i] + 6.0F/b[i];
}
}
}"""
def test_find_sections(exprs, block1, block2, block3):
finder = FindSections()
sections = finder.visit(block1)
assert len(sections) == 1
sections = finder.visit(block2)
assert len(sections) == 2
found = list(sections.values())
assert len(found[0]) == 1
assert len(found[1]) == 1
sections = finder.visit(block3)
assert len(sections) == 3
found = list(sections.values())
assert len(found[0]) == 1
assert len(found[1]) == 2
assert len(found[2]) == 1
def test_is_perfect_iteration(block1, block2, block3, block4):
checker = IsPerfectIteration()
assert checker.visit(block1) is True
assert checker.visit(block1.nodes[0]) is True
assert checker.visit(block1.nodes[0].nodes[0]) is True
assert checker.visit(block2) is False
assert checker.visit(block2.nodes[1]) is True
assert checker.visit(block2.nodes[1].nodes[0]) is True
assert checker.visit(block3) is False
assert checker.visit(block3.nodes[0]) is True
assert checker.visit(block3.nodes[1]) is True
assert checker.visit(block3.nodes[2]) is True
assert checker.visit(block4) is False
assert checker.visit(block4.nodes[0]) is False
assert checker.visit(block4.nodes[0].then_body) is True
def test_transformer_wrap(exprs, block1, block2, block3):
"""Basic transformer test that wraps an expression in comments"""
line1 = '// This is the opening comment'
line2 = '// This is the closing comment'
wrapper = lambda n: Block(c.Line(line1), n, c.Line(line2))
transformer = Transformer({exprs[0]: wrapper(exprs[0])})
for block in [block1, block2, block3]:
newblock = transformer.visit(block)
newcode = str(newblock.ccode)
oldnumlines = len(str(block.ccode).split('\n'))
newnumlines = len(newcode.split('\n'))
assert newnumlines >= oldnumlines + 2
assert line1 in newcode
assert line2 in newcode
assert "a[i] = a[i] + b[i] + 5.0F;" in newcode
def test_transformer_replace(exprs, block1, block2, block3):
"""Basic transformer test that replaces an expression"""
line1 = '// Replaced expression'
replacer = Block(c.Line(line1))
transformer = Transformer({exprs[0]: replacer})
for block in [block1, block2, block3]:
newblock = transformer.visit(block)
newcode = str(newblock.ccode)
oldnumlines = len(str(block.ccode).split('\n'))
newnumlines = len(newcode.split('\n'))
assert newnumlines >= oldnumlines
assert line1 in newcode
assert "a[i0] = a[i0] + b[i0] + 5.0F;" not in newcode
def test_transformer_replace_function_body(block1, block2):
"""Create a Function and replace its body with another."""
args = FindSymbols().visit(block1)
f = Callable('foo', block1, 'void', args)
assert str(f.ccode) == """void foo(float *restrict a_vec, float *restrict b_vec)
{
for (int i = 0; i <= 3; i += 1)
{
for (int j = 0; j <= 5; j += 1)
{
for (int k = 0; k <= 7; k += 1)
{
a[i] = a[i] + b[i] + 5.0F;
}
}
}
}"""
f = Transformer({block1: block2}).visit(f)
assert str(f.ccode) == """void foo(float *restrict a_vec, float *restrict b_vec)
{
for (int i = 0; i <= 3; i += 1)
{
a[i] = a[i] + b[i] + 5.0F;
for (int j = 0; j <= 5; j += 1)
{
for (int k = 0; k <= 7; k += 1)
{
a[i] = -a[i] + b[i];
}
}
}
}"""
def test_transformer_add_replace(exprs, block2, block3):
"""Basic transformer test that adds one expression and replaces another"""
line1 = '// Replaced expression'
line2 = '// Adding a simple line'
replacer = Block(c.Line(line1))
adder = lambda n: Block(c.Line(line2), n)
transformer = Transformer({exprs[0]: replacer,
exprs[1]: adder(exprs[1])})
for block in [block2, block3]:
newblock = transformer.visit(block)
newcode = str(newblock.ccode)
oldnumlines = len(str(block.ccode).split('\n'))
newnumlines = len(newcode.split('\n'))
assert newnumlines >= oldnumlines + 1
assert line1 in newcode
assert line2 in newcode
assert "a[i0] = a[i0] + b[i0] + 5.0F;" not in newcode
def test_nested_transformer(exprs, iters, block2):
"""When created with the kwarg ``nested=True``, a Transformer performs
nested replacements. This test simultaneously replace an inner expression
and an Iteration sorrounding it."""
target_loop = block2.nodes[1]
target_expr = target_loop.nodes[0].nodes[0]
mapper = {target_loop: iters[3](target_loop.nodes[0]),
target_expr: exprs[3]}
processed = Transformer(mapper, nested=True).visit(block2)
assert printAST(processed) == """<Iteration i::i::(0, 3, 1)>
<Expression a[i] = a[i] + b[i] + 5.0>
<Iteration s::s::(0, 4, 1)>
<Iteration k::k::(0, 7, 1)>
<Expression a[i] = 8.0*a[i] + 6.0/b[i]>"""
def test_find_symbols():
grid = Grid(shape=(4, 4))
x, y = grid.dimensions
f = Function(name='f', grid=grid)
op = Operator(Eq(f, f[x-1, y] + f[x+1, y] + 1))
symbols = FindSymbols('indexeds').visit(op)
assert len(symbols) == 3
def test_find_symbols_with_duplicates():
grid = Grid(shape=(4, 4))
x, y = grid.dimensions
f = TimeFunction(name='f', grid=grid)
g = Function(name='g', grid=grid)
eq = Eq(f.forward, sin(g)*f + g)
op = Operator(eq)
exprs = FindNodes(Expression).visit(op)
assert len(exprs) == 2
# Two syntactically identical indexeds r0[x, y], but they're different
# objects because they are constructed in two different places during
# the CIRE pass
r0a = exprs[0].output
r0b = exprs[1].expr.rhs.args[0].args[0]
assert r0a.function is r0b.function
assert r0a.name == r0b.name == "r0"
assert hash(r0a) == hash(r0b)
assert r0a is not r0b
# So we expect FindSymbols to catch five Indexeds in total
symbols = FindSymbols('indexeds').visit(op)
assert len(symbols) == 5
def test_map_nodes(block1):
"""
Tests MapNodes visitor. When MapNodes is created with mode='groupby',
matching ancestors are grouped together under a single key.
This can be useful, for example, when applying transformations to the
outermost Iteration containing a specific node.
"""
map_nodes = MapNodes(Iteration, Expression, mode='groupby').visit(block1)
assert len(map_nodes.keys()) == 1
for iters, (expr,) in map_nodes.items():
# Replace the outermost `Iteration` with a `Call`
callback = Callable('solver', iters[0], 'void', ())
processed = Transformer({iters[0]: Call(callback.name)}).visit(block1)
assert str(processed) == 'solver();'