#![cfg(feature = "std")]
use colmat::diag::Diag;
use colmat::dyn_stack::{MemBuffer, MemStack};
use colmat::linalg::svd::{ComputeSvdVectors, svd, svd_scratch};
use colmat::traits::ComplexField;
use colmat::{Auto, Mat, Par, ParShape, c32, c64};
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::atomic::{AtomicUsize, Ordering};
struct Counting;
thread_local! {
static COUNTING: Cell<bool> = const { Cell::new(false) };
}
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
#[inline]
fn counting() -> bool {
COUNTING.with(Cell::get)
}
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if counting() {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
}
unsafe { System.alloc(layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
if counting() {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
}
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(
&self,
ptr: *mut u8,
layout: Layout,
new_size: usize,
) -> *mut u8 {
if counting() {
ALLOCATIONS.fetch_add(1, Ordering::Relaxed);
}
unsafe { System.realloc(ptr, layout, new_size) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { System.dealloc(ptr, layout) }
}
}
#[global_allocator]
static GLOBAL: Counting = Counting;
fn values_only_allocations<T: ComplexField>(a: &Mat<T>) -> usize {
let (m, n) = a.shape();
let params = <colmat::linalg::svd::SvdParams as Auto<T>>::auto().into();
let mut mem = MemBuffer::new(svd_scratch::<T>(
m,
n,
ComputeSvdVectors::No,
ComputeSvdVectors::No,
ParShape::Seq,
params,
));
let mut s = Diag::<T>::zeros(Ord::min(m, n));
let mut run = || {
svd(
a.as_ref(),
s.as_mut(),
None,
None,
Par::Seq,
MemStack::new(&mut mem),
params,
)
.unwrap()
};
run(); ALLOCATIONS.store(0, Ordering::SeqCst);
COUNTING.with(|c| c.set(true));
run();
COUNTING.with(|c| c.set(false));
ALLOCATIONS.load(Ordering::SeqCst)
}
fn sample(m: usize, n: usize, seed: u64) -> Mat<f64> {
let mut x = seed
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
Mat::from_fn(m, n, |_, _| {
x = x
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
((x >> 11) as f64 / (1u64 << 53) as f64) - 0.5
})
}
static GATE: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn check_cell(m: usize, n: usize) {
let _turn = GATE.lock().unwrap_or_else(|e| e.into_inner());
let re = sample(m, n, 156);
let im = sample(m, n, 157);
let f32s = Mat::<f32>::from_fn(m, n, |i, j| re[(i, j)] as f32);
let c64s =
Mat::<c64>::from_fn(m, n, |i, j| c64::new(re[(i, j)], im[(i, j)]));
let c32s = Mat::<c32>::from_fn(m, n, |i, j| {
c32::new(re[(i, j)] as f32, im[(i, j)] as f32)
});
let counts = [
values_only_allocations(&re),
values_only_allocations(&f32s),
values_only_allocations(&c64s),
values_only_allocations(&c32s),
];
assert_eq!(
counts, [0; 4],
"{m} x {n}: allocations (f64, f32, c64, c32)"
);
}
macro_rules! cells {
($($name:ident: ($m:expr, $n:expr);)*) => {
$(
#[test]
fn $name() {
check_cell($m, $n);
}
)*
};
}
cells! {
values_only_svd_allocates_nothing_1x1: (1, 1);
values_only_svd_allocates_nothing_2x2: (2, 2);
values_only_svd_allocates_nothing_17x17: (17, 17);
values_only_svd_allocates_nothing_40x33: (40, 33);
values_only_svd_allocates_nothing_33x40: (33, 40);
values_only_svd_allocates_nothing_128x128: (128, 128);
values_only_svd_allocates_nothing_300x60: (300, 60);
values_only_svd_allocates_nothing_700x700: (700, 700);
}