Skip to content

Commit

Permalink
Serial version complete
Browse files Browse the repository at this point in the history
  • Loading branch information
adityauj committed Sep 11, 2024
1 parent 305eb8e commit f4cca43
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 26 deletions.
Binary file modified bench
Binary file not shown.
59 changes: 59 additions & 0 deletions src/kernels/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#[allow(unused_assignments, clippy::ptr_arg)]
pub fn check(a: &Vec<f64>, b: &Vec<f64>, c: &Vec<f64>, d: &Vec<f64>, n: usize, ntimes: usize) {
/* reproduce initialization */
let mut aj = 2.0;
let mut bj = 2.0;
let mut cj = 0.5;
let mut dj = 1.0;

let mut asum = 0.0;
let mut bsum = 0.0;
let mut csum = 0.0;
let mut dsum = 0.0;
let epsilon = 1e-8;

/* now execute timing loop */
let scalar = 3.0;

for _ in 0..ntimes {
bj = scalar;
cj = aj;
aj *= scalar;
aj = bj + scalar * cj;
aj += scalar * bj;
aj = bj + cj * dj;
aj += bj * cj;
}

aj *= n as f64;
bj *= n as f64;
cj *= n as f64;
dj *= n as f64;

for i in 0..n {
asum += a[i];
bsum += b[i];
csum += c[i];
dsum += d[i];
}

if f64::abs(aj - asum) / asum > epsilon {
println!("Failed Validation on array a[]\n");
println!(" Expected : {} \n", aj);
println!(" Observed : {} \n", asum);
} else if f64::abs(bj - bsum) / bsum > epsilon {
println!("Failed Validation on array b[]\n");
println!(" Expected : {} \n", bj);
println!(" Observed : {} \n", bsum);
} else if f64::abs(cj - csum) / csum > epsilon {
println!("Failed Validation on array c[]\n");
println!(" Expected : {} \n", cj);
println!(" Observed : {} \n", csum);
} else if f64::abs(dj - dsum) / dsum > epsilon {
println!("Failed Validation on array d[]\n");
println!(" Expected : {} \n", dj);
println!(" Observed : {} \n", dsum);
} else {
println!("Solution Validates\n");
}
}
2 changes: 1 addition & 1 deletion src/kernels/copy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
#[allow(clippy::ptr_arg, clippy::manual_memcpy)]
pub fn copy(c: &mut Vec<f64>, a: &Vec<f64>, n: usize) -> f64 {
let s = Instant::now();

Expand Down
1 change: 1 addition & 0 deletions src/kernels/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod striad;
pub mod sum;
pub mod triad;
pub mod update;
pub mod check;
71 changes: 48 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod kernels;
mod utils;

use clap::Parser;
use kernels::check::check;
use kernels::copy::copy;
use kernels::daxpy::daxpy;
use kernels::init::init;
Expand All @@ -17,8 +18,7 @@ use std::time::Instant;
use crate::utils::arg_parser::ArgParser;
use crate::utils::benchmark::{Benchmark, BenchmarkType};

const HLINE: &str =
"----------------------------------------------------------------------------\n";
const HLINE: &str = "----------------------------------------------------------------------------";

macro_rules! bench {
($tag:expr, $func:expr, $times:expr, $index:expr) => {
Expand All @@ -37,7 +37,8 @@ fn main() {
println!("Benchmarking with {:#?} workers.", arg_parser.n);

const BYTES_PER_WORD: usize = size_of::<f64>();
let n: usize = arg_parser.size;
let n = arg_parser.size;
let ntimes = arg_parser.ntimes;

let num_of_benchmarks = Benchmark::Numbench as usize;

Expand All @@ -55,44 +56,45 @@ fn main() {
BenchmarkType {
label: "Sum: ".to_string(),
words: 1,
flops: 0,
flops: 1,
},
BenchmarkType {
label: "Copy: ".to_string(),
words: 1,
words: 2,
flops: 0,
},
BenchmarkType {
label: "Update: ".to_string(),
words: 1,
flops: 0,
words: 2,
flops: 1,
},
BenchmarkType {
label: "Triad: ".to_string(),
words: 1,
flops: 0,
words: 2,
flops: 2,
},
BenchmarkType {
label: "Daxpy: ".to_string(),
words: 1,
flops: 0,
words: 3,
flops: 2,
},
BenchmarkType {
label: "STriad: ".to_string(),
words: 1,
flops: 0,
words: 4,
flops: 2,
},
BenchmarkType {
label: "SDaxpy: ".to_string(),
words: 1,
flops: 0,
words: 4,
flops: 2,
},
];

let s = Instant::now();

// Can also randomise the initialisation of arrays with rand crate : https://docs.rs/rand/0.8.5/rand/
// let mut x: Arc<Vec<f64>> = Arc::new((0..n).into_par_iter().map(|_| (rand::random::<i32>() % 100) as f64 + 1.1).collect());
// But randomising will fail the check function at the end.

let mut a: Vec<f64> = (0..n).into_par_iter().map(|_| 2.0).collect();
let mut b: Vec<f64> = (0..n).into_par_iter().map(|_| 2.0).collect();
Expand All @@ -105,7 +107,7 @@ fn main() {

let scalar = 3.0;

for k in 0..arg_parser.ntimes {
for k in 0..ntimes {
bench!(
Benchmark::Init as usize,
init(b.as_mut(), scalar, n),
Expand Down Expand Up @@ -157,20 +159,43 @@ fn main() {
);

for j in 0..num_of_benchmarks {
for k in 0..arg_parser.ntimes {
for k in 0..ntimes {
avgtime[j] += times[j][k];
mintime[j] = f64::min(mintime[j], times[j][k]);
maxtime[j] = f64::max(maxtime[j], times[j][k]);
}
}
}

println!("{HLINE:#?}");
println!("{HLINE}");
println!("Function Rate(MB/s) Rate(MFlop/s) Avg time Min time Max time\n");
for j in 0..num_of_benchmarks {
avgtime[j] /= (ntimes - 1) as f64;
let bytes = benchmarks[j].words * BYTES_PER_WORD * n;
let flops = benchmarks[j].flops * n;

if flops > 0 {
println!(
"{} {:.2} {:.2} {:.4} {:.4} {:.4}",
benchmarks[j].label,
1e-6 * bytes as f64 / mintime[j],
1e-6 * flops as f64 / mintime[j],
avgtime[j],
mintime[j],
maxtime[j]
);
} else {
println!(
"{} {:.2} - {:.4} {:.4} {:.4}",
benchmarks[j].label,
1e-6 * bytes as f64 / mintime[j],
avgtime[j],
mintime[j],
maxtime[j]
);
}
}
println!("{HLINE}");






check(a.as_ref(), b.as_ref(), c.as_ref(), d.as_ref(), n, ntimes);
}
4 changes: 2 additions & 2 deletions src/utils/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ pub enum Benchmark {

pub struct BenchmarkType {
pub label: String,
pub words: i8,
pub flops: i8,
pub words: usize,
pub flops: usize,
}

0 comments on commit f4cca43

Please sign in to comment.