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

nordisk/kleene

crates/kleene-plan/tests/one_part_cost.rs

//! The cost of one part: a reach, weak components and shortest paths on a graph of 2^22
//! vertices and 32M edges, timed through the engine with no split (one part). Run on main
//! before and after the rounds landed to compare:
//! `cargo test --release -p kleene-plan --test one_part_cost -- --ignored --nocapture`.

use std::time::Instant;

use kleene_plan::{Atom, Engine, Facts, Inputs, Plan, Procedure, Term};
use kleene_sparse::{BoolOrAnd, CpuBackend, CpuDevice, Csr};

#[tokio::test]
#[ignore]
async fn one_part_costs_what_the_whole_graph_cost() {
    let n = 1usize << 22;
    let m = 32usize << 20;
    let mut x = 5u64;
    let mut next = || {
        x = x
            .wrapping_mul(6364136223846793005)
            .wrapping_add(1442695040888963407);
        (x >> 33) as u32
    };
    let (mut src, mut dst) = (Vec::with_capacity(m), Vec::with_capacity(m));
    for _ in 0..m {
        src.push(next() % n as u32);
        dst.push(next() % n as u32);
    }
    let out = Csr::<u8>::from_edges(n, n, &src, &dst, None, BoolOrAnd).unwrap();
    let inn = out.transpose();
    let mut p = Plan::new();
    let entry = p.input("entry", 1);
    let e = p.input("e", 2);
    let reach = p.derived("reach", 1);
    let (a, b) = (p.var("A"), p.var("B"));
    let at = |rel, t: &[kleene_plan::Var]| Atom {
        rel,
        terms: t.iter().map(|&v| Term::Var(v)).collect(),
    };
    p.rule(at(reach, &[a]), [at(entry, &[a])]).unwrap();
    p.rule(at(reach, &[b]), [at(reach, &[a]), at(e, &[a, b])])
        .unwrap();
    let mut q = Plan::new();
    let e2 = q.input("e", 2);
    let wcc = q.derived("wcc", 2);
    q.procedure(wcc, e2, Procedure::WeakComponents).unwrap();
    let mut r = Plan::new();
    let e3 = r.input("e", 2);
    let hops = r.derived("hops", 2);
    r.procedure(
        hops,
        e3,
        Procedure::Sssp {
            source: 7,
            weights: None,
        },
    )
    .unwrap();
    let mut inputs = Inputs::new();
    inputs
        .facts(entry, Facts::from_rows(1, [[7u32]]).unwrap())
        .graph(e, out.clone(), Some(inn.clone()));
    let mut qi = Inputs::new();
    qi.graph(e2, out.clone(), Some(inn.clone()));
    let mut ri = Inputs::new();
    ri.graph(e3, out, Some(inn));
    let engine: Engine<CpuBackend> = Engine::new(CpuDevice::default());
    for (name, plan, ins, rel) in [
        ("reach", &p, &inputs, reach),
        ("wcc", &q, &qi, wcc),
        ("sssp", &r, &ri, hops),
    ] {
        let mut t = Vec::new();
        let mut rows = 0;
        for _ in 0..11 {
            let t0 = Instant::now();
            let o = engine.run(plan, ins).await.unwrap();
            t.push(t0.elapsed().as_secs_f64() * 1e3);
            rows = o.get(rel).unwrap().len();
        }
        t.sort_by(f64::total_cmp);
        println!(
            "{name}: {rows} rows, least {:.2} ms, median {:.2} ms of 11",
            t[0], t[5]
        );
    }
}