use default trait methods
This commit is contained in:
parent
60f9a89e65
commit
c3a40d81ee
|
@ -3,23 +3,38 @@
|
|||
|
||||
use crate::Float;
|
||||
|
||||
use ndarray::{ArrayView2, ArrayViewMut2};
|
||||
use ndarray::{ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
|
||||
|
||||
pub trait SbpOperator: Send + Sync {
|
||||
fn diffxi(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
||||
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
||||
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diff1d(r0, r1);
|
||||
}
|
||||
}
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
Self::diffxi(prev.reversed_axes(), fut.reversed_axes())
|
||||
}
|
||||
fn h() -> &'static [Float];
|
||||
}
|
||||
|
||||
pub trait UpwindOperator: SbpOperator {
|
||||
fn dissxi(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
||||
fn disseta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>);
|
||||
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>);
|
||||
fn dissxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diss1d(r0, r1);
|
||||
}
|
||||
}
|
||||
fn disseta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
Self::dissxi(prev.reversed_axes(), fut.reversed_axes())
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! diff_op_1d {
|
||||
($self: ty, $name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
impl $self {
|
||||
($name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
fn $name(prev: ArrayView1<Float>, mut fut: ArrayViewMut1<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
let nx = prev.shape()[0];
|
||||
|
@ -66,7 +81,6 @@ macro_rules! diff_op_1d {
|
|||
*f = diff * idx;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use super::SbpOperator;
|
||||
use crate::diff_op_1d;
|
||||
use crate::Float;
|
||||
use ndarray::{s, ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
|
||||
use ndarray::{s, ArrayView1, ArrayViewMut1};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SBP4 {}
|
||||
|
||||
diff_op_1d!(SBP4, diff_1d, SBP4::BLOCK, SBP4::DIAG, false);
|
||||
diff_op_1d!(diff_1d, SBP4::BLOCK, SBP4::DIAG, false);
|
||||
|
||||
impl SBP4 {
|
||||
#[rustfmt::skip]
|
||||
|
@ -27,18 +27,8 @@ impl SBP4 {
|
|||
}
|
||||
|
||||
impl SbpOperator for SBP4 {
|
||||
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diff_1d(r0, r1);
|
||||
}
|
||||
}
|
||||
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// transpose then use diffxi
|
||||
Self::diffxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diff_1d(prev, fut)
|
||||
}
|
||||
|
||||
fn h() -> &'static [Float] {
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use super::SbpOperator;
|
||||
use crate::diff_op_1d;
|
||||
use crate::Float;
|
||||
use ndarray::{s, ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
|
||||
use ndarray::{s, ArrayView1, ArrayViewMut1};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SBP8 {}
|
||||
|
||||
diff_op_1d!(SBP8, diff_1d, SBP8::BLOCK, SBP8::DIAG, false);
|
||||
diff_op_1d!(diff_1d, SBP8::BLOCK, SBP8::DIAG, false);
|
||||
|
||||
impl SBP8 {
|
||||
#[rustfmt::skip]
|
||||
|
@ -31,18 +31,8 @@ impl SBP8 {
|
|||
}
|
||||
|
||||
impl SbpOperator for SBP8 {
|
||||
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diff_1d(r0, r1);
|
||||
}
|
||||
}
|
||||
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// transpose then use diffxi
|
||||
Self::diffxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diff_1d(prev, fut)
|
||||
}
|
||||
|
||||
fn h() -> &'static [Float] {
|
||||
|
|
|
@ -12,18 +12,11 @@ type SimdT = packed_simd::f32x8;
|
|||
#[cfg(not(feature = "f32"))]
|
||||
type SimdT = packed_simd::f64x8;
|
||||
|
||||
diff_op_1d!(Upwind4, diff_1d, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_op_1d!(
|
||||
Upwind4,
|
||||
diss_1d,
|
||||
Upwind4::DISS_BLOCK,
|
||||
Upwind4::DISS_DIAG,
|
||||
true
|
||||
);
|
||||
diff_op_1d!(diff_1d, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_op_1d!(diss_1d, Upwind4::DISS_BLOCK, Upwind4::DISS_DIAG, true);
|
||||
|
||||
macro_rules! diff_simd_row_7_47 {
|
||||
($self: ident, $name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
impl $self {
|
||||
($name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
#[inline(never)]
|
||||
fn $name(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
|
@ -39,31 +32,29 @@ macro_rules! diff_simd_row_7_47 {
|
|||
|
||||
for j in 0..prev.len_of(Axis(0)) {
|
||||
use std::slice;
|
||||
let prev =
|
||||
unsafe { slice::from_raw_parts(prev.uget((j, 0)) as *const Float, nx) };
|
||||
let fut = unsafe {
|
||||
slice::from_raw_parts_mut(fut.uget_mut((j, 0)) as *mut Float, nx)
|
||||
};
|
||||
let prev = unsafe { slice::from_raw_parts(prev.uget((j, 0)) as *const Float, nx) };
|
||||
let fut =
|
||||
unsafe { slice::from_raw_parts_mut(fut.uget_mut((j, 0)) as *mut Float, nx) };
|
||||
|
||||
let first_elems = unsafe { SimdT::from_slice_unaligned_unchecked(prev) };
|
||||
let block = {
|
||||
let bl = $BLOCK;
|
||||
[
|
||||
SimdT::new(
|
||||
bl[0][0], bl[0][1], bl[0][2], bl[0][3], bl[0][4], bl[0][5],
|
||||
bl[0][6], 0.0,
|
||||
bl[0][0], bl[0][1], bl[0][2], bl[0][3], bl[0][4], bl[0][5], bl[0][6],
|
||||
0.0,
|
||||
),
|
||||
SimdT::new(
|
||||
bl[1][0], bl[1][1], bl[1][2], bl[1][3], bl[1][4], bl[1][5],
|
||||
bl[1][6], 0.0,
|
||||
bl[1][0], bl[1][1], bl[1][2], bl[1][3], bl[1][4], bl[1][5], bl[1][6],
|
||||
0.0,
|
||||
),
|
||||
SimdT::new(
|
||||
bl[2][0], bl[2][1], bl[2][2], bl[2][3], bl[2][4], bl[2][5],
|
||||
bl[2][6], 0.0,
|
||||
bl[2][0], bl[2][1], bl[2][2], bl[2][3], bl[2][4], bl[2][5], bl[2][6],
|
||||
0.0,
|
||||
),
|
||||
SimdT::new(
|
||||
bl[3][0], bl[3][1], bl[3][2], bl[3][3], bl[3][4], bl[3][5],
|
||||
bl[3][6], 0.0,
|
||||
bl[3][0], bl[3][1], bl[3][2], bl[3][3], bl[3][4], bl[3][5], bl[3][6],
|
||||
0.0,
|
||||
),
|
||||
]
|
||||
};
|
||||
|
@ -85,8 +76,7 @@ macro_rules! diff_simd_row_7_47 {
|
|||
}
|
||||
}
|
||||
|
||||
let last_elems =
|
||||
unsafe { SimdT::from_slice_unaligned_unchecked(&prev[nx - 8..]) }
|
||||
let last_elems = unsafe { SimdT::from_slice_unaligned_unchecked(&prev[nx - 8..]) }
|
||||
.shuffle1_dyn([7, 6, 5, 4, 3, 2, 1, 0].into());
|
||||
if $symmetric {
|
||||
fut[nx - 4] = idx * (block[3] * last_elems).sum();
|
||||
|
@ -101,22 +91,14 @@ macro_rules! diff_simd_row_7_47 {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
diff_simd_row_7_47!(Upwind4, diff_simd_row, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_simd_row_7_47!(
|
||||
Upwind4,
|
||||
diss_simd_row,
|
||||
Upwind4::DISS_BLOCK,
|
||||
Upwind4::DISS_DIAG,
|
||||
true
|
||||
);
|
||||
diff_simd_row_7_47!(diff_simd_row, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_simd_row_7_47!(diss_simd_row, Upwind4::DISS_BLOCK, Upwind4::DISS_DIAG, true);
|
||||
|
||||
macro_rules! diff_simd_col_7_47 {
|
||||
($self: ident, $name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
impl $self {
|
||||
($name: ident, $BLOCK: expr, $DIAG: expr, $symmetric: expr) => {
|
||||
#[inline(never)]
|
||||
fn $name(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
use std::slice;
|
||||
|
@ -260,18 +242,11 @@ macro_rules! diff_simd_col_7_47 {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
diff_simd_col_7_47!(Upwind4, diff_simd_col, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_simd_col_7_47!(
|
||||
Upwind4,
|
||||
diss_simd_col,
|
||||
Upwind4::DISS_BLOCK,
|
||||
Upwind4::DISS_DIAG,
|
||||
true
|
||||
);
|
||||
diff_simd_col_7_47!(diff_simd_col, Upwind4::BLOCK, Upwind4::DIAG, false);
|
||||
diff_simd_col_7_47!(diss_simd_col, Upwind4::DISS_BLOCK, Upwind4::DISS_DIAG, true);
|
||||
|
||||
impl Upwind4 {
|
||||
#[rustfmt::skip]
|
||||
|
@ -305,32 +280,30 @@ impl Upwind4 {
|
|||
}
|
||||
|
||||
impl SbpOperator for Upwind4 {
|
||||
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diff_1d(prev, fut)
|
||||
}
|
||||
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
match (prev.strides(), fut.strides()) {
|
||||
([_, 1], [_, 1]) => {
|
||||
Self::diff_simd_row(prev, fut);
|
||||
diff_simd_row(prev, fut);
|
||||
}
|
||||
([1, _], [1, _]) if prev.len_of(Axis(0)) % SimdT::lanes() == 0 => {
|
||||
Self::diff_simd_col(prev, fut);
|
||||
diff_simd_col(prev, fut);
|
||||
}
|
||||
([_, _], [_, _]) => {
|
||||
// Fallback, work row by row
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diff_1d(r0, r1);
|
||||
Self::diff1d(r0, r1);
|
||||
}
|
||||
}
|
||||
_ => unreachable!("Should only be two elements in the strides vectors"),
|
||||
}
|
||||
}
|
||||
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// transpose then use diffxi
|
||||
Self::diffxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
}
|
||||
|
||||
fn h() -> &'static [Float] {
|
||||
Self::HBLOCK
|
||||
}
|
||||
|
@ -350,7 +323,7 @@ fn upwind4_test() {
|
|||
target[i] = 1.0;
|
||||
}
|
||||
res.fill(0.0);
|
||||
Upwind4::diff_1d(source.view(), res.view_mut());
|
||||
Upwind4::diff1d(source.view(), res.view_mut());
|
||||
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-4);
|
||||
{
|
||||
let source = source.to_owned().insert_axis(ndarray::Axis(0));
|
||||
|
@ -376,7 +349,7 @@ fn upwind4_test() {
|
|||
target[i] = 2.0 * x;
|
||||
}
|
||||
res.fill(0.0);
|
||||
Upwind4::diff_1d(source.view(), res.view_mut());
|
||||
Upwind4::diff1d(source.view(), res.view_mut());
|
||||
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-4);
|
||||
{
|
||||
let source = source.to_owned().insert_axis(ndarray::Axis(0));
|
||||
|
@ -402,7 +375,7 @@ fn upwind4_test() {
|
|||
target[i] = 3.0 * x * x;
|
||||
}
|
||||
res.fill(0.0);
|
||||
Upwind4::diff_1d(source.view(), res.view_mut());
|
||||
Upwind4::diff1d(source.view(), res.view_mut());
|
||||
approx::assert_abs_diff_eq!(&res, &target, epsilon = 1e-2);
|
||||
|
||||
{
|
||||
|
@ -425,31 +398,29 @@ fn upwind4_test() {
|
|||
}
|
||||
|
||||
impl UpwindOperator for Upwind4 {
|
||||
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diss_1d(prev, fut)
|
||||
}
|
||||
fn dissxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
match (prev.strides(), fut.strides()) {
|
||||
([_, 1], [_, 1]) => {
|
||||
Self::diss_simd_row(prev, fut);
|
||||
diss_simd_row(prev, fut);
|
||||
}
|
||||
([1, _], [1, _]) if prev.len_of(Axis(0)) % SimdT::lanes() == 0 => {
|
||||
Self::diss_simd_col(prev, fut);
|
||||
diss_simd_col(prev, fut);
|
||||
}
|
||||
([_, _], [_, _]) => {
|
||||
// Fallback, work row by row
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diss_1d(r0, r1);
|
||||
Self::diss1d(r0, r1);
|
||||
}
|
||||
}
|
||||
_ => unreachable!("Should only be two elements in the strides vectors"),
|
||||
}
|
||||
}
|
||||
|
||||
fn disseta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// diffeta = transpose then use dissxi
|
||||
Self::dissxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -1,19 +1,13 @@
|
|||
use super::{SbpOperator, UpwindOperator};
|
||||
use crate::diff_op_1d;
|
||||
use crate::Float;
|
||||
use ndarray::{s, ArrayView1, ArrayView2, ArrayViewMut1, ArrayViewMut2};
|
||||
use ndarray::{s, ArrayView1, ArrayViewMut1};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Upwind9 {}
|
||||
|
||||
diff_op_1d!(Upwind9, diff_1d, Upwind9::BLOCK, Upwind9::DIAG, false);
|
||||
diff_op_1d!(
|
||||
Upwind9,
|
||||
diss_1d,
|
||||
Upwind9::DISS_BLOCK,
|
||||
Upwind9::DISS_DIAG,
|
||||
true
|
||||
);
|
||||
diff_op_1d!(diff_1d, Upwind9::BLOCK, Upwind9::DIAG, false);
|
||||
diff_op_1d!(diss_1d, Upwind9::DISS_BLOCK, Upwind9::DISS_DIAG, true);
|
||||
|
||||
impl Upwind9 {
|
||||
#[rustfmt::skip]
|
||||
|
@ -55,18 +49,8 @@ impl Upwind9 {
|
|||
}
|
||||
|
||||
impl SbpOperator for Upwind9 {
|
||||
fn diffxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diff_1d(r0, r1);
|
||||
}
|
||||
}
|
||||
|
||||
fn diffeta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// transpose then use diffxi
|
||||
Self::diffxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
fn diff1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diff_1d(prev, fut)
|
||||
}
|
||||
|
||||
fn h() -> &'static [Float] {
|
||||
|
@ -75,18 +59,8 @@ impl SbpOperator for Upwind9 {
|
|||
}
|
||||
|
||||
impl UpwindOperator for Upwind9 {
|
||||
fn dissxi(prev: ArrayView2<Float>, mut fut: ArrayViewMut2<Float>) {
|
||||
assert_eq!(prev.shape(), fut.shape());
|
||||
assert!(prev.shape()[1] >= 2 * Self::BLOCK.len());
|
||||
|
||||
for (r0, r1) in prev.outer_iter().zip(fut.outer_iter_mut()) {
|
||||
Self::diss_1d(r0, r1);
|
||||
}
|
||||
}
|
||||
|
||||
fn disseta(prev: ArrayView2<Float>, fut: ArrayViewMut2<Float>) {
|
||||
// diffeta = transpose then use dissxi
|
||||
Self::dissxi(prev.reversed_axes(), fut.reversed_axes());
|
||||
fn diss1d(prev: ArrayView1<Float>, fut: ArrayViewMut1<Float>) {
|
||||
diss_1d(prev, fut)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue