use core::ops::Range;
use eunomia::{NumericElement, RealField};
use super::super::leapfrog::Axis;
use super::stencil::{Scales, Stencil, ELEMENTS_PER_UNIT};
use crate::infrastructure::parallel::{for_each_plane_mut, for_each_plane_mut_many_with};
pub(super) fn sweep_dense<T>(
source: &[T],
out: &mut [T],
shape: [usize; 3],
axis: Axis,
scales: Scales<T>,
) where
T: RealField,
{
let [nx, ny, nz] = shape;
let plane_len = ny * nz;
let element_bytes = ELEMENTS_PER_UNIT * size_of::<T>();
for_each_plane_mut(out, plane_len, element_bytes, |x, out_plane| {
let plane_at = |i: usize| &source[i * plane_len..(i + 1) * plane_len];
match axis {
Axis::X => Stencil::at(x, nx)
.apply_lane(scales, out_plane, |o| plane_at(x.wrapping_add_signed(o))),
Axis::Y => {
let plane = plane_at(x);
for (y, out_row) in out_plane.chunks_exact_mut(nz).enumerate() {
Stencil::at(y, ny).apply_lane(scales, out_row, |o| {
let row = y.wrapping_add_signed(o);
&plane[row * nz..(row + 1) * nz]
});
}
}
Axis::Z => {
let plane = plane_at(x);
for (out_row, row) in out_plane.chunks_exact_mut(nz).zip(plane.chunks_exact(nz)) {
sweep_row(scales, out_row, row);
}
}
}
});
}
pub(super) fn divergence_dense<T>(
fields: [&[T]; 3],
out: &mut [T],
shape: [usize; 3],
scales: [Scales<T>; 3],
) where
T: RealField,
{
let [nx, ny, nz] = shape;
let plane_len = ny * nz;
let element_bytes = (1 + 3 * (ELEMENTS_PER_UNIT - 1)) * size_of::<T>();
for_each_plane_mut(out, plane_len, element_bytes, |x, out_plane| {
let x_stencil = Stencil::at(x, nx);
for (y, out_row) in out_plane.chunks_exact_mut(nz).enumerate() {
x_stencil.apply_lane(scales[0], out_row, |o| {
let base = x.wrapping_add_signed(o) * plane_len + y * nz;
&fields[0][base..base + nz]
});
Stencil::at(y, ny).add_lane(scales[1], out_row, |o| {
let base = x * plane_len + y.wrapping_add_signed(o) * nz;
&fields[1][base..base + nz]
});
let base = x * plane_len + y * nz;
add_row(scales[2], out_row, &fields[2][base..base + nz]);
}
});
}
#[derive(Clone, Copy)]
pub(super) struct DenseTerm<'a, T> {
pub(super) axis: Axis,
pub(super) field: &'a [T],
pub(super) origin: usize,
pub(super) scales: Scales<T>,
}
pub(super) fn map_dense<T, const N: usize, const M: usize, const K: usize, F>(
terms: [DenseTerm<'_, T>; N],
pointwise: [(&[T], usize); M],
(out, out_origin): ([&mut [T]; K], usize),
shape: [usize; 3],
planes: Range<usize>,
combine: &F,
) where
T: RealField,
F: Fn([T; N], [T; M], [T; K]) -> [T; K] + Send + Sync,
{
let [nx, ny, nz] = shape;
let plane_len = ny * nz;
let first_plane = planes.start;
let written = (planes.start - out_origin) * plane_len..(planes.end - out_origin) * plane_len;
let element_bytes = (K + N * (ELEMENTS_PER_UNIT - 1) + M) * size_of::<T>();
for_each_plane_mut_many_with(
out.map(|destination| &mut destination[written.clone()]),
plane_len,
element_bytes,
|| vec![<T as NumericElement>::ZERO; N * nz],
|scratch, offset, out_planes| {
let x = first_plane + offset;
let mut out_rows = out_planes.map(|plane| plane.chunks_exact_mut(nz));
for y in 0..ny {
for (term, values) in terms.iter().zip(scratch.chunks_exact_mut(nz)) {
let DenseTerm {
axis,
field,
origin,
scales,
} = *term;
match axis {
Axis::X => Stencil::at(x, nx).apply_lane(scales, values, |o| {
let plane = (x.wrapping_add_signed(o) - origin) * plane_len + y * nz;
&field[plane..plane + nz]
}),
Axis::Y => Stencil::at(y, ny).apply_lane(scales, values, |o| {
let lane = (x - origin) * plane_len + y.wrapping_add_signed(o) * nz;
&field[lane..lane + nz]
}),
Axis::Z => {
let lane = (x - origin) * plane_len + y * nz;
sweep_row(scales, values, &field[lane..lane + nz]);
}
}
}
let lanes: [&[T]; N] = core::array::from_fn(|j| &scratch[j * nz..(j + 1) * nz]);
let points: [&[T]; M] = core::array::from_fn(|p| {
let start = (x - pointwise[p].1) * plane_len + y * nz;
&pointwise[p].0[start..start + nz]
});
let mut rows: [&mut [T]; K] = core::array::from_fn(|d| {
let row = out_rows[d]
.next()
.expect("invariant: every destination plane holds ny rows");
&mut row[..nz]
});
for k in 0..nz {
let values = combine(
core::array::from_fn(|j| lanes[j][k]),
core::array::from_fn(|p| points[p][k]),
core::array::from_fn(|d| rows[d][k]),
);
for (row, value) in rows.iter_mut().zip(values) {
row[k] = value;
}
}
}
},
);
}
pub(super) fn add_row<T: RealField>(scales: Scales<T>, out: &mut [T], row: &[T]) {
let n = row.len();
if n >= 5 {
let interior = row[..n - 4]
.iter()
.zip(&row[1..n - 3])
.zip(&row[3..n - 1])
.zip(&row[4..]);
for (value, (((&m2, &m1), &p1), &p2)) in out[2..n - 2].iter_mut().zip(interior) {
*value += scales.fourth(m2, m1, p1, p2);
}
}
if n >= 4 {
for c in [0, 1, n - 2, n - 1] {
out[c] += Stencil::at(c, n).apply(scales, |o| row[c.wrapping_add_signed(o)]);
}
} else {
for c in 0..n {
out[c] += Stencil::at(c, n).apply(scales, |o| row[c.wrapping_add_signed(o)]);
}
}
}
pub(super) fn sweep_row<T: RealField>(scales: Scales<T>, out: &mut [T], row: &[T]) {
let n = row.len();
if n >= 5 {
let interior = row[..n - 4]
.iter()
.zip(&row[1..n - 3])
.zip(&row[3..n - 1])
.zip(&row[4..]);
for (value, (((&m2, &m1), &p1), &p2)) in out[2..n - 2].iter_mut().zip(interior) {
*value = scales.fourth(m2, m1, p1, p2);
}
}
let edges = [0, 1, n.saturating_sub(2), n.saturating_sub(1)];
for c in edges.into_iter().filter(|&c| c < n) {
out[c] = Stencil::at(c, n).apply(scales, |o| row[c.wrapping_add_signed(o)]);
}
}