Created
September 29, 2017 16:40
-
-
Save anonymous/10fab8fe9588e7fe472c9cf9b69f0ea4 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
extern crate num_cpus; | |
use std::io; | |
use rand::Rng; | |
use std::sync::{Arc,Mutex}; | |
use std::{thread,time}; | |
fn main() { | |
let five_ms=time::Duration::from_millis(5); | |
////////////////////////////////////////////////////////////// | |
let mut a = String::new(); | |
let mut b = String::new(); | |
let mut c = String::new(); | |
let mut d = String::new(); | |
////////////////////////////////////////////////////////////// | |
println!("Enter dimensions of the 1 matrix: rows&columns"); | |
io::stdin().read_line(&mut a) | |
.expect("Failed"); | |
io::stdin().read_line(&mut b) | |
.expect("Failed"); | |
println!("Enter dimensions of the 2 matrix: rows&columns"); | |
io::stdin().read_line(&mut c) | |
.expect("Failed"); | |
io::stdin().read_line(&mut d) | |
.expect("Failed"); | |
////////////////////////////////////////////////////////////// | |
let a: usize = a.trim().parse() | |
.expect("Number expected"); | |
let b: usize = b.trim().parse() | |
.expect("Number expected"); | |
let c: usize = c.trim().parse() | |
.expect("Number expected"); | |
let d: usize = d.trim().parse() | |
.expect("Number expected"); | |
if b!=c { | |
println!("Wrong input!"); | |
std::process::exit(-1); | |
} | |
////////////////////////////////////////////////////////////// | |
let mut firstM = vec![vec![0i32; b]; a]; | |
let mut secondM = vec![vec![0i32; d]; c]; | |
let mut resultM = vec![vec![0i32; d]; a]; | |
////////////////////////////////////////////////////////////// | |
for i in 0..a { | |
for j in 0..b { | |
firstM [i][j]= rand::thread_rng().gen_range(1, 10); | |
//println!("firstM [{}] [{}] = {}",i,j,firstM[i][j]); | |
} | |
} | |
for m in 0..c { | |
for n in 0..d { | |
secondM [m][n] = rand::thread_rng().gen_range(1, 10); | |
//println!("secondM [{}] [{}] = {}",m,n,secondM[m][n]); | |
} | |
} | |
for m in 0..a { | |
for n in 0..d { | |
resultM [m][n] = 0; | |
} | |
} | |
////////////////////////////////////////////////////////////// | |
let x = Arc::new(firstM); | |
let y = Arc::new(secondM); | |
let z = Arc::new(Mutex::new(resultM)); | |
let threads = num_cpus::get(); | |
let mut boundaries = vec![0;2*threads]; | |
let range = a/threads; | |
let mut j = 1; | |
for i in 1..2*threads { | |
if (i%2)!=0 { | |
boundaries[i]=j*range; | |
j+=1; | |
} else {boundaries[i]=boundaries[i-1];} | |
if i==(2*threads-1) { | |
boundaries[0]=0; | |
boundaries[i]=a; | |
} | |
} | |
let w = Arc::new(boundaries); | |
///////////////////////////////////////////////////////////// | |
let mut flags = vec![0;threads]; | |
for i in 0..threads { | |
flags[i]=1; | |
} | |
let f = Arc::new(Mutex::new(flags)); | |
for q in 0..threads { | |
let x_clone = x.clone(); | |
let y_clone = y.clone(); | |
let z_clone = z.clone(); | |
let w_clone = w.clone(); | |
let f_clone = f.clone(); | |
thread::spawn(move ||{ | |
let mut shared = z_clone.lock().unwrap(); | |
for z in (w_clone[2*q])..(w_clone[(2*q)+1]) { | |
for i in 0..d{ | |
for j in 0..b{ | |
shared[z][i]+=(x_clone[z][j])*(y_clone[j][i]); | |
}}} | |
let mut fshared = f_clone.lock().unwrap(); | |
fshared[q]=0; | |
}); | |
} | |
/////////////////////////////////////////////////////////////// | |
for q in 0..threads { | |
loop { | |
thread::sleep(five_ms); | |
let f_clone = f.clone(); | |
let fshared = f_clone.lock().unwrap(); | |
if fshared[q]==0 {break;} | |
} | |
} | |
let z_clone = z.clone(); | |
let result = z_clone.lock().unwrap(); | |
for m in 0..a{ | |
for n in 0..d { | |
println!("resultM [{}] [{}] = {}",m+1,n+1,result[m][n]); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment