From 4bb34fa294d399c37b07d9cc6887beee7121072a Mon Sep 17 00:00:00 2001 From: Yiyao Yu Date: Tue, 4 May 2021 02:32:58 -0400 Subject: [PATCH] Removed test_simple.rs from master branch, linted all release rust code --- src/bin/rayon_matmul.rs | 34 +++++++++------- src/bin/rmp_matmul.rs | 11 ++++-- src/bin/rmpunsafe_matmul.rs | 9 +++-- src/bin/seqrs_matmul.rs | 9 +++-- src/bin/test_simple.rs | 78 ------------------------------------- src/lib.rs | 7 ++-- 6 files changed, 42 insertions(+), 106 deletions(-) delete mode 100644 src/bin/test_simple.rs diff --git a/src/bin/rayon_matmul.rs b/src/bin/rayon_matmul.rs index 7968e8d..49a4c43 100644 --- a/src/bin/rayon_matmul.rs +++ b/src/bin/rayon_matmul.rs @@ -1,8 +1,8 @@ -use std::env; -use std::cmp::max; use rand::random; -use std::time::Instant; use rayon::prelude::*; +use std::cmp::max; +use std::env; +use std::time::Instant; fn gen_matrix(nsize: usize) -> Vec> { let mut ret = Vec::with_capacity(nsize); @@ -22,20 +22,26 @@ fn main() { eprintln!("Usage: {} ", args[0]); return; } - let nsize = max(args[1].parse::().expect("Usage: matrix_mul "), 1); + let nsize = max( + args[1].parse::().expect("Usage: matrix_mul "), + 1, + ); let matrix = gen_matrix(nsize); let timer = Instant::now(); - let _result = (0..nsize).into_par_iter().map(|i| { - let mut res_row = Vec::with_capacity(nsize); - for j in 0..nsize { - let mut sum: f64 = 0.0; - for k in 0..nsize { - sum += matrix[i][k] * matrix[k][j]; + let _result = (0..nsize) + .into_par_iter() + .map(|i| { + let mut res_row = Vec::with_capacity(nsize); + for j in 0..nsize { + let mut sum: f64 = 0.0; + for k in 0..nsize { + sum += matrix[i][k] * matrix[k][j]; + } + res_row.push(sum); } - res_row.push(sum); - } - res_row - }).collect::>>(); + res_row + }) + .collect::>>(); let interval = timer.elapsed(); println!("Elapsed time: {:?}", interval); } diff --git a/src/bin/rmp_matmul.rs b/src/bin/rmp_matmul.rs index 077190f..e03c032 100644 --- a/src/bin/rmp_matmul.rs +++ b/src/bin/rmp_matmul.rs @@ -1,9 +1,9 @@ -use std::env; -use std::cmp::max; use rand::random; +use std::cmp::max; +use std::env; use std::time::Instant; -use rustmp::{par_for, critical}; +use rustmp::{critical, par_for}; fn gen_matrix(nsize: usize) -> Vec> { let mut ret = Vec::with_capacity(nsize); @@ -35,7 +35,10 @@ fn main() { eprintln!("Usage: {} ", args[0]); return; } - let nsize = max(args[1].parse::().expect("Usage: matrix_mul "), 1); + let nsize = max( + args[1].parse::().expect("Usage: matrix_mul "), + 1, + ); let matrix = gen_matrix(nsize); let result = gen_empty(nsize); let timer = Instant::now(); diff --git a/src/bin/rmpunsafe_matmul.rs b/src/bin/rmpunsafe_matmul.rs index b6ee153..bcd4c6a 100644 --- a/src/bin/rmpunsafe_matmul.rs +++ b/src/bin/rmpunsafe_matmul.rs @@ -1,6 +1,6 @@ -use std::env; -use std::cmp::max; use rand::random; +use std::cmp::max; +use std::env; use std::time::Instant; use rustmp::par_for; @@ -35,7 +35,10 @@ fn main() { eprintln!("Usage: {} ", args[0]); return; } - let nsize = max(args[1].parse::().expect("Usage: matrix_mul "), 1); + let nsize = max( + args[1].parse::().expect("Usage: matrix_mul "), + 1, + ); let matrix = gen_matrix(nsize); let mut result = gen_empty(nsize); let timer = Instant::now(); diff --git a/src/bin/seqrs_matmul.rs b/src/bin/seqrs_matmul.rs index 4c6f975..a800933 100644 --- a/src/bin/seqrs_matmul.rs +++ b/src/bin/seqrs_matmul.rs @@ -1,6 +1,6 @@ -use std::env; -use std::cmp::max; use rand::random; +use std::cmp::max; +use std::env; use std::time::Instant; fn gen_matrix(nsize: usize) -> Vec> { @@ -33,7 +33,10 @@ fn main() { eprintln!("Usage: {} ", args[0]); return; } - let nsize = max(args[1].parse::().expect("Usage: matrix_mul "), 1); + let nsize = max( + args[1].parse::().expect("Usage: matrix_mul "), + 1, + ); let matrix = gen_matrix(nsize); let mut result = gen_empty(nsize); let timer = Instant::now(); diff --git a/src/bin/test_simple.rs b/src/bin/test_simple.rs deleted file mode 100644 index 6088c42..0000000 --- a/src/bin/test_simple.rs +++ /dev/null @@ -1,78 +0,0 @@ -// use rand::Rng; -use rustmp::{par_for, critical}; -// use std::time; - -#[derive(Debug)] -struct Student { - name: String, - age: u8, - gpa: f32, -} - -impl Student { - pub fn new(age: u8) -> Student { - Student { - name: "Default".to_string(), - age, - gpa: age as f32, - } - } -} - -fn min(x: i32, y: &i32) -> i32 { - if x < *y { - x - } else { - *y - } -} - -fn main() { - let numbers: Vec = vec![]; - - par_for! { - for i in 1..32, blocksize 4, shared_mut numbers, { - //std::thread::sleep( - // time::Duration::from_secs( - // rand::thread_rng().gen_range(1..10))); - critical! { - // Automatically locks numbers as read+write, - // and makes the result accessible as number - readwrite numbers; - numbers.push(Student::new(i)); - } - println!("Thread {} running!", i); - } - } - - for num in numbers { - println!("{:?}", num); - } - - let a = vec![vec![1,2,3],vec![4,5,6],vec![7,8,9]]; - let b = vec![vec![3,2,1],vec![6,5,4],vec![9,8,7]]; - let n = a.len(); - let mut c = vec![vec![0;n];n]; - for i in 0..n { - for j in 0..n { - let mut x = 0; - par_for! { - for k in 0..n, shared a b, reduction x#+, { - x += (a[i][k]*b[k][j]); - } - } - c[i][j] = x; - } - } - println!("{:?}", c); - // let mut local = 0; - // par_for! { - // for i in 1..32, blocksize 1, shared_mut 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 5660c0d..97968de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ pub mod threadpool; mod sysinfo; -use std::ops::{DerefMut, Deref}; +use std::ops::{Deref, DerefMut}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; pub use threadpool::{as_static_job, Job, ThreadPoolManager}; @@ -56,7 +56,7 @@ impl Copy for UnsafePtr {} impl Clone for UnsafePtr { fn clone(&self) -> Self { -UnsafePtr { value: self.value } + UnsafePtr { value: self.value } } } @@ -70,14 +70,13 @@ impl Deref for UnsafePtr { impl DerefMut for UnsafePtr { fn deref_mut(&mut self) -> &mut Self::Target { -unsafe { self.value.as_mut().unwrap() } + unsafe { self.value.as_mut().unwrap() } } } unsafe impl Send for UnsafePtr {} unsafe impl Sync for UnsafePtr {} - #[macro_export] macro_rules! critical { (read $($r:ident)+; readwrite $($w:ident)+; $($ops:tt)+) => {