Skip to content

Commit 144c4bb

Browse files
Merge pull request #1839 from devitocodes/fix-linearize-temps
compiler: Patch linearization pass
2 parents 7f15c89 + f791850 commit 144c4bb

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎devito/passes/iet/linearization.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,34 @@ def linearize_accesses(iet, key, cache, sregistry):
8282

8383
# For all unseen Functions, build the size exprs. For example:
8484
# `x_fsz0 = u_vec->size[1]`
85-
imapper = DefaultOrderedDict(list)
85+
imapper = DefaultOrderedDict(dict)
8686
for (d, halo, _), v in mapper.items():
8787
v_unseen = [f for f in v if f in functions_unseen]
8888
if not v_unseen:
8989
continue
9090
expr = _generate_fsz(v_unseen[0], d, sregistry)
9191
if expr:
9292
for f in v_unseen:
93-
imapper[f].append((d, expr.write))
93+
imapper[f][d] = expr.write
9494
cache[f].stmts0.append(expr)
9595

9696
# For all unseen Functions, build the stride exprs. For example:
9797
# `y_stride0 = y_fsz0*z_fsz0`
9898
built = {}
99-
mapper = DefaultOrderedDict(list)
99+
mapper = DefaultOrderedDict(dict)
100100
for f, v in imapper.items():
101-
for n, (d, _) in enumerate(v):
102-
expr = prod(list(zip(*v[n:]))[1])
101+
for d in v:
102+
n = f.dimensions.index(d)
103+
expr = prod(v[i] for i in f.dimensions[n:])
103104
try:
104105
stmt = built[expr]
105106
except KeyError:
106107
name = sregistry.make_name(prefix='%s_stride' % d.name)
107108
s = Symbol(name=name, dtype=np.uint32, is_const=True)
108109
stmt = built[expr] = DummyExpr(s, expr, init=True)
109-
mapper[f].append(stmt.write)
110+
mapper[f][d] = stmt.write
110111
cache[f].stmts1.append(stmt)
111-
mapper.update([(f, []) for f in functions_unseen if f not in mapper])
112+
mapper.update([(f, {}) for f in functions_unseen if f not in mapper])
112113

113114
# For all unseen Functions, build defines. For example:
114115
# `#define uL(t, x, y, z) u[(t)*t_stride0 + (x)*x_stride0 + (y)*y_stride0 + (z)]`
@@ -201,7 +202,8 @@ def _(f, szs, sregistry):
201202
pname = sregistry.make_name(prefix='%sL' % f.name)
202203
cbk = lambda i, pname=pname: FIndexed(i, pname)
203204

204-
expr = sum([MacroArgument(d.name)*s for d, s in zip(f.dimensions, szs)])
205+
expr = sum([MacroArgument(d0.name)*szs[d1]
206+
for d0, d1 in zip(f.dimensions, f.dimensions[1:])])
205207
expr += MacroArgument(f.dimensions[-1].name)
206208
expr = Indexed(IndexedData(f.name, None, f), expr)
207209
define = DefFunction(pname, f.dimensions)

‎tests/test_linearize.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,35 @@ def test_strides_forwarding1():
334334
assert len(bar.body.body) == 5
335335
assert bar.body.body[0].write.name == 'y_fsz0'
336336
assert bar.body.body[2].write.name == 'y_stride0'
337+
338+
339+
def test_issue_1838():
340+
"""
341+
MFE for issue #1838.
342+
"""
343+
space_order = 4
344+
345+
grid = Grid(shape=(4, 4, 4))
346+
347+
f = Function(name='f', grid=grid, space_order=space_order)
348+
b = Function(name='b', grid=grid, space_order=space_order)
349+
p0 = TimeFunction(name='p0', grid=grid, space_order=space_order)
350+
p1 = TimeFunction(name='p0', grid=grid, space_order=space_order)
351+
352+
f.data[:] = 2.1
353+
b.data[:] = 1.3
354+
p0.data[:, 2, 2, 2] = .3
355+
p1.data[:, 2, 2, 2] = .3
356+
357+
eq = Eq(p0.forward, (sin(b)*p0.dx).dx + (sin(b)*p0.dx).dy + (sin(b)*p0.dx).dz + p0)
358+
359+
op0 = Operator(eq)
360+
op1 = Operator(eq, opt=('advanced', {'linearize': True}))
361+
362+
op0.apply(time_M=3, dt=1.)
363+
op1.apply(time_M=3, dt=1., p0=p1)
364+
365+
# Check generated code
366+
assert "r4L0(x, y, z) r4[(x)*y_stride2 + (y)*z_stride1 + (z)]" in str(op1)
367+
368+
assert np.allclose(p0.data, p1.data, rtol=1e-6)

0 commit comments

Comments
 (0)