leto-ops 0.45.2

Math, reduction, SIMD, and parallel kernels for Leto arrays
Documentation
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! The 3-D finite-difference operator: its spacings, and the dispatch from a
//! stencil scheme to the stencil that implements it.
//!
//! The stencils live in [`super::central`], [`super::fourth_order`] and
//! [`super::staggered`]; this
//! drives them and checks the destination shape.

use core::ops::Range;

use eunomia::{NumericElement, RealField};
use leto::{ArrayView3, ArrayViewMut3, LetoError, Result};

use super::central::{
    central2_x_into, central2_y_into, central2_z_into, central6_x_into, central6_y_into,
    central6_z_into,
};
use super::checks::{assert_dst_shape, check_windows, no_destination};
use super::fourth_order::{central4_divergence_into, central4_into, central4_map_into};
use super::leapfrog::Axis;
use super::staggered::{
    staggered_backward_x_into, staggered_backward_y_into, staggered_backward_z_into,
    staggered_forward_x_into, staggered_forward_y_into, staggered_forward_z_into,
};
use super::window::{PlaneWindow, PlaneWindowMut};
use super::FiniteDifference3DScheme;

/// Generic 3-D finite-difference operator acting on `u(x, y, z)`.
#[derive(Debug, Clone, Copy)]
pub struct FiniteDifference3D<T> {
    scheme: FiniteDifference3DScheme,
    dx: T,
    dy: T,
    dz: T,
}

impl<T: RealField> FiniteDifference3D<T> {
    /// Construct an operator from a stencil scheme and per-axis spacings.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when any of `dx`, `dy`, `dz` is not
    ///   strictly positive.
    pub fn new(scheme: FiniteDifference3DScheme, dx: T, dy: T, dz: T) -> Result<Self> {
        let zero = <T as NumericElement>::ZERO;
        if dx <= zero || dy <= zero || dz <= zero {
            return Err(LetoError::InvalidInput(
                "FiniteDifference3D: dx, dy, dz must all be strictly positive".into(),
            ));
        }
        Ok(Self { scheme, dx, dy, dz })
    }

    /// 2nd-order central-difference scheme.
    pub fn central_second_order(dx: T, dy: T, dz: T) -> Result<Self> {
        Self::new(FiniteDifference3DScheme::CentralSecondOrder, dx, dy, dz)
    }

    /// 4th-order central-difference scheme.
    pub fn central_fourth_order(dx: T, dy: T, dz: T) -> Result<Self> {
        Self::new(FiniteDifference3DScheme::CentralFourthOrder, dx, dy, dz)
    }

    /// 6th-order central-difference scheme.
    pub fn central_sixth_order(dx: T, dy: T, dz: T) -> Result<Self> {
        Self::new(FiniteDifference3DScheme::CentralSixthOrder, dx, dy, dz)
    }

    /// Yee staggered forward scheme.
    pub fn staggered_forward(dx: T, dy: T, dz: T) -> Result<Self> {
        Self::new(FiniteDifference3DScheme::StaggeredForward, dx, dy, dz)
    }

    /// Yee staggered backward scheme (kwavers-side convention).
    pub fn staggered_backward(dx: T, dy: T, dz: T) -> Result<Self> {
        Self::new(FiniteDifference3DScheme::StaggeredBackward, dx, dy, dz)
    }

    /// Returns the configured stencil scheme.
    #[must_use]
    pub fn scheme(&self) -> FiniteDifference3DScheme {
        self.scheme
    }

    /// Returns the per-axis grid spacing `(dx, dy, dz)`.
    #[must_use]
    pub fn spacing(&self) -> (T, T, T) {
        (self.dx, self.dy, self.dz)
    }

    /// Stencil width = number of grid points used by the interior kernel.
    #[must_use]
    pub fn stencil_width(&self) -> usize {
        match self.scheme {
            FiniteDifference3DScheme::CentralSecondOrder => 3,
            FiniteDifference3DScheme::CentralFourthOrder => 5,
            FiniteDifference3DScheme::CentralSixthOrder => 7,
            FiniteDifference3DScheme::StaggeredForward
            | FiniteDifference3DScheme::StaggeredBackward => 2,
        }
    }

    /// Apply ∂/∂x into a pre-allocated destination.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when the diff axis has fewer than the
    ///   minimum required points for the chosen scheme, or when the dst shape
    ///   does not match the scheme's documented contract.
    pub fn apply_x_into(&self, field: ArrayView3<T>, dst: &mut ArrayViewMut3<'_, T>) -> Result<()> {
        let [nx, ny, nz] = field.shape();
        match self.scheme {
            FiniteDifference3DScheme::CentralSecondOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central2_x_into(field, dst, nx, ny, nz, self.dx)
            }
            FiniteDifference3DScheme::CentralFourthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central4_into(field, dst, Axis::X, self.dx)
            }
            FiniteDifference3DScheme::CentralSixthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central6_x_into(field, dst, nx, ny, nz, self.dx)
            }
            FiniteDifference3DScheme::StaggeredForward => {
                assert_dst_shape(&dst.shape(), &[nx - 1, ny, nz])?;
                staggered_forward_x_into(field, dst, nx, ny, nz, self.dx)
            }
            FiniteDifference3DScheme::StaggeredBackward => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                staggered_backward_x_into(field, dst, nx, ny, nz, self.dx)
            }
        }
    }

    /// Sum the three axis derivatives of three fields into one destination:
    /// `dst = ∂fields[0]/∂x + ∂fields[1]/∂y + ∂fields[2]/∂z`.
    ///
    /// The fused form reads each field once per output lane instead of
    /// writing three per-axis buffers a caller then adds, which is where a
    /// divergence spends its time once the stencils are lane-swept: at 64
    /// cubed it moves 8 MB against the composed form's 20 MB. Values are
    /// bit-identical to applying each axis separately and summing in x, y, z
    /// order.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when the fields and `dst` do not share
    ///   one shape, or when the scheme is not `CentralFourthOrder`. The other
    ///   schemes have no fused kernel yet; compose [`Self::apply_x_into`] and
    ///   its siblings and sum, or extend `fourth_order` the same way.
    pub fn divergence_into(
        &self,
        fields: [ArrayView3<'_, T>; 3],
        dst: &mut ArrayViewMut3<'_, T>,
    ) -> Result<()> {
        let shape = fields[0].shape();
        for field in &fields[1..] {
            assert_dst_shape(&field.shape(), &shape)?;
        }
        assert_dst_shape(&dst.shape(), &shape)?;
        match self.scheme {
            FiniteDifference3DScheme::CentralFourthOrder => {
                central4_divergence_into(fields, dst, [self.dx, self.dy, self.dz])
            }
            other => Err(LetoError::InvalidInput(format!(
                "divergence_into has no fused kernel for {other:?}; apply each \
                 axis separately and sum"
            ))),
        }
    }

    /// Combine several axis derivatives and pointwise fields into one
    /// destination in a single pass:
    /// `dst[i] = combine(derivatives, values, dst[i])`, where
    /// `derivatives[j]` is `∂terms[j].1/∂terms[j].0` at that lane,
    /// `values[k]` is `pointwise[k]` there, and the last argument is the
    /// value `dst` held before the pass.
    ///
    /// A result that replaces the destination ignores that argument; one
    /// that updates it -- a velocity advanced by the acceleration these
    /// derivatives assemble -- reads it here rather than as a pointwise
    /// input, which could not alias the destination. Ignored, it is never
    /// loaded: `combine` is inlined into the kernel and the load is dead.
    ///
    /// [`Self::divergence_into`] is the sum of three axis derivatives; this
    /// serves the combinations that are not a sum. An elastic shear stress
    /// `μ (∂u/∂b + ∂v/∂a)` is `N = 2, M = 1`: it reads two fields and the
    /// scale once per lane, where sweeping each axis into a buffer and
    /// scaling the sum moves twice the traffic. Each derivative is the value
    /// the corresponding [`Self::apply_x_into`] sweep would have written.
    ///
    /// Terms name distinct axes in the cases this serves; a repeated axis is
    /// read twice and both values are handed to `combine`.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when the fields, the pointwise inputs
    ///   and `dst` do not share one shape, or when the scheme is not
    ///   `CentralFourthOrder`. The other schemes have no fused kernel yet;
    ///   sweep each axis separately and combine, or extend `fourth_order` the
    ///   same way.
    pub fn map_axis_derivatives<const N: usize, const M: usize, F>(
        &self,
        terms: [(Axis, ArrayView3<'_, T>); N],
        pointwise: [ArrayView3<'_, T>; M],
        dst: &mut ArrayViewMut3<'_, T>,
        combine: F,
    ) -> Result<()>
    where
        F: Fn([T; N], [T; M], T) -> T + Send + Sync,
    {
        self.map_axis_derivatives_many(terms, pointwise, [dst], |derivatives, values, [held]| {
            [combine(derivatives, values, held)]
        })
    }

    /// [`Self::map_axis_derivatives`] writing `K` destinations from one
    /// derivative pass: `combine` receives each destination's value and
    /// returns the value for each.
    ///
    /// Where several results read the same axis derivatives -- the diagonal
    /// of an elastic stress tensor reads the same three normal strains, and
    /// all six stresses read the nine displacement gradients -- this sweeps
    /// the stencils once and writes every result, where a call per
    /// destination sweeps them again and re-reads every field. At 64 cubed
    /// the diagonal alone is 16 MB of traffic this way against 36 MB for
    /// three single-destination calls. Any `K` is one parallel region.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when the fields, the pointwise inputs
    ///   and the destinations do not share one shape, when `K` is zero, or
    ///   when the scheme is not `CentralFourthOrder`. The other schemes have
    ///   no fused kernel yet; sweep each axis separately and combine, or
    ///   extend `fourth_order` the same way.
    pub fn map_axis_derivatives_many<const N: usize, const M: usize, const K: usize, F>(
        &self,
        terms: [(Axis, ArrayView3<'_, T>); N],
        pointwise: [ArrayView3<'_, T>; M],
        dst: [&mut ArrayViewMut3<'_, T>; K],
        combine: F,
    ) -> Result<()>
    where
        F: Fn([T; N], [T; M], [T; K]) -> [T; K] + Send + Sync,
    {
        let Some(first) = dst.first() else {
            return Err(no_destination());
        };
        let shape = first.shape();
        for destination in &dst[1..] {
            assert_dst_shape(&destination.shape(), &shape)?;
        }
        for (_, field) in &terms {
            assert_dst_shape(&field.shape(), &shape)?;
        }
        for field in &pointwise {
            assert_dst_shape(&field.shape(), &shape)?;
        }
        self.map_axis_derivatives_in_windows(
            shape[0],
            0..shape[0],
            terms.map(|(axis, field)| (axis, PlaneWindow::whole(field))),
            pointwise.map(PlaneWindow::whole),
            dst.map(PlaneWindowMut::whole),
            combine,
        )
    }

    /// [`Self::map_axis_derivatives_many`] over the grid planes `planes` of a
    /// grid of `grid_planes` x-planes, where each field and each destination
    /// hold only a window of those planes. The destinations hold the same
    /// planes.
    ///
    /// A derivative at grid plane `x` reads the planes its stencil reaches
    /// and takes the stencil the grid's extent gives `x`, whatever window
    /// holds the field, so each plane written is bit-identical to the same
    /// plane of a whole-grid call on the same values. Only `planes` is
    /// written.
    ///
    /// That is what lets a chain of passes run a slab of planes at a time
    /// through buffers the size of a slab. A consumer that writes
    /// intermediates and reads them back -- stresses, then their divergence
    /// -- keeps the intermediates in a window it reuses for every slab, so
    /// they stay in cache instead of making a grid-sized round trip through
    /// DRAM; see [`PlaneWindow`].
    ///
    /// # Errors
    /// [`LetoError::InvalidInput`] when
    /// - `K` is zero, or the destinations do not hold the same planes in one
    ///   shape;
    /// - `planes` runs backwards or past `grid_planes`, or a window reaches
    ///   past `grid_planes`;
    /// - a field or pointwise input differs from the destinations in its
    ///   second or third extent;
    /// - a destination, a pointwise input, or a y- or z-derivative field does
    ///   not hold every plane in `planes`, or an x-derivative field does not
    ///   hold every plane its stencil reaches from them;
    /// - the scheme is not `CentralFourthOrder`.
    pub fn map_axis_derivatives_in_windows<const N: usize, const M: usize, const K: usize, F>(
        &self,
        grid_planes: usize,
        planes: Range<usize>,
        terms: [(Axis, PlaneWindow<'_, T>); N],
        pointwise: [PlaneWindow<'_, T>; M],
        dst: [PlaneWindowMut<'_, '_, T>; K],
        combine: F,
    ) -> Result<()>
    where
        F: Fn([T; N], [T; M], [T; K]) -> [T; K] + Send + Sync,
    {
        let destinations = dst
            .each_ref()
            .map(|window| (window.planes(), window.shape()));
        let Some(held) = destinations.first() else {
            return Err(no_destination());
        };
        if destinations.iter().any(|destination| destination != held) {
            return Err(LetoError::InvalidInput(format!(
                "FiniteDifference3D: every destination must hold the same planes in one \
                 shape, not {destinations:?}"
            )));
        }
        check_windows(grid_planes, &planes, held, &terms, &pointwise)?;
        match self.scheme {
            FiniteDifference3DScheme::CentralFourthOrder => central4_map_into(
                terms,
                pointwise,
                dst,
                grid_planes,
                planes,
                [self.dx, self.dy, self.dz],
                combine,
            ),
            other => Err(LetoError::InvalidInput(format!(
                "map_axis_derivatives has no fused kernel for {other:?}; sweep \
                 each axis separately and combine"
            ))),
        }
    }

    /// Apply ∂/∂y into a pre-allocated destination.
    /// # Errors
    /// See [`Self::apply_x_into`].
    pub fn apply_y_into(&self, field: ArrayView3<T>, dst: &mut ArrayViewMut3<'_, T>) -> Result<()> {
        let [nx, ny, nz] = field.shape();
        match self.scheme {
            FiniteDifference3DScheme::CentralSecondOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central2_y_into(field, dst, nx, ny, nz, self.dy)
            }
            FiniteDifference3DScheme::CentralFourthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central4_into(field, dst, Axis::Y, self.dy)
            }
            FiniteDifference3DScheme::CentralSixthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central6_y_into(field, dst, nx, ny, nz, self.dy)
            }
            FiniteDifference3DScheme::StaggeredForward => {
                assert_dst_shape(&dst.shape(), &[nx, ny - 1, nz])?;
                staggered_forward_y_into(field, dst, nx, ny, nz, self.dy)
            }
            FiniteDifference3DScheme::StaggeredBackward => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                staggered_backward_y_into(field, dst, nx, ny, nz, self.dy)
            }
        }
    }

    /// Apply ∂/∂z into a pre-allocated destination.
    /// # Errors
    /// See [`Self::apply_x_into`].
    pub fn apply_z_into(&self, field: ArrayView3<T>, dst: &mut ArrayViewMut3<'_, T>) -> Result<()> {
        let [nx, ny, nz] = field.shape();
        match self.scheme {
            FiniteDifference3DScheme::CentralSecondOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central2_z_into(field, dst, nx, ny, nz, self.dz)
            }
            FiniteDifference3DScheme::CentralFourthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central4_into(field, dst, Axis::Z, self.dz)
            }
            FiniteDifference3DScheme::CentralSixthOrder => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                central6_z_into(field, dst, nx, ny, nz, self.dz)
            }
            FiniteDifference3DScheme::StaggeredForward => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz - 1])?;
                staggered_forward_z_into(field, dst, nx, ny, nz, self.dz)
            }
            FiniteDifference3DScheme::StaggeredBackward => {
                assert_dst_shape(&dst.shape(), &[nx, ny, nz])?;
                staggered_backward_z_into(field, dst, nx, ny, nz, self.dz)
            }
        }
    }

    /// Apply the transpose of ∂/∂x into a pre-allocated destination.
    ///
    /// For a forward sweep `y = A f`, this maps an upstream lane `u` shaped
    /// like `y` to `v = Aᵀ u` shaped like `f`: the gradient of `⟨u, A f⟩`
    /// with respect to `f`, which is what an autograd backward pass needs.
    /// A forward sweep shrinks the grid, so its adjoint fans back out.
    ///
    /// # Errors
    /// - [`LetoError::InvalidInput`] when the diff axis has fewer than the
    ///   minimum required points for the chosen scheme, or when the upstream
    ///   shape does not match the scheme's forward output shape.
    pub fn adjoint_x_into(
        &self,
        upstream: ArrayView3<T>,
        grad: &mut ArrayViewMut3<'_, T>,
    ) -> Result<()> {
        super::adjoint::adjoint_into(self.scheme, Axis::X, upstream, grad, self.dx)
    }

    /// Apply the transpose of ∂/∂y into a pre-allocated destination.
    /// # Errors
    /// See [`Self::adjoint_x_into`].
    pub fn adjoint_y_into(
        &self,
        upstream: ArrayView3<T>,
        grad: &mut ArrayViewMut3<'_, T>,
    ) -> Result<()> {
        super::adjoint::adjoint_into(self.scheme, Axis::Y, upstream, grad, self.dy)
    }

    /// Apply the transpose of ∂/∂z into a pre-allocated destination.
    /// # Errors
    /// See [`Self::adjoint_x_into`].
    pub fn adjoint_z_into(
        &self,
        upstream: ArrayView3<T>,
        grad: &mut ArrayViewMut3<'_, T>,
    ) -> Result<()> {
        super::adjoint::adjoint_into(self.scheme, Axis::Z, upstream, grad, self.dz)
    }
}