diff --git a/devito/ir/clusters/algorithms.py b/devito/ir/clusters/algorithms.py index 93b0c851ec..34d5f10b70 100644 --- a/devito/ir/clusters/algorithms.py +++ b/devito/ir/clusters/algorithms.py @@ -237,6 +237,14 @@ def callback(self, clusters, prefix, backlog=None, known_break=None): if d.root in scope.d_anti_logical.cause} if maybe_break: idir.update({d: Forward for d in candidates if d.root in scope.d_flow.cause}) + if is_sub: + # The Clusters cannot be scheduled over different IterationSpaces + # (see above). The dependences above are computed as if `dim` ran + # Forward, so they do not settle the direction; the Clusters' + # dependences on themselves do + direction = self._recurrence_direction(clusters, dim) + if direction is not None: + idir.update({d: direction for d in candidates}) # Default to forward for remaining dimensions idir.update({d: Forward for d in candidates if d not in idir}) @@ -262,6 +270,50 @@ def callback(self, clusters, prefix, backlog=None, known_break=None): return processed + self.callback(backlog, prefix) + @staticmethod + def _recurrence_direction(clusters, dim): + """ + Infer the iteration direction along `dim` from the dependences of each + Cluster on itself. + + The dependences are computed as if `dim` ran Forward. A Cluster whose + carried self-dependences are all flow dependences, e.g. `u[t+1] = f(u[t])`, + requires Forward. A Cluster whose carried self-dependences are all anti + dependences, e.g. `u[t-1] = f(u[t])`, requires Backward. Reductions, and + Clusters without carried self-dependences, impose no direction. + + Parameters + ---------- + clusters : list of Cluster + The Clusters sharing the iteration Dimension `dim`. + dim : Dimension + The iteration Dimension. + + Returns + ------- + IterationDirection or None + The direction required by all Clusters that require one, or None if + no Cluster requires one, if they require different ones, or if a + Cluster has both carried flow and carried anti self-dependences. + """ + def is_carried(dependences): + return any(d.is_carried(dim) and not d.is_reduce(dim) for d in dependences) + + required = set() + for c in clusters: + scope = Scope(c.exprs) + flow = is_carried(scope.d_flow) + anti = is_carried(scope.d_anti_logical) + + if flow and anti: + return None + elif flow: + required.add(Forward) + elif anti: + required.add(Backward) + + return required.pop() if len(required) == 1 else None + def _break_for_parallelism(self, scope, dim, timestamp): candidates = dim._defines diff --git a/tests/test_operator.py b/tests/test_operator.py index b87a9c02d3..db0bfde6e8 100644 --- a/tests/test_operator.py +++ b/tests/test_operator.py @@ -2095,6 +2095,46 @@ def test_no_time_fission_reversing_war(self): assert np.allclose(u1.data, r1) assert np.allclose(u2.data, r2) + def test_no_time_fission_backward_recurrence(self): + """ + Update `u[t]`, step `u.backward`, then read `u.backward`, as in an adjoint + with a source injection before the stencil and a gradient update after + it. Computed as if `time` ran Forward, the dependences are both flow and + anti; the time loop cannot be split (sub-iterators), and the only Cluster + with carried self-dependences, `u[t-1] = f(u[t], u[t+1])`, requires + Backward. Hence a single, backward time loop is expected (issue #2780). + """ + nt = 8 + grid = Grid((4, 4)) + time = grid.time_dim + + u = TimeFunction(name='u', grid=grid, time_order=2) + f = Function(name='f', grid=grid) + + eqns = [Eq(u, u + 1.0), + Eq(u.backward, 2*u - u.forward), + Inc(f, u.backward)] + + op = Operator(eqns, opt=('noop', {'openmp': False})) + trees = retrieve_iteration_tree(op) + assert len({i[0] for i in trees}) == 1 + assert trees[0][0].dim is time + assert trees[0][0].direction is Backward + + op.apply(time_m=1, time_M=nt) + + # Reference: the same equations, in order, one time step at a time + r = np.zeros((3, 4, 4), dtype=grid.dtype) + rf = np.zeros((4, 4), dtype=grid.dtype) + for t in range(nt, 0, -1): + tm1, t0, tp1 = (t - 1) % 3, t % 3, (t + 1) % 3 + r[t0] += 1.0 + r[tm1] = 2*r[t0] - r[tp1] + rf += r[tm1] + + assert np.allclose(u.data, r) + assert np.allclose(f.data, rf) + def test_no_time_fission_splitting_temporaries(self): """ The time loop must not be split between a sparse function's position