From 523a1f590948b8f3e314b5619f4e0bd0ff564e4b Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Sun, 19 Mar 2023 23:29:12 -0700 Subject: [PATCH 1/2] Getting the Iai tutorial to work on CI --- .github/workflows/bench.yml | 24 ++++++++++++++++++++++++ Cargo.toml | 6 ++++++ benches/iai.rs | 19 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 .github/workflows/bench.yml create mode 100644 benches/iai.rs diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml new file mode 100644 index 00000000..60ebbafa --- /dev/null +++ b/.github/workflows/bench.yml @@ -0,0 +1,24 @@ +name: CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +env: + CARGO_TERM_COLOR: always + +jobs: + iai: + name: Bench (iai) + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Install Valgrind + run: sudo apt install valgrind # sudo is necessary! + - name: Build + run: cargo build --verbose + - name: Bench + run: cargo bench --verbose diff --git a/Cargo.toml b/Cargo.toml index 55339849..164da57e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ petgraph = "0.6.2" rsgm = { git = "https://github.com/pmall-neu/rsgm" } rand_chacha = "0.3.1" +[dev-dependencies] +iai = "0.1" [lib] name = "rsdd" @@ -59,3 +61,7 @@ path = "bin/bayesian_network_compiler.rs" [[bin]] name = "semantic_hash_experiment" path = "bin/semantic_hash_experiment.rs" + +[[bench]] +name = "iai" +harness = false diff --git a/benches/iai.rs b/benches/iai.rs new file mode 100644 index 00000000..b8c55d45 --- /dev/null +++ b/benches/iai.rs @@ -0,0 +1,19 @@ +use iai::black_box; + +fn fibonacci(n: u64) -> u64 { + match n { + 0 => 1, + 1 => 1, + n => fibonacci(n - 1) + fibonacci(n - 2), + } +} + +fn iai_benchmark_short() -> u64 { + fibonacci(black_box(10)) +} + +fn iai_benchmark_long() -> u64 { + fibonacci(black_box(30)) +} + +iai::main!(iai_benchmark_short, iai_benchmark_long); From 1d5570315fab29aa915f4fb170365b5072c7a16d Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Sun, 19 Mar 2023 23:52:25 -0700 Subject: [PATCH 2/2] First attempt with sdd canonicity benchmark --- benches/iai.rs | 89 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/benches/iai.rs b/benches/iai.rs index b8c55d45..fe7ed514 100644 --- a/benches/iai.rs +++ b/benches/iai.rs @@ -1,19 +1,86 @@ use iai::black_box; +use rsdd::{ + builder::{canonicalize::CompressionCanonicalizer, sdd_builder::SddManager}, + repr::{cnf::Cnf, var_label::VarLabel, vtree::VTree}, +}; -fn fibonacci(n: u64) -> u64 { - match n { - 0 => 1, - 1 => 1, - n => fibonacci(n - 1) + fibonacci(n - 2), - } +// subset of DIMACS from CNFs from test.rs +static C1_A: &str = " +p cnf 5 3 +1 2 0 +-1 2 0 +"; + +static C1_B: &str = " +p cnf 2 1 +2 0 +"; + +static C2_A: &str = " +p cnf 5 3 +1 2 3 0 +1 2 0 +-1 2 0 +"; + +static C2_B: &str = " +p cnf 2 1 +2 0 +"; + +static C3_A: &str = " +p cnf 5 3 +1 2 3 4 5 0 +1 2 0 +-1 2 0 +"; + +static C3_B: &str = " +p cnf 2 1 +2 0 +"; + +fn sdd_benchmark_helper(cnf1: Cnf, cnf2: Cnf) { + let v: Vec = (0..cnf1.num_vars()) + .map(|x| VarLabel::new(x as u64)) + .collect(); + let vtree = VTree::even_split(&v, 1); + let mut man = SddManager::::new(vtree); + let r1 = man.from_cnf(&cnf1); + let r2 = man.from_cnf(&cnf2); + assert!( + man.sdd_eq(r1, r2), + "Not eq\nCNF 1: {:?}\nCNF 2: {:?}\nSDD 1:{}\n SDD 2: {}", + cnf1, + cnf2, + man.print_sdd(r1), + man.print_sdd(r2) + ); +} + +fn iai_benchmark_sdd_canonicity_c1() { + sdd_benchmark_helper( + Cnf::from_file(String::from(black_box(C1_A))), + Cnf::from_file(String::from(black_box(C1_B))), + ) } -fn iai_benchmark_short() -> u64 { - fibonacci(black_box(10)) +fn iai_benchmark_sdd_canonicity_c2() { + sdd_benchmark_helper( + Cnf::from_file(String::from(black_box(C2_A))), + Cnf::from_file(String::from(black_box(C2_B))), + ) } -fn iai_benchmark_long() -> u64 { - fibonacci(black_box(30)) +fn iai_benchmark_sdd_canonicity_c3() { + sdd_benchmark_helper( + Cnf::from_file(String::from(black_box(C3_A))), + Cnf::from_file(String::from(black_box(C3_B))), + ) } -iai::main!(iai_benchmark_short, iai_benchmark_long); +iai::main!( + iai_benchmark_sdd_canonicity_c1, + iai_benchmark_sdd_canonicity_c2, + iai_benchmark_sdd_canonicity_c3 +);