From 7e4b3fc25e9059fab0db70d47ce5cf9c292fc65d Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 20 Apr 2021 19:39:33 -0400 Subject: [PATCH 1/2] Added in reduction keyword and a simple example --- src/bin/test_simple.rs | 34 +++++++++++++++-------- src/lib.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/src/bin/test_simple.rs b/src/bin/test_simple.rs index c0d9f5b..d9404e7 100644 --- a/src/bin/test_simple.rs +++ b/src/bin/test_simple.rs @@ -1,6 +1,6 @@ -use rand::Rng; +// use rand::Rng; use rustmp::{par_for, critical}; -use std::time; +// use std::time; #[derive(Debug)] struct Student { @@ -41,14 +41,26 @@ fn main() { println!("{:?}", num); } - //let mut local = 0; - //par_for! { - //for i in 1..32, blocksize 1, capturing numbers, private local, { - //local += 1; - //println!("{}", local); - //let mut lock = numbers.write(); - //lock.push(Student::new(i)); - //println!("Thread {} running!", i); - //} } + let mut x = 0; + // let mut y = 1; + par_for! { + for i in 0..10, reduction x#+, { + // let mut lock = x.write(); + // *lock += 7; + // y *= 6; + x += 2; + } + } + println!("{:?}", x); + + // let mut local = 0; + // par_for! { + // for i in 1..32, blocksize 1, capturing numbers, private local, { + // local += 1; + // println!("{}", local); + // let mut lock = numbers.write(); + // lock.push(Student::new(i)); + // println!("Thread {} running!", i); + // } } } diff --git a/src/lib.rs b/src/lib.rs index 5b17c0a..c0ef82b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,11 +71,13 @@ macro_rules! critical { #[macro_export] macro_rules! __internal_par_for { + // without reduction (var_name($name:ident), iterator($iter:expr), blocksize($size:expr), captured($($captured:ident)*), private($($private:ident)*), + reduction(), $blk:block) => { let mut __rmp_tasks = Vec::new(); $(let $captured = rustmp::Capture::new($captured);)* @@ -95,12 +97,48 @@ macro_rules! __internal_par_for { } $(let $captured = $captured.unwrap();)* }; + // with reduction + (var_name($name:ident), + iterator($iter:expr), + blocksize($size:expr), + captured($($captured:ident)*), + private($($private:ident)*), + reduction($($red_name:ident, $red_op:tt)+), + $blk:block) => { + let mut __rmp_tasks = Vec::new(); + $(let $captured = rustmp::Capture::new($captured);)* + { + let __rmp_tpm_mtx = rustmp::ThreadPoolManager::get_instance_guard(); + let __rmp_tpm = __rmp_tpm_mtx.lock().unwrap(); + let __rmp_iters = __rmp_tpm.split_iterators($iter, $size); + let __rmp_red_vals = rustmp::Capture::new(Vec::with_capacity(__rmp_iters.len())); + for iter in __rmp_iters { + $(let $captured = $captured.clone();)* + let __rmp_red_vals = __rmp_red_vals.clone(); + __rmp_tasks.push(rustmp::as_static_job(move || { + $(let mut $private = $private.clone();)* + $(let mut $red_name = $red_name.clone();)* + for &$name in &iter + $blk + let mut __rmp_temp = __rmp_red_vals.write(); + $(__rmp_temp.push($red_name);)* + })); + } + __rmp_tpm.exec(__rmp_tasks); + let mut __rmp_temp = __rmp_red_vals.read(); + for i in 0..__rmp_temp.len() { + $($red_name = $red_name $red_op __rmp_temp[i];)* + } + } + $(let $captured = $captured.unwrap();)* + }; // Parse blocksize (var_name($name:ident), iterator($iter:expr), blocksize($size:expr), captured($($captured:ident)*), private($($private:ident)*), + reduction($($red_name:ident, $red_op:tt)*), blocksize $new_size:expr, $($rem:tt)+) => { rustmp::__internal_par_for!( @@ -109,6 +147,7 @@ macro_rules! __internal_par_for { blocksize($new_size), captured($($captured)*), private($($private)*), + reduction($($red_name, $red_op)*), $($rem)*) }; // Parse capturing @@ -117,6 +156,7 @@ macro_rules! __internal_par_for { blocksize($size:expr), captured($($captured:ident)*), private($($private:ident)*), + reduction($($red_name:ident, $red_op:tt)*), capturing $($new_captured:ident)*, $($rem:tt)+) => { rustmp::__internal_par_for!( @@ -125,6 +165,7 @@ macro_rules! __internal_par_for { blocksize($size), captured($($new_captured)*), private($($private)*), + reduction($($red_name, $red_op)*), $($rem)*) }; // Parse private @@ -133,6 +174,7 @@ macro_rules! __internal_par_for { blocksize($size:expr), captured($($captured:ident)*), private($($private:ident)*), + reduction($($red_name:ident, $red_op:tt)*), private $($new_private:ident)*, $($rem:tt)+) => { rustmp::__internal_par_for!( @@ -141,6 +183,25 @@ macro_rules! __internal_par_for { blocksize($size), captured($($captured)*), private($($new_private)*), + reduction($($red_name, $red_op)*), + $($rem)*) + }; + // Parse reduction + (var_name($name:ident), + iterator($iter:expr), + blocksize($size:expr), + captured($($captured:ident)*), + private($($private:ident)*), + reduction($($red_name:ident, $red_op:tt)*), + reduction $($new_name:ident#$new_op:tt)*, + $($rem:tt)+) => { + rustmp::__internal_par_for!( + var_name($name), + iterator($iter), + blocksize($size), + captured($($captured)*), + private($($private)*), + reduction($($new_name, $new_op)*), $($rem)*) }; } @@ -158,6 +219,7 @@ macro_rules! par_for { blocksize(1), captured(), private(), + reduction(), $($rem)*) } } From ab10c5fef3470f2e3f0549f922b32f2475ea11ac Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 22 Apr 2021 12:26:16 -0400 Subject: [PATCH 2/2] Fixed minor issues in reduction, added pseudo matmul test --- src/bin/test_simple.rs | 20 +++++++++++--------- src/lib.rs | 15 +++++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/bin/test_simple.rs b/src/bin/test_simple.rs index d9404e7..cf31015 100644 --- a/src/bin/test_simple.rs +++ b/src/bin/test_simple.rs @@ -18,7 +18,6 @@ impl Student { } } } - fn main() { let numbers: Vec = vec![]; @@ -41,18 +40,21 @@ fn main() { println!("{:?}", num); } + let a:Vec> = vec![vec![1,2,3],vec![4,5,6],vec![7,8,9]]; + let n = a.len(); + let mut c = vec![vec![0;n];n]; let mut x = 0; - // let mut y = 1; par_for! { - for i in 0..10, reduction x#+, { - // let mut lock = x.write(); - // *lock += 7; - // y *= 6; - x += 2; + // I feel like we shouldn't need to capture a here + for k in 0..n, capturing a, reduction x#+, { + critical! { + read a; + x += a[1][k]*a[k][0]; + } } } - println!("{:?}", x); - + c[1][0] = x; + println!("{:?}", c[1][0]); // let mut local = 0; // par_for! { // for i in 1..32, blocksize 1, capturing numbers, private local, { diff --git a/src/lib.rs b/src/lib.rs index c0ef82b..b666278 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,7 +111,10 @@ macro_rules! __internal_par_for { let __rmp_tpm_mtx = rustmp::ThreadPoolManager::get_instance_guard(); let __rmp_tpm = __rmp_tpm_mtx.lock().unwrap(); let __rmp_iters = __rmp_tpm.split_iterators($iter, $size); - let __rmp_red_vals = rustmp::Capture::new(Vec::with_capacity(__rmp_iters.len())); + let mut __rmp_red_vals = Vec::new(); + // let mut __rmp_count = 0; + $(__rmp_red_vals.push(Vec::new()); $red_name = $red_name;)* + let __rmp_red_vals = rustmp::Capture::new(__rmp_red_vals); for iter in __rmp_iters { $(let $captured = $captured.clone();)* let __rmp_red_vals = __rmp_red_vals.clone(); @@ -121,14 +124,14 @@ macro_rules! __internal_par_for { for &$name in &iter $blk let mut __rmp_temp = __rmp_red_vals.write(); - $(__rmp_temp.push($red_name);)* + let mut __rmp_counter = 0; + $(__rmp_temp[__rmp_counter].push($red_name); __rmp_counter += 1;)* })); } __rmp_tpm.exec(__rmp_tasks); let mut __rmp_temp = __rmp_red_vals.read(); - for i in 0..__rmp_temp.len() { - $($red_name = $red_name $red_op __rmp_temp[i];)* - } + let mut __rmp_counter = 0; + $($red_name = __rmp_temp[__rmp_counter].iter().fold($red_name, |x, &y| {x $red_op y}); __rmp_counter += 1;)*; } $(let $captured = $captured.unwrap();)* }; @@ -193,7 +196,7 @@ macro_rules! __internal_par_for { captured($($captured:ident)*), private($($private:ident)*), reduction($($red_name:ident, $red_op:tt)*), - reduction $($new_name:ident#$new_op:tt)*, + reduction $($new_name:ident#$new_op:tt);*, $($rem:tt)+) => { rustmp::__internal_par_for!( var_name($name),