crates/kleene-cubecl/tests/graphs/mod.rs
//! The test and timing graphs of the computed split: the benchmark's R-MAT and road
//! generators (`kleene::synth`, copied here on one thread: the same graph for the same
//! seed), K dense groups joined by a few edges, and a graph with isolated nodes.
#![allow(dead_code)]
use kleene_sparse::{BoolOrAnd, Csr};
/// splitmix64, as `kleene::synth::SplitMix`.
pub struct SplitMix(pub u64);
impl SplitMix {
pub fn next_u64(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
pub fn below(&mut self, n: u64) -> u64 {
((self.next_u64() as u128 * n as u128) >> 64) as u64
}
}
const RMAT_CHUNK: usize = 1 << 16;
/// `kleene::synth::rmat` on one thread: `edge_factor << scale` R-MAT edges (Graph500's
/// a = 0.57, b = c = 0.19) over `1 << scale` nodes, ids shuffled. The benchmark's sets
/// are `rmat(scale, 8, 1)`.
pub fn rmat_edges(scale: u32, edge_factor: u64, seed: u64) -> (Vec<u32>, Vec<u32>) {
let n = 1u64 << scale;
let m = (edge_factor << scale) as usize;
let mut src = vec![0u32; m];
let mut dst = vec![0u32; m];
const A: u64 = (0.57 * 4294967296.0) as u64;
const AB: u64 = (0.76 * 4294967296.0) as u64;
const ABC: u64 = (0.95 * 4294967296.0) as u64;
for (c, (sc, dc)) in src
.chunks_mut(RMAT_CHUNK)
.zip(dst.chunks_mut(RMAT_CHUNK))
.enumerate()
{
let mut r = SplitMix(seed ^ (c as u64 + 1).wrapping_mul(0xA076_1D64_78BD_642F));
for (x, y) in sc.iter_mut().zip(dc.iter_mut()) {
let (mut u, mut v) = (0u32, 0u32);
for _ in 0..scale {
let q = r.next_u64() & 0xFFFF_FFFF;
let (bu, bv) = if q < A {
(0, 0)
} else if q < AB {
(0, 1)
} else if q < ABC {
(1, 0)
} else {
(1, 1)
};
u = (u << 1) | bu;
v = (v << 1) | bv;
}
*x = u;
*y = v;
}
}
let mut perm: Vec<u32> = (0..n as u32).collect();
let mut r = SplitMix(seed.rotate_left(17) ^ 0x5851_F42D_4C95_7F2D);
for i in (1..perm.len()).rev() {
let j = r.below(i as u64 + 1) as usize;
perm.swap(i, j);
}
for x in src.iter_mut().chain(dst.iter_mut()) {
*x = perm[*x as usize];
}
(src, dst)
}
/// `kleene::synth::road`: a `side` x `side` grid, each street kept with probability
/// `keep`, both ways. The benchmark's road sets are `road(side, 0.6, 1)`.
pub fn road_edges(side: u32, keep: f64, seed: u64) -> (Vec<u32>, Vec<u32>) {
let s = side as u64;
let cut = (keep.clamp(0.0, 1.0) * 4294967296.0) as u64;
let kept = |street: u64| {
let mut r = SplitMix(seed ^ street.wrapping_mul(0xD1B5_4A32_D192_ED03));
(r.next_u64() & 0xFFFF_FFFF) < cut
};
let (mut src, mut dst) = (Vec::new(), Vec::new());
for r in 0..s {
for c in 0..s {
let v = (r * s + c) as u32;
if c + 1 < s && kept(2 * v as u64) {
src.extend([v, v + 1]);
dst.extend([v + 1, v]);
}
if r + 1 < s && kept(2 * v as u64 + 1) {
let w = v + side;
src.extend([v, w]);
dst.extend([w, v]);
}
}
}
(src, dst)
}
/// `k` groups of `size` nodes, each node with `per` edges to random nodes of its own group,
/// node `v` in group `v % k` (so the groups are not id blocks), and `joins` edges between
/// random groups.
pub fn grouped_edges(k: u32, size: u32, per: u32, joins: u32, seed: u64) -> (Vec<u32>, Vec<u32>) {
let mut r = SplitMix(seed);
let (mut src, mut dst) = (Vec::new(), Vec::new());
for g in 0..k {
for i in 0..size {
for _ in 0..per {
let j = r.below(size as u64) as u32;
src.push(i * k + g);
dst.push(j * k + g);
}
}
}
for _ in 0..joins {
src.push(r.below((k * size) as u64) as u32);
dst.push(r.below((k * size) as u64) as u32);
}
(src, dst)
}
/// The graph and its transpose of `n` nodes from edge lists (duplicates merged).
pub fn graph(n: usize, src: &[u32], dst: &[u32]) -> (Csr<u8>, Csr<u8>) {
let out = Csr::from_edges(n, n, src, dst, None, BoolOrAnd).expect("edges in range");
let inn = out.transpose();
(out, inn)
}
pub fn rmat(scale: u32) -> (Csr<u8>, Csr<u8>) {
let (s, d) = rmat_edges(scale, 8, 1);
graph(1 << scale, &s, &d)
}
pub fn road(side: u32) -> (Csr<u8>, Csr<u8>) {
let (s, d) = road_edges(side, 0.6, 1);
graph((side * side) as usize, &s, &d)
}
/// 8 groups of 4096 nodes, 8 edges a node inside, 64 edges between groups.
pub fn grouped() -> (Csr<u8>, Csr<u8>) {
let (s, d) = grouped_edges(8, 4096, 8, 64, 7);
graph(8 * 4096, &s, &d)
}
/// An R-MAT 14 graph over 4 times its nodes: three quarters of them have no edge (R-MAT
/// draws self-loops too: they must not vote).
pub fn isolated() -> (Csr<u8>, Csr<u8>) {
let (s, d) = rmat_edges(14, 8, 3);
let s: Vec<u32> = s.iter().map(|&v| v * 4 + 1).collect();
let d: Vec<u32> = d.iter().map(|&v| v * 4 + 1).collect();
graph(4 << 14, &s, &d)
}
/// `n` parts below `k` from a seed.
pub fn parts(n: usize, k: u32, seed: u64) -> Vec<u32> {
let mut r = SplitMix(seed);
(0..n).map(|_| r.below(k as u64) as u32).collect()
}
/// `k` rooms in 0..=65535 from a seed: about a quarter at 65535, an eighth at 0, the
/// rest at random (equal rooms make ties).
pub fn rooms(k: u32, seed: u64) -> Vec<u32> {
let mut r = SplitMix(seed);
(0..k)
.map(|_| match r.below(8) {
0 => 0,
1 | 2 => 65535,
_ => r.below(65536) as u32,
})
.collect()
}
/// The seam's rule written plainly: per node the part `p` of the largest
/// `votes(v, p) * room[p]` over its out- and in-neighbours (a self-loop not counted); ties to its own part, then to
/// the lowest part.
pub fn plain_votes(out: &Csr<u8>, inn: &Csr<u8>, part: &[u32], room: &[u32]) -> Vec<u32> {
let k = room.len();
(0..out.nrows())
.map(|v| {
let mut votes = vec![0u64; k];
for &u in out.row(v).iter().chain(inn.row(v)) {
if u as usize == v {
continue;
}
votes[part[u as usize] as usize] += 1;
}
let own = part[v] as usize;
let mut best = own;
let mut top = votes[own] * room[own] as u64;
for p in 0..k {
let s = votes[p] * room[p] as u64;
if s > top {
top = s;
best = p;
}
}
best as u32
})
.collect()
}