Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-graph/tests/many.rs

//! Many searches side by side give each search's own answer, at every thread count, within
//! the budget or past it (one at a time), with or without a hop limit and a mask.

use kleene_graph::{BfsMany, BfsOptions, BfsState, Direction, Graph, bfs_with};
use kleene_sparse::{BoolOrAnd, CpuBackend, CpuDevice, Csr, Mask};

type B = CpuBackend;

fn rmat_like(scale: u32, factor: usize, seed: u64) -> (usize, Vec<u32>, Vec<u32>) {
    let n = 1usize << scale;
    let mut x = seed;
    let mut step = || {
        x = x
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        (x >> 33) as u32
    };
    let (mut src, mut dst) = (Vec::new(), Vec::new());
    for _ in 0..n * factor {
        let (mut s, mut d) = (0u32, 0u32);
        for _ in 0..scale {
            let r = step() % 100;
            s = (s << 1) | u32::from((57..76).contains(&r) || r >= 95);
            d = (d << 1) | u32::from(r >= 76);
        }
        src.push(s);
        dst.push(d);
    }
    (n, src, dst)
}

#[test]
fn many_searches_side_by_side_give_each_search_alone() {
    let (n, src, dst) = rmat_like(13, 6, 11);
    let a = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
    let sources: Vec<u32> = (0..37u32).map(|i| i * 211 % n as u32).collect();
    let allowed = Mask::from_indices(
        n,
        &(0..n as u32).filter(|v| v % 11 != 3).collect::<Vec<_>>(),
    );
    for device in [
        CpuDevice::new(1),
        CpuDevice::new(4).with_grain(64),
        CpuDevice::new(5).with_grain(64),
        CpuDevice::default().with_grain(256),
    ] {
        let g: Graph<B> = Graph::new(a.clone(), &device).unwrap();
        // the default budget (side by side), one state's worth (one at a time), two states'
        // worth (two searchers sharing the threads)
        let per = n * kleene_graph::MANY_BYTES_PER_VERTEX;
        for budget in [kleene_graph::MANY_BUDGET, per, 2 * per] {
            let mut many = BfsMany::new().with_budget(budget);
            for direction in [Direction::Auto, Direction::Push, Direction::Pull] {
                for max_depth in [None, Some(2)] {
                    for mask in [None, Some(allowed.clone())] {
                        let opts = BfsOptions {
                            direction,
                            max_depth,
                            allowed: mask,
                            ..BfsOptions::default()
                        };
                        let mut st = BfsState::<B>::new(n, &device);
                        let want: Vec<u64> = sources
                            .iter()
                            .map(|&s| {
                                bfs_with(&g, &[s], &opts, &device, &mut st).unwrap().reached as u64
                            })
                            .collect();
                        // twice: the second call on the kept searchers
                        for _ in 0..2 {
                            let mut got = vec![0u64; sources.len()];
                            many.run(&g, &sources, &opts, &device, &mut got).unwrap();
                            assert_eq!(
                                got,
                                want,
                                "{direction:?}, depth {max_depth:?}, budget {budget}, {} threads",
                                device.threads()
                            );
                        }
                    }
                }
            }
            assert!(many.searchers(n, sources.len(), &device) <= device.threads());
        }
        let wide = BfsMany::new();
        assert_eq!(
            wide.searchers(n, sources.len(), &device),
            device.threads().min(sources.len())
        );
        assert_eq!(BfsMany::new().with_budget(0).searchers(n, 9, &device), 1);
    }
}

#[test]
fn many_searches_refuse_a_bad_source_and_a_wrong_answer_length() {
    let (n, src, dst) = rmat_like(10, 4, 5);
    let a = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
    let device = CpuDevice::new(4);
    let g: Graph<B> = Graph::new(a, &device).unwrap();
    let mut many = BfsMany::new();
    let opts = BfsOptions::default();
    let mut got = vec![0u64; 3];
    assert!(
        many.run(&g, &[1, n as u32, 2], &opts, &device, &mut got)
            .is_err()
    );
    assert!(many.run(&g, &[1, 2], &opts, &device, &mut got).is_err());
    // still usable after a refusal
    many.run(&g, &[1, 2, 3], &opts, &device, &mut got).unwrap();
    assert!(got.iter().all(|&r| r >= 1));
}