mirror of https://github.com/xythrez/RustMP.git
Removed test_simple.rs from master branch, linted all release rust code
This commit is contained in:
parent
80853a1721
commit
4bb34fa294
|
@ -1,8 +1,8 @@
|
||||||
use std::env;
|
|
||||||
use std::cmp::max;
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use std::time::Instant;
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
use std::cmp::max;
|
||||||
|
use std::env;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
||||||
let mut ret = Vec::with_capacity(nsize);
|
let mut ret = Vec::with_capacity(nsize);
|
||||||
|
@ -22,10 +22,15 @@ fn main() {
|
||||||
eprintln!("Usage: {} <msize>", args[0]);
|
eprintln!("Usage: {} <msize>", args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let nsize = max(args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"), 1);
|
let nsize = max(
|
||||||
|
args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"),
|
||||||
|
1,
|
||||||
|
);
|
||||||
let matrix = gen_matrix(nsize);
|
let matrix = gen_matrix(nsize);
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
let _result = (0..nsize).into_par_iter().map(|i| {
|
let _result = (0..nsize)
|
||||||
|
.into_par_iter()
|
||||||
|
.map(|i| {
|
||||||
let mut res_row = Vec::with_capacity(nsize);
|
let mut res_row = Vec::with_capacity(nsize);
|
||||||
for j in 0..nsize {
|
for j in 0..nsize {
|
||||||
let mut sum: f64 = 0.0;
|
let mut sum: f64 = 0.0;
|
||||||
|
@ -35,7 +40,8 @@ fn main() {
|
||||||
res_row.push(sum);
|
res_row.push(sum);
|
||||||
}
|
}
|
||||||
res_row
|
res_row
|
||||||
}).collect::<Vec<Vec<f64>>>();
|
})
|
||||||
|
.collect::<Vec<Vec<f64>>>();
|
||||||
let interval = timer.elapsed();
|
let interval = timer.elapsed();
|
||||||
println!("Elapsed time: {:?}", interval);
|
println!("Elapsed time: {:?}", interval);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use std::env;
|
|
||||||
use std::cmp::max;
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
use std::cmp::max;
|
||||||
|
use std::env;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use rustmp::{par_for, critical};
|
use rustmp::{critical, par_for};
|
||||||
|
|
||||||
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
||||||
let mut ret = Vec::with_capacity(nsize);
|
let mut ret = Vec::with_capacity(nsize);
|
||||||
|
@ -35,7 +35,10 @@ fn main() {
|
||||||
eprintln!("Usage: {} <msize>", args[0]);
|
eprintln!("Usage: {} <msize>", args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let nsize = max(args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"), 1);
|
let nsize = max(
|
||||||
|
args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"),
|
||||||
|
1,
|
||||||
|
);
|
||||||
let matrix = gen_matrix(nsize);
|
let matrix = gen_matrix(nsize);
|
||||||
let result = gen_empty(nsize);
|
let result = gen_empty(nsize);
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::env;
|
|
||||||
use std::cmp::max;
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
use std::cmp::max;
|
||||||
|
use std::env;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use rustmp::par_for;
|
use rustmp::par_for;
|
||||||
|
@ -35,7 +35,10 @@ fn main() {
|
||||||
eprintln!("Usage: {} <msize>", args[0]);
|
eprintln!("Usage: {} <msize>", args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let nsize = max(args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"), 1);
|
let nsize = max(
|
||||||
|
args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"),
|
||||||
|
1,
|
||||||
|
);
|
||||||
let matrix = gen_matrix(nsize);
|
let matrix = gen_matrix(nsize);
|
||||||
let mut result = gen_empty(nsize);
|
let mut result = gen_empty(nsize);
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::env;
|
|
||||||
use std::cmp::max;
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
use std::cmp::max;
|
||||||
|
use std::env;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
fn gen_matrix(nsize: usize) -> Vec<Vec<f64>> {
|
||||||
|
@ -33,7 +33,10 @@ fn main() {
|
||||||
eprintln!("Usage: {} <msize>", args[0]);
|
eprintln!("Usage: {} <msize>", args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let nsize = max(args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"), 1);
|
let nsize = max(
|
||||||
|
args[1].parse::<usize>().expect("Usage: matrix_mul <msize>"),
|
||||||
|
1,
|
||||||
|
);
|
||||||
let matrix = gen_matrix(nsize);
|
let matrix = gen_matrix(nsize);
|
||||||
let mut result = gen_empty(nsize);
|
let mut result = gen_empty(nsize);
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
|
|
@ -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<Student> = 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);
|
|
||||||
// } }
|
|
||||||
|
|
||||||
}
|
|
|
@ -2,7 +2,7 @@ pub mod threadpool;
|
||||||
|
|
||||||
mod sysinfo;
|
mod sysinfo;
|
||||||
|
|
||||||
use std::ops::{DerefMut, Deref};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||||
|
|
||||||
pub use threadpool::{as_static_job, Job, ThreadPoolManager};
|
pub use threadpool::{as_static_job, Job, ThreadPoolManager};
|
||||||
|
@ -56,7 +56,7 @@ impl<T> Copy for UnsafePtr<T> {}
|
||||||
|
|
||||||
impl<T> Clone for UnsafePtr<T> {
|
impl<T> Clone for UnsafePtr<T> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
UnsafePtr { value: self.value }
|
UnsafePtr { value: self.value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,14 +70,13 @@ impl<T> Deref for UnsafePtr<T> {
|
||||||
|
|
||||||
impl<T> DerefMut for UnsafePtr<T> {
|
impl<T> DerefMut for UnsafePtr<T> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
unsafe { self.value.as_mut().unwrap() }
|
unsafe { self.value.as_mut().unwrap() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T> Send for UnsafePtr<T> {}
|
unsafe impl<T> Send for UnsafePtr<T> {}
|
||||||
unsafe impl<T> Sync for UnsafePtr<T> {}
|
unsafe impl<T> Sync for UnsafePtr<T> {}
|
||||||
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! critical {
|
macro_rules! critical {
|
||||||
(read $($r:ident)+; readwrite $($w:ident)+; $($ops:tt)+) => {
|
(read $($r:ident)+; readwrite $($w:ident)+; $($ops:tt)+) => {
|
||||||
|
|
Loading…
Reference in New Issue