Merge branch 'alex' into jack

This commit is contained in:
Jack Yu 2021-04-23 11:25:25 -04:00
commit ab669a038a
2 changed files with 91 additions and 12 deletions

View File

@ -1,6 +1,6 @@
use rand::Rng; // use rand::Rng;
use rustmp::{par_for, critical}; use rustmp::{par_for, critical};
use std::time; // use std::time;
#[derive(Debug)] #[derive(Debug)]
struct Student { struct Student {
@ -18,7 +18,6 @@ impl Student {
} }
} }
} }
fn main() { fn main() {
let numbers: Vec<Student> = vec![]; let numbers: Vec<Student> = vec![];
@ -41,14 +40,29 @@ fn main() {
println!("{:?}", num); println!("{:?}", num);
} }
//let mut local = 0; let a:Vec<Vec<i32>> = vec![vec![1,2,3],vec![4,5,6],vec![7,8,9]];
//par_for! { let n = a.len();
//for i in 1..32, blocksize 1, capturing numbers, private local, { let mut c = vec![vec![0;n];n];
//local += 1; let mut x = 0;
//println!("{}", local); par_for! {
//let mut lock = numbers.write(); // I feel like we shouldn't need to capture a here
//lock.push(Student::new(i)); for k in 0..n, capturing a, reduction x#+, {
//println!("Thread {} running!", i); critical! {
//} } read a;
x += a[1][k]*a[k][0];
}
}
}
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, {
// local += 1;
// println!("{}", local);
// let mut lock = numbers.write();
// lock.push(Student::new(i));
// println!("Thread {} running!", i);
// } }
} }

View File

@ -71,11 +71,13 @@ macro_rules! critical {
#[macro_export] #[macro_export]
macro_rules! __internal_par_for { macro_rules! __internal_par_for {
// without reduction
(var_name($name:ident), (var_name($name:ident),
iterator($iter:expr), iterator($iter:expr),
blocksize($size:expr), blocksize($size:expr),
captured($($captured:ident)*), captured($($captured:ident)*),
private($($private:ident)*), private($($private:ident)*),
reduction(),
$blk:block) => { $blk:block) => {
let mut __rmp_tasks = Vec::new(); let mut __rmp_tasks = Vec::new();
$(let $captured = rustmp::Capture::new($captured);)* $(let $captured = rustmp::Capture::new($captured);)*
@ -95,12 +97,51 @@ macro_rules! __internal_par_for {
} }
$(let $captured = $captured.unwrap();)* $(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 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();
__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();
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();
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();)*
};
// Parse blocksize // Parse blocksize
(var_name($name:ident), (var_name($name:ident),
iterator($iter:expr), iterator($iter:expr),
blocksize($size:expr), blocksize($size:expr),
captured($($captured:ident)*), captured($($captured:ident)*),
private($($private:ident)*), private($($private:ident)*),
reduction($($red_name:ident, $red_op:tt)*),
blocksize $new_size:expr, blocksize $new_size:expr,
$($rem:tt)+) => { $($rem:tt)+) => {
rustmp::__internal_par_for!( rustmp::__internal_par_for!(
@ -109,6 +150,7 @@ macro_rules! __internal_par_for {
blocksize($new_size), blocksize($new_size),
captured($($captured)*), captured($($captured)*),
private($($private)*), private($($private)*),
reduction($($red_name, $red_op)*),
$($rem)*) $($rem)*)
}; };
// Parse capturing // Parse capturing
@ -117,6 +159,7 @@ macro_rules! __internal_par_for {
blocksize($size:expr), blocksize($size:expr),
captured($($captured:ident)*), captured($($captured:ident)*),
private($($private:ident)*), private($($private:ident)*),
reduction($($red_name:ident, $red_op:tt)*),
capturing $($new_captured:ident)*, capturing $($new_captured:ident)*,
$($rem:tt)+) => { $($rem:tt)+) => {
rustmp::__internal_par_for!( rustmp::__internal_par_for!(
@ -125,6 +168,7 @@ macro_rules! __internal_par_for {
blocksize($size), blocksize($size),
captured($($new_captured)*), captured($($new_captured)*),
private($($private)*), private($($private)*),
reduction($($red_name, $red_op)*),
$($rem)*) $($rem)*)
}; };
// Parse private // Parse private
@ -133,6 +177,7 @@ macro_rules! __internal_par_for {
blocksize($size:expr), blocksize($size:expr),
captured($($captured:ident)*), captured($($captured:ident)*),
private($($private:ident)*), private($($private:ident)*),
reduction($($red_name:ident, $red_op:tt)*),
private $($new_private:ident)*, private $($new_private:ident)*,
$($rem:tt)+) => { $($rem:tt)+) => {
rustmp::__internal_par_for!( rustmp::__internal_par_for!(
@ -141,6 +186,25 @@ macro_rules! __internal_par_for {
blocksize($size), blocksize($size),
captured($($captured)*), captured($($captured)*),
private($($new_private)*), 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)*) $($rem)*)
}; };
} }
@ -158,6 +222,7 @@ macro_rules! par_for {
blocksize(1), blocksize(1),
captured(), captured(),
private(), private(),
reduction(),
$($rem)*) $($rem)*)
} }
} }