Skip to content

Instantly share code, notes, and snippets.

Created September 29, 2017 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4653dedc6b3e806417983bbd5f236557 to your computer and use it in GitHub Desktop.
Save anonymous/4653dedc6b3e806417983bbd5f236557 to your computer and use it in GitHub Desktop.
Rust code shared from the playground
extern crate rand;
extern crate num_cpus;
use std::io;
use rand::Rng;
use std::sync::{Arc,Mutex};
use std::thread;
fn main() {
//////////////////////////////////////////////////////////////
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 half = a/2;
//////////////////////////////////////////////////////////////
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 mut 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];
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();
let mut fshared = f_clone.lock().unwrap();
fshared[q]=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 f_clone = f.clone();
let mut fshared = f_clone.lock().unwrap();
for q in 0..threads {
let mut wait = fshared[q].join();
}
let z_clone = z.clone();
let mut result = z_clone.lock().unwrap();
for m in 0..a{
for n in 0..d {
println!("resultM [{}] [{}] = {}",m,n,result[m][n]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment