ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

`par_map_axis`?

Open justinlovinger opened this issue 3 years ago • 2 comments
trafficstars

The documentation recommends collecting into a Vec when performing parallel axis operations, https://docs.rs/ndarray/0.15.4/ndarray/parallel/index.html#axis-iterators. This is far from ideal when further ndarray operations are required. It would be helpful if axis could be mapped in parallel like how map_axis maps sequentially.

justinlovinger avatar Jul 04 '22 22:07 justinlovinger

I tried using

let arr: Array1<_> = a.axis_iter(Axis(0)).into_par_iter().map(|row| row.sum()).collect();

instead but I got the following error

the trait bound ArrayBase<OwnedRepr<_>, ndarray::Dim<[usize; 1]>>: rayon::iter::FromParallelIterator<{float}> is not satisfied

Replace Array1<_> with Vec<_> and it works. This is somewhat surprising, as one would think that it should work with Array1! Maybe one of the maintainer know why?

In the meantime, you can create a ndarray with Array1::from(v) or ArrayView1::from(v)

nilgoyette avatar Jul 20 '22 18:07 nilgoyette

In the meantime, you can create a ndarray with Array1::from(v) or ArrayView1::from(v)

That only works with static dimensionality, not D::Smaller as in map_axis.

justinlovinger avatar Jul 21 '22 01:07 justinlovinger