mirror of
				https://github.com/xythrez/RustMP.git
				synced 2025-11-03 21:41:01 +00:00 
			
		
		
		
	Removed imports for multiple calls to par_for!, renamed variables to
avoid clashing, added blocksize parameter
This commit is contained in:
		
					parent
					
						
							
								bb1391b8b2
							
						
					
				
			
			
				commit
				
					
						e23b39ee41
					
				
			
		
					 3 changed files with 50 additions and 33 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
					@ -1,2 +1,3 @@
 | 
				
			||||||
/target
 | 
					/target
 | 
				
			||||||
Cargo.lock
 | 
					Cargo.lock
 | 
				
			||||||
 | 
					/.idea
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,7 +12,7 @@ struct Student {
 | 
				
			||||||
impl Student {
 | 
					impl Student {
 | 
				
			||||||
    pub fn new(age: u8) -> Student {
 | 
					    pub fn new(age: u8) -> Student {
 | 
				
			||||||
    Student { name: "Default".to_string(),
 | 
					    Student { name: "Default".to_string(),
 | 
				
			||||||
          age: age,
 | 
					          age,
 | 
				
			||||||
          gpa: age as f32 }
 | 
					          gpa: age as f32 }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -21,8 +21,7 @@ fn main() {
 | 
				
			||||||
    let numbers: Vec<Student> = vec![];
 | 
					    let numbers: Vec<Student> = vec![];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    par_for! {
 | 
					    par_for! {
 | 
				
			||||||
    for i in 1..10, capturing numbers {
 | 
					    for i in 1..32, capturing numbers {
 | 
				
			||||||
 | 
					 | 
				
			||||||
    //std::thread::sleep(
 | 
					    //std::thread::sleep(
 | 
				
			||||||
    //    time::Duration::from_secs(
 | 
					    //    time::Duration::from_secs(
 | 
				
			||||||
    //    rand::thread_rng().gen_range(1..10)));
 | 
					    //    rand::thread_rng().gen_range(1..10)));
 | 
				
			||||||
| 
						 | 
					@ -31,6 +30,13 @@ fn main() {
 | 
				
			||||||
    println!("Thread {} running!", i);
 | 
					    println!("Thread {} running!", i);
 | 
				
			||||||
    } };
 | 
					    } };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    par_for! {
 | 
				
			||||||
 | 
					    for i in 1..32, blocksize 16, capturing numbers {
 | 
				
			||||||
 | 
					    let mut lock = numbers.write();
 | 
				
			||||||
 | 
					    lock.push(Student::new(i));
 | 
				
			||||||
 | 
					    println!("Thread {} running!", i);
 | 
				
			||||||
 | 
					    } };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for num in numbers {
 | 
					    for num in numbers {
 | 
				
			||||||
    println!("{:?}", num);
 | 
					    println!("{:?}", num);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										70
									
								
								src/lib.rs
									
										
									
									
									
								
							
							
						
						
									
										70
									
								
								src/lib.rs
									
										
									
									
									
								
							| 
						 | 
					@ -4,6 +4,8 @@ mod sysinfo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
 | 
					use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub use threadpool::{as_static_job, Job, ThreadPoolManager};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Capture<T> {
 | 
					pub struct Capture<T> {
 | 
				
			||||||
    value: Arc<RwLock<T>>,
 | 
					    value: Arc<RwLock<T>>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -38,41 +40,49 @@ impl<T> Capture<T> {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[macro_export]
 | 
					#[macro_export]
 | 
				
			||||||
macro_rules! par_for {
 | 
					macro_rules! __internal_par_for {
 | 
				
			||||||
    (for $name:ident in $iterator:expr, capturing $captured:ident $blk:block) => {
 | 
					    ($name:ident, $iterator:expr, $size:expr, $($captured:ident)*, $blk:block) => {
 | 
				
			||||||
        use rustmp::Capture;
 | 
					        let mut __rmp_tasks = Vec::new();
 | 
				
			||||||
        use rustmp::threadpool::{Job, ThreadPoolManager, as_static_job};
 | 
					        $(let $captured = rustmp::Capture::new($captured);)*
 | 
				
			||||||
        use std::sync::{Arc, RwLock};
 | 
					 | 
				
			||||||
        use std::thread;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let mut tasks = Vec::new();
 | 
					 | 
				
			||||||
        let $captured = Capture::new($captured);
 | 
					 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            let tpm_mtx = ThreadPoolManager::get_instance_guard();
 | 
					            let __rmp_tpm_mtx = rustmp::ThreadPoolManager::get_instance_guard();
 | 
				
			||||||
            let tpm = tpm_mtx.lock().unwrap();
 | 
					            let __rmp_tpm = __rmp_tpm_mtx.lock().unwrap();
 | 
				
			||||||
            let iters = tpm.split_iterators($iterator, 1);
 | 
					            let __rmp_iters = __rmp_tpm.split_iterators($iterator, $size);
 | 
				
			||||||
            for iter in iters {
 | 
					            for iter in __rmp_iters {
 | 
				
			||||||
                let $captured = $captured.clone();
 | 
					                $(let $captured = $captured.clone();)*
 | 
				
			||||||
                tasks.push(as_static_job(move || {
 | 
					                __rmp_tasks.push(rustmp::as_static_job(move || {
 | 
				
			||||||
                    for &$name in &iter
 | 
					                    for &$name in &iter
 | 
				
			||||||
                        $blk
 | 
					                        $blk
 | 
				
			||||||
                }));
 | 
					                }));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            tpm.exec(tasks);
 | 
					            __rmp_tpm.exec(__rmp_tasks);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        $(let $captured = $captured.unwrap();)*
 | 
				
			||||||
        //let itr = $iterator;
 | 
					 | 
				
			||||||
        //let $captured = Capture::new($captured);
 | 
					 | 
				
			||||||
        //let mut handles: Vec<thread::JoinHandle<()>> = vec![];
 | 
					 | 
				
			||||||
        //for $name in itr {
 | 
					 | 
				
			||||||
        //    let $captured = $captured.clone();
 | 
					 | 
				
			||||||
        //    handles.push(thread::spawn(move || $blk));
 | 
					 | 
				
			||||||
        //}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        //for handle in handles {
 | 
					 | 
				
			||||||
        //    handle.join().expect("Thread paniced!");
 | 
					 | 
				
			||||||
        //}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let $captured = $captured.unwrap();
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// "parallel for" wrapper
 | 
				
			||||||
 | 
					///
 | 
				
			||||||
 | 
					/// If the number of arguments increases, convert this to a tail recursive parser instead.
 | 
				
			||||||
 | 
					/// Current implementation save limited (max depth 32) stack space for macro expansion.
 | 
				
			||||||
 | 
					#[macro_export]
 | 
				
			||||||
 | 
					macro_rules! par_for {
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, blocksize $size:expr, capturing $($captured:ident)+, $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, blocksize $size:expr, capturing $($captured:ident)+ $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, capturing $($captured:ident)+, blocksize $size:expr, $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, $size, $($captured)*, $blk);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, blocksize $size:expr, $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, $size,, $blk);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, capturing $($captured:ident)+, $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, 1, $($captured)*, $blk);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    (for $name:ident in $iterator:expr, capturing $($captured:ident)+ $blk:block) => {
 | 
				
			||||||
 | 
					        rustmp::__internal_par_for!($name, $iterator, 1, $($captured)*, $blk);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue