Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: is it possible to modify different DMatrix columns in parallel (via Rayon) without Mutex? #848

Open
pgagarinov opened this issue Mar 10, 2021 · 4 comments
Labels
enhancement good first issue Good first issue for newcomers. P-medium Medium priority

Comments

@pgagarinov
Copy link

pgagarinov commented Mar 10, 2021

I'm trying to make the following code work:

use nalgebra::*;
use rayon::prelude::*;
let mut m = DMatrix::<f64>::from_vec(2, 3, (0..6).map(|n| n as f64).collect());
dbg!(&m);
m.column_iter_mut()
    .par_bridge()
     .for_each(|mut c| c[0] = 111.0);
dbg!(&m);

But the code doesn't compile:

no method named par_bridge found for struct ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>> in the current scope
the method par_bridge exists but the following trait bounds were not satisfied:
ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: Send
which is required by ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: rayon::iter::ParallelBridge
&ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: Send
which is required by &ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: rayon::iter::ParallelBridge
&ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: Iterator
which is required by &ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: rayon::iter::ParallelBridge
&mut ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: Send
which is required by &mut ColumnIterMut<'_, f64, Dynamic, Dynamic, VecStorage<f64, Dynamic, Dynamic>>: rayon::iter::ParallelBridge

Is there a relatively short path for making the code above work (with some changes to either the code or nalgebra source code)? I can do some digging on my own, I just need the direction. Thanks!

@sebcrozet
Copy link
Member

sebcrozet commented Mar 13, 2021

Hi! We never attempted to make this kind of parallelisation possible with rayon so far. So I don’t have much advice to give. I don’t believe &ColumnIterMut can be modified to make it implement Send. So I feel like we would have to implement custom parallel iterators for nalgebra matrices.
One "simple" workaround I can think of is to convert the matrix to a slice and iterate on chunks:

matrix
    .as_mut_slice()
    .par_chunks(matrix.nrows())
    .for_each(|col| col[0] = 111.0)

This works because the matrix is stored in column-major order.
Though is is much less ergonomic than what a custom parallel iterator would offer.

@sebcrozet sebcrozet added enhancement good first issue Good first issue for newcomers. P-medium Medium priority labels Mar 13, 2021
@SallySoul
Copy link

I don't mean to commit myself to anything right now, but would y'all be amenable to a rayon feature for nalgebra that added support for parallel iterators over rows, columns, coefficients, ect? What would scoping out work like that entail?

@geo-ant
Copy link
Contributor

geo-ant commented Oct 21, 2022

I am also very keen on having this feature and created a PR for this in #1165

@geo-ant
Copy link
Contributor

geo-ant commented Sep 14, 2023

the aforementioned PR is merged and I believe this issue can be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue Good first issue for newcomers. P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

4 participants