Parallel calculation of array elements in rust

I am new to Rust (v1.0.0) and thread programming. I am trying to compute the elements of a b-array using a-array. Each element of the b-array can be calculated independently of the others (in parallel).

extern crate rand;
use rand::Rng;
use std::io;
use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
  let mut a : [u32; 10] = [0; 10];
  let mut b = Arc::new(Mutex::new([0; 10]));
  let mut rng = rand::thread_rng();

  for x in 0..9 {
    a[x] = (rng.gen::<u32>()  % 1000) + 1;
  };

  for x in 0..4 { 
    let b = b.clone(); 
    thread::spawn(move || { let mut b = b.lock().unwrap();
      for y in 0..4 {
        b[x]   += a[y] * a[y*2+1];
        b[x+5] += a[y+1] * a[y*2];
      }
    });
  };

  thread::sleep_ms(1000); 

  for x in 0..a.len() {
    println!("a({0})={1}, b({0})={2}", x, a[x], b[x]);
  };
}

Can you help me:

  • if I use: let mut b = Arc::new(Mutex::new([u32; 10] = [0; 10]));→ I get an error unresolved name 'u32'. Did you mean 'a'?How to set the type of an array element?
  • thread :: sleep_ms (1000) - This is so rude. How can I verify that all threads are complete?
  • How can I return my calculated b [i] and / or collect thread-computed b-arrays in the latter? Now I have an error:cannot index a value of type 'alloc::arc::Arc<std::sync::mutex::Mutex<[u32; 10]>>'
  • Is it possible to use only one b-array in memory and send to the stream (using pointers) to calculate two elements of the b-array?

Thanks for the solution. Working code (I changed it for display problem):

extern crate rand;
use rand::Rng;
use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
  let mut a : [u32; 10000] = [0; 10000];
  let b = Arc::new(Mutex::new([0u32; 10]));
  let mut rng = rand::thread_rng();

  for x in 0..10000 {
    a[x] = (rng.gen::<u32>() % 10) + 1;
  };

  for x in 0..5 {
    let b = b.clone();
    thread::spawn(move || { let mut b = b.lock().unwrap();
      println!("thread {} started", x);
      for y in 0..5000 {
        b[x]   += a[y] * a[y*2+1];
        b[x+5] += a[y+1] * a[y*2];
      };
      b[x] += a[x];
      b[x+5] -= a[x];
    println!("thread {} finished", x);
    });
  };

  thread::sleep_ms(1000);

  for x in 0..10 {
    println!("b({0})={1}", x, b.lock().unwrap()[x]);
  };
}

Output:

thread 1 started
thread 1 finished
thread 3 started
thread 3 finished
thread 0 started
thread 0 finished
thread 2 started
thread 2 finished
thread 4 started
thread 4 finished
b(0)=149482
...
b(9)=149065

.

-2
1

, clone() Arc "" , Arc.

, Rust. b , .

, Rust.

- , .

( ) () :

use std::thread;
use std::sync::Arc;

fn main() {
    let input = Arc::new([1u32, 2, 3, 4]);
    let output = Arc::new([0; 4]);

    let mut handles = Vec::new();

    for t in 0..4 {
        let inp = input.clone();
        let out = output.clone();
        let handle = thread::spawn(move || unsafe {
            let p = (out.as_ptr() as *mut u32).offset(t as isize);

            *p = inp[t] + (t as u32 + 1);
        });

        handles.push(handle);
    }


    for h in handles {
        h.join().unwrap();
    }

    println!("{:?}", output);
}

Arc . (out.as_ptr() as *mut u32), offset.

0

All Articles