How to use an ArcArray across threads? #1046
-
| 
         I have a large matrix as an  use ndarray::{Array2, ArcArray2};
use std::thread;
fn process_matrix(mat: Array2<f64>){
    let matrix: ArcArray2<f64> = data.into_shared();
    for _ in 0..10 {
        matrix = matrix.clone();
        let handle = thread::spawn(move || {
            matrix.sum();
            // Placeholder for processing
        });
    }
}I just want to check. Is this the way that   | 
  
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
| 
         Yes, that's correct. The data of an  
 Somewhat related -- If you're not already familiar with it, you may be interested in the  use ndarray::Array2;
use rayon::prelude::*;
fn process_matrix2(mat: Array2<f64>) {
    (0..10).into_par_iter().for_each(|_| {
        mat.sum();
        // Placeholder for processing
    });
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Very helpful answer, thank you! Do you mind if I (or you) copy this into a PR that describes the ArcArray? It's exactly what I would have wanted as a new user. Regarding your   | 
  
Beta Was this translation helpful? Give feedback.
Yes, that's correct. The data of an
ArcArrayis wrapped in anArc. It's similar toArc<Array<A, D>>, except that:The shape/strides/pointer are uniquely owned by the
ArcArray, not wrapped in theArc. Only the data is wrapped in theArc. As a result, for example, calling.slice_collapse()on anArcArraymodifies its shape/strides/pointer in-place without cloning the data. In contrast, if you had anArc<Array<A, D>>and wanted to call.slice_collapse(), you'd have to call.make_mut()(which would clone the wrapped array, including its data) to get a mutable reference in order to modify the shape/strides/pointer.When modifying the data in the
ArcArray(or getting a mutable reference to …