crates/kleene-index/tests/gatling.rs
//! kleene's device on gatling answers as on its own pool.
use std::sync::Arc;
use kleene_index::Gatling;
use kleene_sparse::{BoolOrAnd, CpuBackend, CpuDevice, Csr, Executor, Mask, SparseBackend, Vector};
#[test]
fn a_device_on_gatling_answers_as_on_its_own_pool() {
let exec = Arc::new(Gatling::new(4));
assert_eq!(exec.threads(), 4);
let on_gatling = CpuDevice::with_executor(exec).with_grain(64);
let own = CpuDevice::new(4).with_grain(64);
let n = 50_000usize;
let mut src = Vec::new();
let mut dst = Vec::new();
for i in 0..n {
for d in [1usize, 3, 1000] {
src.push(i as u32);
dst.push(((i * 7 + d) % n) as u32);
}
}
let a = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
let x = Vector::from_indices(n, &(0..n as u32).step_by(5).collect::<Vec<_>>(), 1u8).unwrap();
let mask = Mask::from_indices(n, &(0..n as u32).step_by(3).collect::<Vec<_>>()).complement();
for m in [None, Some(&mask)] {
for _ in 0..20 {
let got = CpuBackend::vxm(&x, &a, m, BoolOrAnd, &on_gatling).unwrap();
let want = CpuBackend::vxm(&x, &a, m, BoolOrAnd, &own).unwrap();
assert_eq!(got.indices(), want.indices());
let got = CpuBackend::mxv(&a, &x, m, BoolOrAnd, &on_gatling).unwrap();
let want = CpuBackend::mxv(&a, &x, m, BoolOrAnd, &own).unwrap();
assert_eq!(got.indices(), want.indices());
}
}
}
#[test]
fn a_job_calling_back_in_runs_its_shares_alone() {
let exec = Gatling::new(4);
let hits = std::sync::atomic::AtomicUsize::new(0);
exec.run(4, &|_| {
exec.run(4, &|_| {
hits.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
});
});
assert_eq!(hits.into_inner(), 16);
}
#[test]
fn strong_components_and_source_sets_on_gatling_answer_as_kleene_s_own() {
let gat = CpuDevice::with_executor(Arc::new(Gatling::new(4)));
let own = CpuDevice::new(4);
let n = 30_000usize;
let mut src = Vec::new();
let mut dst = Vec::new();
for i in 0..n {
// forward chains with back edges: cycles of many sizes
for d in [1usize, 17] {
src.push(i as u32);
dst.push(((i + d) % n) as u32);
}
if i % 7 == 0 {
src.push(i as u32);
dst.push((i / 3) as u32);
}
}
let out = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
let inn = out.transpose();
let got = kleene_graph::scc_of::<CpuBackend>(&out, &inn, &gat).unwrap();
let want = kleene_graph::scc_of::<CpuBackend>(&out, &inn, &own).unwrap();
assert_eq!(got.of, want.of, "components named by their least vertex");
let sources: Vec<u32> = (0..100u32).map(|k| (k * 7919) % n as u32).collect();
let got = kleene_graph::reach_sets::<CpuBackend>(&out, &sources, &gat).unwrap();
let want = kleene_graph::reach_sets::<CpuBackend>(&out, &sources, &own).unwrap();
assert_eq!(got.reached(), want.reached());
for v in (0..n as u32).step_by(13) {
assert_eq!(got.sources_of(v), want.sources_of(v), "sources of {v}");
}
}
#[test]
fn above_the_condensed_walk_limit_source_sets_come_from_the_pulling_walk() {
// a limit of 0 edges: every graph takes gatling's pulling mask walk
let gat = CpuDevice::with_executor(Arc::new(Gatling::new(4).with_condensed_walk_edges(0)));
let own = CpuDevice::new(4);
let n = 40_000usize;
let mut src = Vec::new();
let mut dst = Vec::new();
let mut s = 5u64;
for i in 0..n {
// a wide random part (its middle levels pull) and chains with back edges
for _ in 0..(i % 9) {
s = s
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
src.push(i as u32);
dst.push(((s >> 33) % n as u64) as u32);
}
src.push(i as u32);
dst.push(((i + 1) % n) as u32);
}
let out = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
let (off, adj) = (out.pattern().offsets(), out.pattern().indices());
let one: Vec<(u32, u64)> = vec![(3, 1), (n as u32 - 1, 1 << 63)];
let batches: [&[(u32, u64)]; 1] = [&one];
let masks = gat
.engine_mask_walk(off, adj, &batches)
.expect("the engine answers above its limit");
assert_eq!(masks.len(), 1);
assert_eq!(masks[0].len(), n);
for sources in [
vec![0u32],
(0..100u32)
.map(|k| (k * 7919) % n as u32)
.collect::<Vec<_>>(),
] {
let got = kleene_graph::reach_sets::<CpuBackend>(&out, &sources, &gat).unwrap();
let want = kleene_graph::reach_sets_generic::<CpuBackend>(&out, &sources, &own).unwrap();
assert_eq!(got.reached(), want.reached());
for v in 0..n as u32 {
assert_eq!(got.sources_of(v), want.sources_of(v), "sources of {v}");
}
}
}
#[test]
fn labelled_source_sets_on_gatling_equal_the_backend_s_propagation() {
let gat = CpuDevice::with_executor(Arc::new(Gatling::new(4)));
let own = CpuDevice::new(4);
let n = 60_000usize;
let mut s = 11u64;
let mut next = || {
s = s
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
s >> 33
};
let (mut src, mut dst) = (Vec::new(), Vec::new());
for i in 0..n {
// chains with back edges and a random spread: cycles that pass only some sources
src.push(i as u32);
dst.push(((i + 1) % n) as u32);
for _ in 0..(i % 5) {
src.push(i as u32);
dst.push((next() % n as u64) as u32);
}
}
let out = Csr::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
for sources in [50usize, 150] {
let words = sources.div_ceil(64);
let sets = 9usize;
let mut set_words = vec![0u64; sets * words];
for (i, w) in set_words.iter_mut().enumerate() {
// set 0 passes everyone; the others random halves
*w = if i < words {
u64::MAX
} else {
next() | next() << 31 | next() << 62
};
}
let slot_set: Vec<u32> = (0..out.nnz())
.map(|_| (next() % sets as u64) as u32)
.collect();
let edge_sets = kleene_graph::EdgeSets::new(&slot_set, &set_words, words).unwrap();
let seeds: Vec<(u32, u32)> = (0..sources as u32 * 2)
.map(|i| ((next() % n as u64) as u32, i % sources as u32))
.collect();
let (off, adj) = (out.pattern().offsets(), out.pattern().indices());
let starts: Vec<(u32, Vec<u64>)> = seeds
.iter()
.map(|&(v, k)| {
let mut m = vec![0u64; words];
m[k as usize / 64] |= 1 << (k % 64);
(v, m)
})
.collect();
let refs: Vec<(u32, &[u64])> = starts.iter().map(|(v, m)| (*v, &m[..])).collect();
let masks = gat
.engine_mask_walk_sets(off, adj, &slot_set, &set_words, words, &refs)
.expect("gatling walks edge sets");
assert_eq!(masks.len(), n * words);
let t = std::time::Instant::now();
let got = kleene_graph::label_reach::<CpuBackend>(&out, edge_sets, &seeds, sources, &gat)
.unwrap();
let on_gatling = t.elapsed();
let t = std::time::Instant::now();
let want =
kleene_graph::label_reach_generic::<CpuBackend>(&out, edge_sets, &seeds, sources, &own)
.unwrap();
let generic = t.elapsed();
assert_eq!(got.reached(), want.reached());
let mut pairs = 0usize;
for v in 0..n as u32 {
let of = got.sources_of(v);
assert_eq!(of, want.sources_of(v), "sources of {v}");
pairs += of.len();
}
assert!(pairs > n, "the walk spreads");
eprintln!(
"{sources} sources, {} edges: gatling {on_gatling:?}, propagation {generic:?}, {pairs} pairs",
out.nnz()
);
}
}