diff --git a/Cargo.toml b/Cargo.toml index 0eced3e..4b992c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ rand = "0.8.3" rayon = "1.5.0" hwloc2 = "2.2.0" lazy_static = "1.4.0" +num = "0.3" diff --git a/README.md b/README.md index 5e675d4..abebe14 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,14 @@ following command: $ cargo run --release --bin ``` -Additional comparison programs are located in `omp/`, where C comparison -benchmarks can be found. +C comparison benchmarks can be found in `omp/`. + +Additional benchmarks are located in `benches`. These require Rust nightly and +can be run with the following command: + +``` +$ cargo +nightly bench +``` ## Known issues diff --git a/benches/factorial.rs b/benches/factorial.rs new file mode 100644 index 0000000..1e94368 --- /dev/null +++ b/benches/factorial.rs @@ -0,0 +1,57 @@ +#![feature(test)] +extern crate test; + +use num::{BigUint, One}; +use rayon::prelude::*; +use rustmp::par_for; +use std::ops::Mul; + +const N: u32 = 9999; + +// following functions copied from rayon-demo + +/// Compute the Factorial using a plain iterator. +fn factorial(n: u32) -> BigUint { + (1..=n).map(BigUint::from).fold(BigUint::one(), Mul::mul) +} + +#[bench] +/// Benchmark the Factorial using a plain iterator. +fn factorial_iterator(b: &mut test::Bencher) { + let f = factorial(N); + b.iter(|| assert_eq!(factorial(test::black_box(N)), f)); +} + +#[bench] +/// Compute the Factorial using rayon::par_iter. +fn factorial_par_iter(b: &mut test::Bencher) { + fn fact(n: u32) -> BigUint { + (1..n + 1) + .into_par_iter() + .map(BigUint::from) + .reduce_with(Mul::mul) + .unwrap() + } + + let f = factorial(N); + b.iter(|| assert_eq!(fact(test::black_box(N)), f)); +} + +// end functions copied from rayon-demo + +#[bench] +/// Compute the Factorial using rustmp::par_for. +fn factorial_rmp(b: &mut test::Bencher) { + fn fact(n: u32) -> BigUint { + let mut res = BigUint::one(); + par_for! { + for i in 2..n + 1, reduction res#*, { + res *= BigUint::from(i); + } + } + res + } + + let f = factorial(N); + b.iter(|| assert_eq!(fact(test::black_box(N)), f)); +} diff --git a/src/lib.rs b/src/lib.rs index c52fc83..ff01120 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ macro_rules! critical { macro_rules! __reduction_operation { ($f:ident) => {$f}; ($op:tt) => { - |x, &y| {x $op y} + |x, y| {x $op y} } }