#![cfg(feature = "parallel")]
use std::mem::MaybeUninit;
use strided_basic::{copy_into_uninit, map_into, Exec, Pool, StridedArray, StridedViewMut};
const ROWS: usize = 257;
const COLS: usize = 257;
const _: () = assert!(ROWS * COLS > (1 << 15));
fn large_map(exec: Exec<'_>) {
let source = StridedArray::<f64>::from_fn_col_major(&[ROWS * COLS], |index| index[0] as f64);
let mut destination = StridedArray::<f64>::col_major(&[ROWS * COLS]);
map_into(
&mut destination.view_mut(),
&source.view(),
|value| value + 1.0,
exec,
)
.unwrap();
assert_eq!(destination.get(&[ROWS * COLS - 1]), (ROWS * COLS) as f64);
}
#[test]
fn pooled_work_never_initializes_the_global_pool() {
let pool = Pool::builder().threads(4).build().unwrap();
let source = StridedArray::<f64>::from_fn_col_major(&[ROWS, COLS], |index| {
(index[0] * COLS + index[1]) as f64
});
let transposed = source.view().permute(&[1, 0]).unwrap();
let mut storage = vec![MaybeUninit::<f64>::uninit(); ROWS * COLS];
let mut dest =
StridedViewMut::new(&mut storage, &[COLS, ROWS], &[1, COLS as isize], 0).unwrap();
let small_source = StridedArray::<f64>::from_fn_col_major(&[4], |index| index[0] as f64);
let mut small_destination = StridedArray::<f64>::col_major(&[4]);
map_into(
&mut small_destination.view_mut(),
&small_source.view(),
|value| value + 1.0,
Exec::new(&pool, 4).unwrap(),
)
.unwrap();
large_map(Exec::new(&pool, 4).unwrap());
copy_into_uninit(&mut dest, &transposed, Exec::new(&pool, 4).unwrap()).unwrap();
for i in 0..COLS {
for j in 0..ROWS {
let got = unsafe { storage[i + j * COLS].assume_init() };
assert_eq!(got, source.get(&[j, i]), "mismatch at [{i}, {j}]");
}
}
assert!(
strided_exec::ledger::global_pool_unstarted(),
"the private strided pool started the process-global pool"
);
}