mirror of https://github.com/xythrez/RustMP.git
Removed imports to ensure generated code consistency
This commit is contained in:
parent
31fca0b6d7
commit
570e884156
|
@ -1,8 +1,10 @@
|
|||
use std::sync::*;
|
||||
//use std::sync::RwLock;
|
||||
use std::sync::atomic::*;
|
||||
use std::thread;
|
||||
//use rustmp::rmp_parallel_for;
|
||||
// libraries used:
|
||||
// std::sync::Arc;
|
||||
// std::sync::RwLock;
|
||||
// std::sync::atomic::AtomicIsize;
|
||||
// std::sync::atomic::AtomicI32;
|
||||
// std::sync::atomic::Ordering;
|
||||
// std::thread;
|
||||
|
||||
/*
|
||||
* A basic sequential function that we want to modify.
|
||||
|
@ -35,7 +37,9 @@ fn aug_main() {
|
|||
for i in 0..4 {
|
||||
println!("Index {}: Hello from loop {}!", counter, i);
|
||||
*counter += 1;
|
||||
}} _loop(&mut counter);
|
||||
}
|
||||
}
|
||||
_loop(&mut counter);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -66,7 +70,7 @@ fn rmp_main() {
|
|||
|
||||
// Startup - Promote shared mutables into Arc references
|
||||
// Idea - Possible optimization based on type? RwLock is expensive.
|
||||
let __rmp_var_counter = Arc::new(AtomicI32::new(*counter));
|
||||
let __rmp_var_counter = std::sync::Arc::new(std::sync::atomic::AtomicI32::new(*counter));
|
||||
|
||||
// Execution - Precompute the iterations for each loop
|
||||
// The 0..4 here should be parsed from the original tokens
|
||||
|
@ -74,7 +78,8 @@ fn rmp_main() {
|
|||
__rmp_internal_iter_arr[__rmp_internal_curr_block_thread].push(__rmp_internal_i);
|
||||
__rmp_internal_curr_block_size += 1;
|
||||
if __rmp_internal_curr_block_size >= __rmp_internal_block_size {
|
||||
__rmp_internal_curr_block_thread = (__rmp_internal_curr_block_thread + 1) % __rmp_internal_max_threads;
|
||||
__rmp_internal_curr_block_thread =
|
||||
(__rmp_internal_curr_block_thread + 1) % __rmp_internal_max_threads;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,25 +89,33 @@ fn rmp_main() {
|
|||
// Execution - Spawn threads with loop contents
|
||||
for __rmp_internal_iter in __rmp_internal_iter_arr {
|
||||
// Clone used Arcs here
|
||||
let __rmp_var_counter = Arc::clone(&__rmp_var_counter);
|
||||
let __rmp_var_counter = std::sync::Arc::clone(&__rmp_var_counter);
|
||||
|
||||
// Spawn threads
|
||||
__rmp_internal_threads_arr.push(thread::spawn(move || {
|
||||
__rmp_internal_threads_arr.push(std::thread::spawn(move || {
|
||||
for i in __rmp_internal_iter {
|
||||
// Having separate load and fetch_add should be a data race,
|
||||
// However, I believe OpenMP also treats it as a data race,
|
||||
// so its fine to have this issue
|
||||
// Need to implement #[rmp_critical] to update it correctly
|
||||
println!("Index {}: Hello from loop {}!", __rmp_var_counter.load(Ordering::SeqCst), i);
|
||||
__rmp_var_counter.fetch_add(1, Ordering::SeqCst);
|
||||
println!(
|
||||
"Index {}: Hello from loop {}!",
|
||||
__rmp_var_counter.load(std::sync::atomic::Ordering::SeqCst),
|
||||
i
|
||||
);
|
||||
__rmp_var_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Execution - Extract the same thread logic for self
|
||||
for i in __rmp_internal_iter_self {
|
||||
println!("Index {}: Hello from loop {}!", __rmp_var_counter.load(Ordering::SeqCst), i);
|
||||
__rmp_var_counter.fetch_add(1, Ordering::SeqCst);
|
||||
println!(
|
||||
"Index {}: Hello from loop {}!",
|
||||
__rmp_var_counter.load(std::sync::atomic::Ordering::SeqCst),
|
||||
i
|
||||
);
|
||||
__rmp_var_counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
|
||||
// Cleanup - Wait for threads
|
||||
|
@ -111,8 +124,9 @@ fn rmp_main() {
|
|||
}
|
||||
|
||||
// Cleanup - Restore variables from Arc references
|
||||
*counter = __rmp_var_counter.load(Ordering::SeqCst);
|
||||
} _loop(&mut counter);
|
||||
*counter = __rmp_var_counter.load(std::sync::atomic::Ordering::SeqCst);
|
||||
}
|
||||
_loop(&mut counter);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -120,17 +134,17 @@ fn rmp_main() {
|
|||
* possible.
|
||||
*/
|
||||
fn par_main() {
|
||||
let counter = Arc::new(AtomicIsize::new(0));
|
||||
let counter = std::sync::Arc::new(std::sync::atomic::AtomicIsize::new(0));
|
||||
let mut children = vec![];
|
||||
|
||||
for i in 1..4 {
|
||||
let counter = Arc::clone(&counter);
|
||||
children.push(thread::spawn(move || {
|
||||
let index = counter.fetch_add(1, Ordering::SeqCst);
|
||||
let counter = std::sync::Arc::clone(&counter);
|
||||
children.push(std::thread::spawn(move || {
|
||||
let index = counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
println!("Index {}: Hello from loop {}!", index, i);
|
||||
}));
|
||||
}
|
||||
let index = counter.fetch_add(1, Ordering::SeqCst);
|
||||
let index = counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
println!("Index {}: Hello from loop {}!", index, 0);
|
||||
|
||||
for child in children {
|
||||
|
@ -138,7 +152,6 @@ fn par_main() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
println!("Running Sequential Version:");
|
||||
seq_main();
|
||||
|
|
Loading…
Reference in New Issue