zc2 0.0.48

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Memory bandwidth: STREAM-style triad `a[i] = b[i] + 3*c[i]`.
//!
//! Every thread allocates and first-touches its OWN chunk of a, b and c, so on
//! NUMA machines the pages land on that thread's node and the aggregate
//! reflects all memory channels rather than one node's.
use super::budget::Budget;
use super::rate::max_rate;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

const FULL_LEN: usize = 32 * 1024 * 1024; // f64 elements in total per array (256 MiB)
const WINDOW: Duration = Duration::from_millis(250);

struct Chunk {
    a: Vec<f64>,
    b: Vec<f64>,
    c: Vec<f64>,
}

fn alloc(len: usize, v: f64) -> Option<Vec<f64>> {
    let mut x = Vec::new();
    x.try_reserve_exact(len).ok()?;
    x.resize(len, v);
    Some(x)
}

/// Elements in the last-resort chunk: it fits in cache, so it measures the
/// cache, not memory.
const TINY_LEN: usize = 1024;

/// Allocates a chunk of `len`, then `len / 4`, then (flagged) a cache-resident
/// one. `mk` is the allocator, injected so the fallback is testable.
fn build_chunk(len: usize, mk: impl Fn(usize) -> Option<Chunk>) -> (Chunk, bool) {
    match mk(len).or_else(|| mk(len / 4)) {
        Some(c) => (c, false),
        None => (
            Chunk {
                a: vec![0.0; TINY_LEN],
                b: vec![1.0; TINY_LEN],
                c: vec![2.0; TINY_LEN],
            },
            true,
        ),
    }
}

/// Built inside the worker thread so the first touch happens there.
fn chunk(len: usize, tiny: &AtomicBool) -> Chunk {
    let (c, was_tiny) = build_chunk(len, |len| {
        Some(Chunk {
            a: alloc(len, 0.0)?,
            b: alloc(len, 1.0)?,
            c: alloc(len, 2.0)?,
        })
    });
    if was_tiny {
        tiny.store(true, Ordering::SeqCst);
    }
    c
}

/// One triad pass over the thread's own chunk; returns the bytes moved.
fn pass(ch: &mut Chunk) -> f64 {
    for ((x, y), z) in ch.a.iter_mut().zip(&ch.b).zip(&ch.c) {
        *x = *y + 3.0 * *z;
    }
    std::hint::black_box(&ch.a);
    3.0 * 8.0 * ch.a.len() as f64
}

/// Triad bandwidth plus, when it could not be measured properly, why.
#[derive(Debug, PartialEq)]
pub struct Triad {
    /// 0 when the figure would have been wrong (see `note`).
    pub gbps: f64,
    pub note: Option<String>,
}

/// Turns the raw outcome into the reported figure. A cache-resident fallback
/// or a panicked worker must not pass off an inflated number as memory bandwidth.
fn finish(rate: Option<f64>, tiny: bool, panicked: usize) -> Option<Triad> {
    let r = rate.filter(|r| r.is_finite() && *r > 0.0)?;
    if tiny {
        return Some(Triad {
            gbps: 0.0,
            note: Some(
                "memory: too little free RAM for the bandwidth test; not measured".to_string(),
            ),
        });
    }
    Some(Triad {
        gbps: r / 1e9,
        note: (panicked > 0)
            .then(|| format!("memory: {panicked} worker(s) panicked; bandwidth understated")),
    })
}

pub fn triad_gbps(b: &Budget) -> Result<Triad, String> {
    if b.cancelled() {
        return Err("cancelled".into());
    }
    let threads = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(1);
    let len = FULL_LEN.div_ceil(threads);
    let tiny = AtomicBool::new(false);
    let o = max_rate(b, threads, WINDOW, |_| chunk(len, &tiny), pass);
    match finish(o.rate, tiny.load(Ordering::SeqCst), o.panicked) {
        Some(t) => Ok(t),
        None if b.cancelled() => Err("cancelled".into()),
        None => Err("triad timing failed".into()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ok_chunk(len: usize) -> Option<Chunk> {
        Some(Chunk {
            a: vec![0.0; len],
            b: vec![0.0; len],
            c: vec![0.0; len],
        })
    }

    #[test]
    fn a_failed_allocation_falls_back_in_two_steps_and_flags_only_the_last() {
        // Full size works.
        let (c, tiny) = build_chunk(4096, ok_chunk);
        assert_eq!((c.a.len(), tiny), (4096, false));
        // Only a quarter works.
        let (c, tiny) = build_chunk(4096, |l| if l <= 1024 { ok_chunk(l) } else { None });
        assert_eq!((c.a.len(), tiny), (1024, false));
        // Nothing works: the cache-resident chunk, flagged.
        let (c, tiny) = build_chunk(1 << 20, |_| None);
        assert_eq!((c.a.len(), tiny), (TINY_LEN, true));
    }

    #[test]
    fn a_cache_resident_run_reports_no_bandwidth_but_a_note() {
        let t = finish(Some(900e9), true, 0).unwrap();
        assert_eq!(t.gbps, 0.0);
        assert!(t.note.unwrap().contains("not measured"));
        let t = finish(Some(40e9), false, 0).unwrap();
        assert_eq!((t.gbps, t.note), (40.0, None));
        let t = finish(Some(40e9), false, 1).unwrap();
        assert!(t.note.unwrap().contains("panicked"));
        assert_eq!(finish(None, false, 0), None);
        assert_eq!(finish(Some(f64::NAN), false, 0), None);
    }
}