Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Post Closed as "Duplicate" by Shepmaster rust
deleted 21 characters in body
Source Link
Shepmaster
  • 439.4k
  • 116
  • 1.3k
  • 1.5k

In my current application I have a vector of bytes (u8) and I need to fill this vector with values that can be computed in parallel:

let vector: Vec<u8> = vec![0, len]; 

Given n_threads threads, each thread can take care of a section of the vector. The only way I know of to pass a mutable vector into multiple threads is to wrap it into a MutexMutex:

let vector: Arc::new(Mutex::new(vec![0, len])); 

and for each thread move the clone of the atomic reference:

for _ in 0..n_hthreads {   let vector = Arc::clone(&vector);   thread::spawn(move || fill_vector()); } 

The problem with this approach is that I would need to lock the entire vector inside the threads (which completely nullifysnullifies the parallelism):

fn fill_vector() {   let vector = vector.lock().unwrap();   vector[i] = /* ... */; } 

A possible alternative would be to have multiple vectors and pass each vector to each thread, and once the threads are joined concatenate the vectors. But, but that would require additional memory unless there is a way to move the sub-vectors into the final vector instead of copying them.

Is there any alternative that would allow me to lock only a specific section of the vector or to move multiple sub-vectors when concatenating (my goal is to have a single vector in the end), or any other alternative to safely write into the vector?

In my current application I have a vector of bytes (u8) and I need to fill this vector with values that can be computed in parallel:

let vector: Vec<u8> = vec![0, len]; 

Given n_threads threads, each thread can take care of a section of the vector. The only way I know of to pass a mutable vector into multiple threads is to wrap it into a Mutex:

let vector: Arc::new(Mutex::new(vec![0, len])); 

and for each thread move the clone of the atomic reference:

for _ in 0..n_hthreads { let vector = Arc::clone(&vector); thread::spawn(move || fill_vector()); } 

The problem with this approach is that I would need to lock the entire vector inside the threads (which completely nullifys the parallelism):

fn fill_vector() { let vector = vector.lock().unwrap(); vector[i] = ...; } 

A possible alternative would be to have multiple vectors and pass each vector to each thread, and once the threads are joined concatenate the vectors. But that would require additional memory unless there is a way to move the sub-vectors into the final vector instead of copying them.

Is there any alternative that would allow me to lock only a specific section of the vector or to move multiple sub-vectors when concatenating (my goal is to have a single vector in the end), or any other alternative to safely write into the vector?

I have a vector of u8 and I need to fill this vector with values that can be computed in parallel:

let vector: Vec<u8> = vec![0, len]; 

Given n_threads threads, each thread can take care of a section of the vector. The only way I know of to pass a mutable vector into multiple threads is to wrap it into a Mutex:

let vector: Arc::new(Mutex::new(vec![0, len])); 

and for each thread move the clone of the atomic reference:

for _ in 0..n_hthreads {   let vector = Arc::clone(&vector);   thread::spawn(move || fill_vector()); } 

The problem with this approach is that I would need to lock the entire vector inside the threads (which completely nullifies the parallelism):

fn fill_vector() {   let vector = vector.lock().unwrap();   vector[i] = /* ... */; } 

A possible alternative would be to have multiple vectors and pass each vector to each thread, and once the threads are joined concatenate the vectors, but that would require additional memory unless there is a way to move the sub-vectors into the final vector instead of copying them.

Is there any alternative that would allow me to lock only a specific section of the vector or to move multiple sub-vectors when concatenating (my goal is to have a single vector in the end), or any other alternative to safely write into the vector?

Source Link
Nick
  • 10.6k
  • 23
  • 107
  • 209

How to modify each section of a vector in multiple threads

In my current application I have a vector of bytes (u8) and I need to fill this vector with values that can be computed in parallel:

let vector: Vec<u8> = vec![0, len]; 

Given n_threads threads, each thread can take care of a section of the vector. The only way I know of to pass a mutable vector into multiple threads is to wrap it into a Mutex:

let vector: Arc::new(Mutex::new(vec![0, len])); 

and for each thread move the clone of the atomic reference:

for _ in 0..n_hthreads { let vector = Arc::clone(&vector); thread::spawn(move || fill_vector()); } 

The problem with this approach is that I would need to lock the entire vector inside the threads (which completely nullifys the parallelism):

fn fill_vector() { let vector = vector.lock().unwrap(); vector[i] = ...; } 

A possible alternative would be to have multiple vectors and pass each vector to each thread, and once the threads are joined concatenate the vectors. But that would require additional memory unless there is a way to move the sub-vectors into the final vector instead of copying them.

Is there any alternative that would allow me to lock only a specific section of the vector or to move multiple sub-vectors when concatenating (my goal is to have a single vector in the end), or any other alternative to safely write into the vector?