diff --git a/stride/physics/common/devito.py b/stride/physics/common/devito.py index 5159bba..78a98ad 100644 --- a/stride/physics/common/devito.py +++ b/stride/physics/common/devito.py @@ -543,10 +543,13 @@ def undersampled_time_derivative(self, fun, factor, time_bounds=None, offset=Non deriv_order=1, fd_order=1): offset = offset or (0, 0) - time_under, buffer_size = self._time_undersampled('time_under_d', factor, time_bounds, offset) - deriv = devito.Derivative(fun, (fun.dimensions[0], deriv_order), fd_order=fd_order) - deriv = deriv.xreplace({fun.dimensions[0]: time_under}) + + # Without an offset, keep the function's own undersampled time dimension. A new dimension + # leaves the old one's factor in the code that streams the function, with no value for it + if offset != (0, 0) or not fun.dimensions[0].is_Conditional: + time_under, buffer_size = self._time_undersampled('time_under_d', factor, time_bounds, offset) + deriv = deriv.xreplace({fun.dimensions[0]: time_under}) return deriv diff --git a/stride/physics/iso_acoustic/devito.py b/stride/physics/iso_acoustic/devito.py index 2983799..5dd08dc 100644 --- a/stride/physics/iso_acoustic/devito.py +++ b/stride/physics/iso_acoustic/devito.py @@ -411,7 +411,7 @@ def before_forward(self, wavelets, vp, rho=None, alpha=None, **kwargs): # ValueError: Cannot access `shape_allocated` as unfinalized - so no size estimate pass - if self._needs_grad(wavelets, rho, alpha, **kwargs): + if self._needs_grad(rho, alpha, **kwargs): p_saved_expr = p else: p_saved_expr = self._forward_save(p) @@ -727,7 +727,6 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k time_bounds = kwargs.get('time_bounds', (0, self.time.extended_num)) fw3d_mode = kwargs.pop('fw3d_mode', False) - fw3d_mode = True # TODO Force fw3d_mode pending Devito-side fix platform = kwargs.get('platform', 'cpu') is_nvidia = platform is not None and 'nvidia' in platform @@ -749,13 +748,11 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k # Create stencil stencil = self._stencil(p_a, wavelets, vp, rho=rho, alpha=alpha, direction='backward', **kwargs) - # Define the source injection function to generate the corresponding code - t = rec.time_dim + # Define the receiver injection, into p_a.backward after the stencil. Injecting into p_a + # before the stencil would be the same sum, but Devito then compiles a wrong time loop when + # an equation after the stencil reads p_a.backward (devitocodes/devito#2780) vp2 = self.dev_grid.vars.vp**2 - if not fw3d_mode: - rec_term = rec.inject(field=p_a, expr=-rec.subs({t: t-1}) * self.time.step**2 * vp2) - else: - rec_term = rec.inject(field=p_a.backward, expr=-rec * self.time.step**2 * vp2) + rec_term = rec.inject(field=p_a.backward, expr=-rec * self.time.step**2 * vp2) if wavelets.needs_grad: src_term = src.interpolate(expr=-p_a) @@ -792,12 +789,8 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k if self.attenuation_power == 2: kwargs['devito_config']['opt'] = 'noop' - if fw3d_mode: - self.adjoint_operator.set_operator(stencil + rec_term + src_term + gradient_update + update_saved, - **kwargs) - else: - self.adjoint_operator.set_operator(rec_term + stencil + src_term + gradient_update + update_saved, - **kwargs) + self.adjoint_operator.set_operator(stencil + rec_term + src_term + gradient_update + update_saved, + **kwargs) self.adjoint_operator.compile() else: @@ -875,6 +868,13 @@ def before_adjoint(self, adjoint_source, wavelets, vp, rho=None, alpha=None, **k self.dev_grid.vars.rec.data[:] = adjoint_source.T * window + # The forward records p.forward, one step after the field the adjoint operator is the exact + # transpose for, so delay the adjoint source by one step to make the adjoint exact. fw3d_mode + # keeps Fullwave3D's pairing, without the delay, unless shift_adjoint_source=True + if kwargs.get('shift_adjoint_source', not fw3d_mode): + self.dev_grid.vars.rec.data[1:] = self.dev_grid.vars.rec.data[:-1].copy() + self.dev_grid.vars.rec.data[0] = 0. + if self.interpolation_type == 'linear': self.dev_grid.vars.src.coordinates.data[:] = source_coordinates self.dev_grid.vars.rec.coordinates.data[:] = receiver_coordinates @@ -1013,22 +1013,26 @@ def prepare_grad_vp(self, vp, **kwargs): subdomain = abox if self.adaptive_boxes else interior wavelets, _, rho, alpha = kwargs.get('wrt') - if self._needs_grad(wavelets, rho, alpha): + if self._needs_grad(rho, alpha, **kwargs): p_dt = self._forward_save_undersampled(p, **kwargs) p_dt_fun = self.dev_grid.function('p_dt', space_order=0) - p_dt_update = (devito.Eq(p_dt_fun, p_dt, subdomain=subdomain),) + # p_dt_fun has no time dimension, so tie the updates to the saved time steps, as when + # p_saved holds the derivative; otherwise every time step adds the last saved value + implicit_dims = (p.time_dim,) + p_dt_update = (devito.Eq(p_dt_fun, p_dt, implicit_dims=implicit_dims, subdomain=subdomain),) else: p_dt = p p_dt_fun = p_dt p_dt_update = () + implicit_dims = None w = self._time_weights(**kwargs) grad = self.dev_grid.function('grad_vp', space_order=0) - grad_update = devito.Inc(grad, w * p_dt_fun * p_a, subdomain=subdomain) + grad_update = devito.Inc(grad, w * p_dt_fun * p_a, implicit_dims=implicit_dims, subdomain=subdomain) prec = self.dev_grid.function('prec_vp', space_order=0) - prec_update = devito.Inc(prec, w * p_dt_fun * p_dt_fun, subdomain=subdomain) + prec_update = devito.Inc(prec, w * p_dt_fun * p_dt_fun, implicit_dims=implicit_dims, subdomain=subdomain) return p_dt_update + (grad_update, prec_update) @@ -1706,7 +1710,7 @@ def _forward_save(self, field): def _forward_save_undersampled(self, field, **kwargs): return self.dev_grid.undersampled_time_derivative(field, self.undersampling_factor, - bounds=kwargs.get('time_bounds', None), + time_bounds=kwargs.get('time_bounds', None), deriv_order=2, fd_order=2) def _adjoint_save(self, field):