-
Hey, I haven't figured out if I can split arrays into multiple mutable chunks somehow. Example of what I would like to achieve: // All the data in same index is related to each other.
// E.g., data1[0], data2[0] and data3.row(0) should all be handled at the same time.
let mut data1 = Array1::zeros(100000);
let mut data2 = Array1::zeros(100000);
let mut data3 = Array2::zeros((100000, 2));
// Split all the data to X chunks (X is known only at runtime).
// All the chunks are guaranteed not to be overlapping, so no need to worry about race conditions.
thread::scope(|scope| {
for (start_index, end_index) in get_chunks() {
let mut data1 = data1.slice_mut(s![start_index..end_index]);
let mut data2 = data2.slice_mut(s![start_index..end_index]);
let mut data3 = data3.slice_mut(s![start_index..end_index, ..]);
scope.spawn(move || {
// Do something with the data
});
}
});
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Will you please clarify why
Will you please clarify what you mean by this? When manually creating threads, array views produced by use ndarray::prelude::*;
use rayon::prelude::*;
fn main() {
let mut data1 = Array1::<i32>::zeros(100000);
let mut data2 = Array1::<i32>::zeros(100000);
let mut data3 = Array2::<i32>::zeros((100000, 2));
let chunk_size = 1000;
data1
.axis_chunks_iter_mut(Axis(0), chunk_size)
.into_par_iter()
.zip_eq(data2.axis_chunks_iter_mut(Axis(0), chunk_size))
.zip_eq(data3.axis_chunks_iter_mut(Axis(0), chunk_size))
.for_each(
|((d1, d2), mut d3): (
(ArrayViewMut1<'_, i32>, ArrayViewMut1<'_, i32>),
ArrayViewMut2<'_, i32>,
)| {
d3.column_mut(0).assign(&d1);
d3.column_mut(1).assign(&d2);
},
);
}
Will you please clarify what you mean by this? You can zip together iterators regardless of the types of the iterators' items. If you're referring to iteration over multiple axes, you can use broadcasting with |
Beta Was this translation helpful? Give feedback.
-
I didn't realize you need to first import rayon for parallel iterator zipping to work, my bad.
Ah yeah sorry, I actually meant to write that you can't zip together immutable and mutable data, rather than different dimensionalities. But anyway, I got this to work with Thank you for the quick help and awesome job with the library! Here's the final solution in case anyone else in the future needs it: let data1 = Array1::zeros(100000);
let mut data2 = Array1::zeros(100000);
let mut data3 = Array2::zeros((100000, 2));
let n_chunks = 20;
let chunk_size = 100000 / n_chunks;
let mut data1_iter = data1.axis_chunks_iter(Axis(0), chunk_size);
let mut data2_iter = data2.axis_chunks_iter_mut(Axis(0), chunk_size);
let mut data3_iter = data3.axis_chunks_iter_mut(Axis(0), chunk_size);
thread::scope(|scope| {
for _ in 0..n_chunks {
let d1 = data1_iter.next().unwrap();
let mut d2 = data1_iter.next().unwrap();
let mut d3 = data1_iter.next().unwrap();
scope.spawn(move || {
// Do something with the data
});
}
} |
Beta Was this translation helpful? Give feedback.
I didn't realize you need to first import rayon for parallel iterator zipping to work, my bad.
Ah yeah sorry, I actually meant to write that you can't zip together immutable and mutable data, rather than different dimensionalities.
But anyway, I got this to work with
axis_chunks_iter
andaxis_chunks_iter_mut
(data1
was supposed to be immutable in my example) and manually consuming the iterators. I guess I was a bit sleep deprived when I posted the topic, the answer was right there in front of me 😞Thank you for the quick help and awe…