-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathtest_skewing.py
174 lines (152 loc) · 7.67 KB
/
test_skewing.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
import pytest
import numpy as np
from conftest import assert_blocking
from devito import Grid, Dimension, Eq, Function, TimeFunction, Operator, norm, Min # noqa
from devito.ir import Expression, Iteration, FindNodes
class TestCodeGenSkewing:
'''
Test code generation with blocking+skewing, tests adapted from test_operator.py
'''
@pytest.mark.parametrize('expr, expected, norm_u, norm_v', [
(['Eq(u.forward, u + 1)',
'Eq(u[t1,x-time+1,y-time+1,z+1],u[t0,x-time+1,y-time+1,z+1]+1)',
np.sqrt((16*16*16)*6**2 + (16*16*16)*5**2), 0]),
(['Eq(u.forward, v + 1)',
'Eq(u[t1,x-time+1,y-time+1,z+1],v[t0,x-time+1,y-time+1,z+1]+1)',
np.sqrt((16*16*16)*1**2 + (16*16*16)*1**2), 0]),
(['Eq(u, v + 1)',
'Eq(u[t0,x-time+1,y-time+1,z+1],v[t0,x-time+1,y-time+1,z+1]+1)',
np.sqrt((16*16*16)*1**2 + (16*16*16)*1**2), 0]),
])
def test_skewed_bounds(self, expr, expected, norm_u, norm_v):
"""Tests code generation on skewed indices."""
grid = Grid(shape=(16, 16, 16))
x, y, z = grid.dimensions
time = grid.time_dim
u = TimeFunction(name='u', grid=grid) # noqa
v = TimeFunction(name='v', grid=grid) # noqa
eqn = eval(expr)
# List comprehension would need explicit locals/globals mappings to eval
op = Operator(eqn, opt=('blocking', {'skewing': True}))
op.apply(time_M=5)
iters = FindNodes(Iteration).visit(op)
time_iter = [i for i in iters if i.dim.is_Time]
assert len(time_iter) == 1
bns, _ = assert_blocking(op, {'x0_blk0'})
iters = FindNodes(Iteration).visit(bns['x0_blk0'])
assert len(iters) == 5
assert iters[0].dim.parent is x
assert iters[1].dim.parent is y
assert iters[4].dim is z
assert iters[2].dim.parent is iters[0].dim
assert iters[3].dim.parent is iters[1].dim
assert iters[0].symbolic_min == (iters[0].dim.parent.symbolic_min + time)
assert iters[0].symbolic_max == (iters[0].dim.parent.symbolic_max + time)
assert iters[1].symbolic_min == (iters[1].dim.parent.symbolic_min + time)
assert iters[1].symbolic_max == (iters[1].dim.parent.symbolic_max + time)
assert iters[2].symbolic_min == iters[2].dim.symbolic_min
assert iters[2].symbolic_max == Min(iters[0].dim + iters[0].dim.symbolic_incr
- 1, iters[0].dim.symbolic_max + time)
assert iters[3].symbolic_min == iters[3].dim.symbolic_min
assert iters[3].symbolic_max == Min(iters[1].dim + iters[1].dim.symbolic_incr
- 1, iters[1].dim.symbolic_max + time)
assert iters[4].symbolic_min == (iters[4].dim.symbolic_min)
assert iters[4].symbolic_max == (iters[4].dim.symbolic_max)
skewed = [i.expr for i in FindNodes(Expression).visit(bns['x0_blk0'])]
assert str(skewed[0]).replace(' ', '') == expected
assert np.isclose(norm(u), norm_u, rtol=1e-5)
assert np.isclose(norm(v), norm_v, rtol=1e-5)
u.data[:] = 0
v.data[:] = 0
op2 = Operator(eqn, opt=('advanced'))
op2.apply(time_M=5)
assert np.isclose(norm(u), norm_u, rtol=1e-5)
assert np.isclose(norm(v), norm_v, rtol=1e-5)
'''
Test code generation with skewing, tests adapted from test_operator.py
'''
@pytest.mark.parametrize('expr, expected', [
(['Eq(u, u + 1)',
'Eq(u[x+1,y+1,z+1],u[x+1,y+1,z+1]+1)']),
(['Eq(u, v + 1)',
'Eq(u[x+1,y+1,z+1],v[x+1,y+1,z+1]+1)']),
(['Eq(u, v + 1)',
'Eq(u[x+1,y+1,z+1],v[x+1,y+1,z+1]+1)']),
])
def test_no_sequential(self, expr, expected):
"""Tests code generation on skewed indices."""
grid = Grid(shape=(16, 16, 16))
x, y, z = grid.dimensions
u = Function(name='u', grid=grid) # noqa
v = Function(name='v', grid=grid) # noqa
eqn = eval(expr)
# List comprehension would need explicit locals/globals mappings to eval
op = Operator(eqn, opt=('blocking', {'skewing': True}))
op.apply()
iters = FindNodes(Iteration).visit(op)
assert len([i for i in iters if i.dim.is_Time]) == 0
assert_blocking(op, {}) # no blocking is expected in the absence of time
iters = FindNodes(Iteration).visit(op)
assert len(iters) == 3
assert iters[0].dim is x
assert iters[1].dim is y
assert iters[2].dim is z
skewed = [i.expr for i in FindNodes(Expression).visit(op)]
assert str(skewed[0]).replace(' ', '') == expected
'''
Test code generation with skewing only
'''
@pytest.mark.parametrize('expr, expected, skewing, blockinner', [
(['Eq(u.forward, u + 1)',
'Eq(u[t1,x-time+1,y-time+1,z+1],u[t0,x-time+1,y-time+1,z+1]+1)', True, False]),
(['Eq(u.forward, v + 1)',
'Eq(u[t1,x-time+1,y-time+1,z+1],v[t0,x-time+1,y-time+1,z+1]+1)', True, False]),
(['Eq(u, v + 1)',
'Eq(u[t0,x-time+1,y-time+1,z+1],v[t0,x-time+1,y-time+1,z+1]+1)', True, False]),
(['Eq(u, v + 1)',
'Eq(u[t0,x+1,y+1,z+1],v[t0,x+1,y+1,z+1]+1)', False, False]),
(['Eq(u, v + 1)',
'Eq(u[t0,x-time+1,y-time+1,z-time+1],v[t0,x-time+1,y-time+1,z-time+1]+1)',
True, True]),
])
def test_skewing_codegen(self, expr, expected, skewing, blockinner):
"""Tests code generation on skewed indices."""
grid = Grid(shape=(16, 16, 16))
x, y, z = grid.dimensions
time = grid.time_dim
u = TimeFunction(name='u', grid=grid) # noqa
v = TimeFunction(name='v', grid=grid) # noqa
eqn = eval(expr)
# List comprehension would need explicit locals/globals mappings to eval
op = Operator(eqn, opt=('blocking', {'blocklevels': 0, 'skewing': skewing,
'blockinner': blockinner}))
op.apply(time_M=5)
iters = FindNodes(Iteration).visit(op)
assert len(iters) == 4
assert iters[0].dim is time
assert iters[1].dim is x
assert iters[2].dim is y
assert iters[3].dim is z
skewed = [i.expr for i in FindNodes(Expression).visit(op)]
if skewing and not blockinner:
assert iters[1].symbolic_min == (iters[1].dim.symbolic_min + time)
assert iters[1].symbolic_max == (iters[1].dim.symbolic_max + time)
assert iters[2].symbolic_min == (iters[2].dim.symbolic_min + time)
assert iters[2].symbolic_max == (iters[2].dim.symbolic_max + time)
assert iters[3].symbolic_min == (iters[3].dim.symbolic_min)
assert iters[3].symbolic_max == (iters[3].dim.symbolic_max)
elif skewing and blockinner:
assert iters[1].symbolic_min == iters[1].dim.symbolic_min + time
assert iters[1].symbolic_max == iters[1].dim.symbolic_max + time
assert iters[2].symbolic_min == iters[2].dim.symbolic_min + time
assert iters[2].symbolic_max == iters[2].dim.symbolic_max + time
assert iters[3].symbolic_min == iters[3].dim.symbolic_min + time
assert iters[3].symbolic_max == iters[3].dim.symbolic_max + time
elif not skewing and not blockinner:
assert iters[1].symbolic_min == iters[1].dim.symbolic_min
assert iters[1].symbolic_max == iters[1].dim.symbolic_max
assert iters[2].symbolic_min == iters[2].dim.symbolic_min
assert iters[2].symbolic_max == iters[2].dim.symbolic_max
assert iters[3].symbolic_min == iters[3].dim.symbolic_min
assert iters[3].symbolic_max == iters[3].dim.symbolic_max
assert str(skewed[0]).replace(' ', '') == expected