Skip to content

Commit

Permalink
Parallel Try
Browse files Browse the repository at this point in the history
  • Loading branch information
adityauj committed Sep 11, 2024
1 parent d048ff1 commit 9e449ca
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 48 deletions.
6 changes: 5 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
[build]
rustflags = ["-C", "target-cpu=native"]
rustflags = ["-C", "target-cpu=native"]

# Can also specify your simd flags like avx512 etc.
# https://rust-lang.github.io/packed_simd/perf-guide/target-feature/rustflags.html
# rustflags = ["-C", "target-cpu=native", "-C", "target-feature=+avx,+fma"]
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ path = "src/main.rs"
[profile.release]
debug = false
lto = true
# codegen-units = 1
incremental = false
Binary file modified bench
Binary file not shown.
13 changes: 9 additions & 4 deletions src/kernels/copy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::time::Instant;

#[allow(clippy::ptr_arg, clippy::manual_memcpy)]
use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};

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

for i in 0..n {
c[i] = a[i];
}
c.par_iter_mut().enumerate().for_each(|(i, x)| *x = a[i]);

// Serial version
// for i in 0..n {
// c[i] = a[i];
// }

s.elapsed().as_secs_f64()
}
17 changes: 12 additions & 5 deletions src/kernels/daxpy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
pub fn daxpy(a: &mut Vec<f64>, b: &mut Vec<f64>, scalar: f64, n: usize) -> f64 {
use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};

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

for i in 0..n {
a[i] += scalar * b[i];
}
a.par_iter_mut()
.enumerate()
.for_each(|(i, x)| *x += scalar * b[i]);

// Serial version
// for i in 0..n {
// a[i] += scalar * b[i];
// }

s.elapsed().as_secs_f64()
}
13 changes: 9 additions & 4 deletions src/kernels/init.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};

#[allow(clippy::ptr_arg, unused_variables)]
pub fn init(b: &mut Vec<f64>, scalar: f64, n: usize) -> f64 {
let s = Instant::now();

for i in b.iter_mut().take(n) {
*i = scalar;
}
b.par_iter_mut().for_each(|x| *x = scalar);

// Serial version
// for i in b.iter_mut().take(n) {
// *i = scalar;
// }

s.elapsed().as_secs_f64()
}
17 changes: 12 additions & 5 deletions src/kernels/sdaxpy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
pub fn sdaxpy(a: &mut Vec<f64>, b: &mut Vec<f64>, c: &mut Vec<f64>, n: usize) -> f64 {
use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};

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

for i in 0..n {
a[i] += b[i] * c[i];
}
a.par_iter_mut()
.enumerate()
.for_each(|(i, x)| *x += b[i] * c[i]);

// Serial version
// for i in 0..n {
// a[i] += b[i] * c[i];
// }

s.elapsed().as_secs_f64()
}
23 changes: 12 additions & 11 deletions src/kernels/striad.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
pub fn striad(
a: &mut Vec<f64>,
b: &mut Vec<f64>,
c: &mut Vec<f64>,
d: &mut Vec<f64>,
n: usize,
) -> f64 {
use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};

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

for i in 0..n {
a[i] = b[i] + d[i] * c[i];
}
a.par_iter_mut()
.enumerate()
.for_each(|(i, x)| *x = b[i] + d[i] * c[i]);

// Serial version
// for i in 0..n {
// a[i] = b[i] + d[i] * c[i];
// }

s.elapsed().as_secs_f64()
}
15 changes: 10 additions & 5 deletions src/kernels/sum.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};

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

let mut sum = 0.0;
for i in a.iter().take(n) {
sum += *i;
}
let sum = a.par_iter().sum();

// Serial version
// let mut sum = 0.0;
// for i in a.iter().take(n) {
// sum += *i;
// }

let e = s.elapsed();

Expand Down
15 changes: 11 additions & 4 deletions src/kernels/triad.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
use rayon::iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator};

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

for i in 0..n {
a[i] = b[i] + scalar * c[i];
}
a.par_iter_mut()
.enumerate()
.for_each(|(i, x)| *x = b[i] + scalar * c[i]);

// Serial version
// for i in 0..n {
// a[i] = b[i] + scalar * c[i];
// }

s.elapsed().as_secs_f64()
}
13 changes: 9 additions & 4 deletions src/kernels/update.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::time::Instant;

#[allow(clippy::ptr_arg)]
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};

#[allow(clippy::ptr_arg, unused_variables)]
pub fn update(b: &mut Vec<f64>, scalar: f64, n: usize) -> f64 {
let s = Instant::now();

for i in b.iter_mut().take(n) {
*i = scalar;
}
b.par_iter_mut().for_each(|x| *x += scalar);

// Serial version
// for i in b.iter_mut().take(n) {
// *i += scalar;
// }

s.elapsed().as_secs_f64()
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn main() {
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();
let mut c: Vec<f64> = (0..n).into_par_iter().map(|_| 0.5).collect();
let mut d: Vec<f64> = (0..n).into_par_iter().map(|_| 1.0).collect();
let d: Vec<f64> = (0..n).into_par_iter().map(|_| 1.0).collect();

let e = s.elapsed();
println!(
Expand Down Expand Up @@ -146,19 +146,19 @@ fn main() {
);
bench!(
Benchmark::Daxpy as usize,
daxpy(a.as_mut(), b.as_mut(), scalar, n),
daxpy(a.as_mut(), b.as_ref(), scalar, n),
times,
k
);
bench!(
Benchmark::Striad as usize,
striad(a.as_mut(), b.as_mut(), c.as_mut(), d.as_mut(), n),
striad(a.as_mut(), b.as_ref(), c.as_ref(), d.as_ref(), n),
times,
k
);
bench!(
Benchmark::Sdaxpy as usize,
sdaxpy(a.as_mut(), b.as_mut(), c.as_mut(), n),
sdaxpy(a.as_mut(), b.as_ref(), c.as_ref(), n),
times,
k
);
Expand Down

0 comments on commit 9e449ca

Please sign in to comment.