#![cfg(windows)]
#![allow(
clippy::type_complexity,
clippy::too_many_arguments,
reason = "test helpers mirror the long signatures of the wrappers under test"
)]
use aocl_sparse::*;
use core::ffi::c_void;
extern "system" {
fn VirtualAlloc(addr: *mut c_void, size: usize, ty: u32, prot: u32) -> *mut c_void;
fn VirtualProtect(addr: *mut c_void, size: usize, prot: u32, old: *mut u32) -> i32;
fn VirtualFree(addr: *mut c_void, size: usize, ty: u32) -> i32;
}
const PAGE: usize = 4096;
type I = i32;
fn at_guard<T: Copy, R>(src: &[T], n: usize, f: impl FnOnce(&mut [T]) -> R) -> R {
assert!(n <= src.len() && n * core::mem::size_of::<T>() <= PAGE);
unsafe {
let base = VirtualAlloc(core::ptr::null_mut(), 2 * PAGE, 0x3000, 0x04);
assert!(!base.is_null());
let mut old = 0u32;
assert_ne!(
VirtualProtect(
(base as *mut u8).add(PAGE) as *mut c_void,
PAGE,
0x01,
&mut old
),
0
);
let start = (base as *mut u8).add(PAGE - n * core::mem::size_of::<T>()) as *mut T;
let s = core::slice::from_raw_parts_mut(start, n);
s.copy_from_slice(&src[..n]);
let r = f(s);
VirtualFree(base, 0, 0x8000);
r
}
}
fn refused<T: core::fmt::Debug>(what: &str, r: Result<T>) {
assert!(matches!(r, Err(Error::InvalidArgument(_))), "{what}: {r:?}");
}
fn descr(ty: MatType) -> MatDescr {
let mut d = MatDescr::new().unwrap();
d.set_type(ty).unwrap();
d.set_fill_mode(FillMode::Lower).unwrap();
d.set_diag_type(DiagType::NonUnit).unwrap();
d
}
#[derive(Clone, Copy, Debug)]
enum Brk {
Ok,
Short(&'static str),
Idx(&'static str, usize, I),
}
macro_rules! family {
($m:ident, $T:ty, $one:expr, csr2ell = $csr2ell:expr, csr2dia = $csr2dia:expr) => {
mod $m {
use super::*;
fn v(x: f64) -> $T {
($one)(x)
}
fn vals(n: usize) -> Vec<$T> {
(0..n).map(|i| v(1.0 + i as f64)).collect()
}
fn csrmv_with(b: Brk) -> Result<()> {
let (mut rp, mut ci): (Vec<I>, Vec<I>) = (vec![0, 2, 3, 5], vec![0, 2, 1, 0, 3]);
let (val, x, mut y) = (vals(5), vals(4), vec![v(0.0); 3]);
if let Brk::Idx(a, p, k) = b {
match a {
"ci" => ci[p] = k,
"rp" => rp[p] = k,
_ => unreachable!(),
}
}
let d = descr(MatType::General);
let call = |val: &[$T], ci: &[I], rp: &[I], x: &[$T], y: &mut [$T]| {
csrmv::<$T>(v(1.0), 3, 4, val, ci, rp, &d, x, v(0.0), y)
};
match b {
Brk::Short("val") => at_guard(&val, 4, |s| call(s, &ci, &rp, &x, &mut y)),
Brk::Short("ci") => at_guard(&ci, 4, |s| call(&val, s, &rp, &x, &mut y)),
Brk::Short("rp") => at_guard(&rp, 3, |s| call(&val, &ci, s, &x, &mut y)),
Brk::Short("x") => at_guard(&x, 3, |s| call(&val, &ci, &rp, s, &mut y)),
Brk::Short("y") => {
let yy = vec![v(0.0); 3];
at_guard(&yy, 2, |s| call(&val, &ci, &rp, &x, s))
}
Brk::Idx("ci", ..) | Brk::Idx("rp", ..) => {
at_guard(&x, 4, |s| call(&val, &ci, &rp, s, &mut y))
}
_ => call(&val, &ci, &rp, &x, &mut y),
}
}
#[test]
fn csrmv_control() {
csrmv_with(Brk::Ok).unwrap();
}
macro_rules! t {
($name:ident, $f:ident, $brk:expr) => {
#[test]
fn $name() {
refused(stringify!($name), $f($brk));
}
};
}
t!(csrmv_short_val, csrmv_with, Brk::Short("val"));
t!(csrmv_short_ci, csrmv_with, Brk::Short("ci"));
t!(csrmv_short_rp, csrmv_with, Brk::Short("rp"));
t!(csrmv_short_x, csrmv_with, Brk::Short("x"));
t!(csrmv_short_y, csrmv_with, Brk::Short("y"));
t!(csrmv_col_past_n, csrmv_with, Brk::Idx("ci", 1, 4));
t!(csrmv_col_negative, csrmv_with, Brk::Idx("ci", 1, -2));
t!(csrmv_rowptr_past_nnz, csrmv_with, Brk::Idx("rp", 3, 99));
t!(csrmv_rowptr_decreasing, csrmv_with, Brk::Idx("rp", 2, 1));
fn csrsv_with(b: Brk) -> Result<()> {
let (mut rp, mut ci): (Vec<I>, Vec<I>) = (vec![0, 1, 3, 6], vec![0, 0, 1, 0, 1, 2]);
let (val, x, mut y) = (vals(6), vals(3), vec![v(0.0); 3]);
if let Brk::Idx(a, p, k) = b {
match a {
"ci" => ci[p] = k,
"rp" => rp[p] = k,
_ => unreachable!(),
}
}
let d = descr(MatType::General);
let call = |val: &[$T], ci: &[I], rp: &[I], x: &[$T], y: &mut [$T]| {
csrsv::<$T>(Trans::No, v(1.0), 3, val, ci, rp, &d, x, y)
};
match b {
Brk::Short("val") => at_guard(&val, 5, |s| call(s, &ci, &rp, &x, &mut y)),
Brk::Short("ci") => at_guard(&ci, 5, |s| call(&val, s, &rp, &x, &mut y)),
Brk::Short("rp") => at_guard(&rp, 3, |s| call(&val, &ci, s, &x, &mut y)),
Brk::Short("x") => at_guard(&x, 2, |s| call(&val, &ci, &rp, s, &mut y)),
Brk::Short("y") => {
let yy = vec![v(0.0); 3];
at_guard(&yy, 2, |s| call(&val, &ci, &rp, &x, s))
}
Brk::Idx(..) => at_guard(&x, 3, |s| call(&val, &ci, &rp, s, &mut y)),
_ => call(&val, &ci, &rp, &x, &mut y),
}
}
#[test]
fn csrsv_control() {
csrsv_with(Brk::Ok).unwrap();
}
t!(csrsv_short_val, csrsv_with, Brk::Short("val"));
t!(csrsv_short_ci, csrsv_with, Brk::Short("ci"));
t!(csrsv_short_rp, csrsv_with, Brk::Short("rp"));
t!(csrsv_short_x, csrsv_with, Brk::Short("x"));
t!(csrsv_short_y, csrsv_with, Brk::Short("y"));
t!(csrsv_col_past_m, csrsv_with, Brk::Idx("ci", 3, 3));
t!(csrsv_rowptr_past_nnz, csrsv_with, Brk::Idx("rp", 3, 99));
fn ell_arrays() -> (Vec<I>, Vec<$T>) {
let (rp, ci): (Vec<I>, Vec<I>) = (vec![0, 2, 3, 5], vec![0, 2, 1, 0, 3]);
let (mut col, mut val) = (vec![0 as I; 9], vec![v(0.0); 9]);
($csr2ell)(
3,
&descr(MatType::General),
&rp,
&ci,
&vals(5),
&mut col,
&mut val,
3,
)
.unwrap();
(col, val)
}
fn ellmv_with(b: Brk) -> Result<()> {
let (mut col, val) = ell_arrays();
let (x, mut y) = (vals(4), vec![v(0.0); 3]);
if let Brk::Idx(_, p, k) = b {
col[p] = k;
}
let d = descr(MatType::General);
let call = |val: &[$T], col: &[I], x: &[$T], y: &mut [$T]| {
ellmv::<$T>(Trans::No, v(1.0), 3, 4, val, col, 3, &d, x, v(0.0), y)
};
match b {
Brk::Short("val") => at_guard(&val, 8, |s| call(s, &col, &x, &mut y)),
Brk::Short("col") => at_guard(&col, 8, |s| call(&val, s, &x, &mut y)),
Brk::Short("x") => at_guard(&x, 3, |s| call(&val, &col, s, &mut y)),
Brk::Short("y") => {
let yy = vec![v(0.0); 3];
at_guard(&yy, 2, |s| call(&val, &col, &x, s))
}
Brk::Idx(..) => at_guard(&x, 4, |s| call(&val, &col, s, &mut y)),
_ => call(&val, &col, &x, &mut y),
}
}
#[test]
fn ellmv_control() {
ellmv_with(Brk::Ok).unwrap();
}
t!(ellmv_short_val, ellmv_with, Brk::Short("val"));
t!(ellmv_short_col, ellmv_with, Brk::Short("col"));
t!(ellmv_short_x, ellmv_with, Brk::Short("x"));
t!(ellmv_short_y, ellmv_with, Brk::Short("y"));
t!(ellmv_col_past_n, ellmv_with, Brk::Idx("col", 0, 4));
fn csr2ell_with(b: Brk) -> Result<()> {
let (rp, ci): (Vec<I>, Vec<I>) = (vec![0, 2, 3, 5], vec![0, 2, 1, 0, 3]);
let (val, col0, val0) = (vals(5), vec![0 as I; 9], vec![v(0.0); 9]);
let d = descr(MatType::General);
let call = |rp: &[I], ci: &[I], val: &[$T], oc: &mut [I], ov: &mut [$T]| {
($csr2ell)(3, &d, rp, ci, val, oc, ov, 3)
};
match b {
Brk::Short("oc") => {
let mut ov = val0.clone();
at_guard(&col0, 8, |s| call(&rp, &ci, &val, s, &mut ov))
}
Brk::Short("ov") => {
let mut oc = col0.clone();
at_guard(&val0, 8, |s| call(&rp, &ci, &val, &mut oc, s))
}
Brk::Short("rp") => {
let (mut oc, mut ov) = (col0.clone(), val0.clone());
at_guard(&rp, 3, |s| call(s, &ci, &val, &mut oc, &mut ov))
}
Brk::Short("ci") => {
let (mut oc, mut ov) = (col0.clone(), val0.clone());
at_guard(&ci, 4, |s| call(&rp, s, &val, &mut oc, &mut ov))
}
Brk::Short("val") => {
let (mut oc, mut ov) = (col0.clone(), val0.clone());
at_guard(&val, 4, |s| call(&rp, &ci, s, &mut oc, &mut ov))
}
_ => {
let (mut oc, mut ov) = (col0.clone(), val0.clone());
call(&rp, &ci, &val, &mut oc, &mut ov)
}
}
}
#[test]
fn csr2ell_control() {
csr2ell_with(Brk::Ok).unwrap();
}
t!(csr2ell_short_out_col, csr2ell_with, Brk::Short("oc"));
t!(csr2ell_short_out_val, csr2ell_with, Brk::Short("ov"));
t!(csr2ell_short_rp, csr2ell_with, Brk::Short("rp"));
t!(csr2ell_short_ci, csr2ell_with, Brk::Short("ci"));
t!(csr2ell_short_val, csr2ell_with, Brk::Short("val"));
fn csr2dia_with(b: Brk) -> Result<()> {
let rp: Vec<I> = vec![0, 2, 5, 7, 9];
let ci: Vec<I> = vec![0, 2, 0, 1, 3, 1, 2, 2, 3];
let (val, off0, dv0) = (vals(9), vec![0 as I; 3], vec![v(-9.0); 12]);
let d = descr(MatType::General);
let call = |rp: &[I], ci: &[I], val: &[$T], off: &mut [I], dv: &mut [$T]| {
($csr2dia)(4, 4, &d, rp, ci, val, 3, off, dv)
};
match b {
Brk::Short("off") => {
let mut dv = dv0.clone();
at_guard(&off0, 2, |s| call(&rp, &ci, &val, s, &mut dv))
}
Brk::Short("dv") => {
let mut off = off0.clone();
at_guard(&dv0, 11, |s| call(&rp, &ci, &val, &mut off, s))
}
Brk::Short("rp") => {
let (mut off, mut dv) = (off0.clone(), dv0.clone());
at_guard(&rp, 4, |s| call(s, &ci, &val, &mut off, &mut dv))
}
Brk::Short("ci") => {
let (mut off, mut dv) = (off0.clone(), dv0.clone());
at_guard(&ci, 8, |s| call(&rp, s, &val, &mut off, &mut dv))
}
Brk::Short("val") => {
let (mut off, mut dv) = (off0.clone(), dv0.clone());
at_guard(&val, 8, |s| call(&rp, &ci, s, &mut off, &mut dv))
}
_ => {
let (mut off, mut dv) = (off0.clone(), dv0.clone());
call(&rp, &ci, &val, &mut off, &mut dv)
}
}
}
#[test]
fn csr2dia_control() {
csr2dia_with(Brk::Ok).unwrap();
}
t!(csr2dia_short_offsets, csr2dia_with, Brk::Short("off"));
t!(csr2dia_short_values, csr2dia_with, Brk::Short("dv"));
t!(csr2dia_short_rp, csr2dia_with, Brk::Short("rp"));
t!(csr2dia_short_ci, csr2dia_with, Brk::Short("ci"));
t!(csr2dia_short_val, csr2dia_with, Brk::Short("val"));
}
};
}
family!(
real_f64,
f64,
|x: f64| x,
csr2ell = csr2ell_f64,
csr2dia = csr2dia_f64
);
family!(
real_f32,
f32,
|x: f64| x as f32,
csr2ell = csr2ell_f32,
csr2dia = csr2dia_f32
);