Comments (9)
Something like this ought to do it:
(1..g.width() - 1)
.into_par_iter()
.flat_map(|i| (1..g.height() - 1).into_par_iter().map(|j| (i, j)))
.map(|(i, j)| ... processing ...)
though I'd like to add a cross
combinator, which would make it shorter.
from rayon.
I tried your example and simplified it a bit, but it gives me a lifetime error (I'm still really bad at resolving those). This is my code:
(1..1000usize)
.into_par_iter()
.flat_map(|x| (1..2000usize).into_par_iter().map(|y| (x, y)))
.map(|(x, y)| {
println!("{}, {}", x, y);
});
And this is the error:
src/edges.rs:57:54: 57:64 error: `x` does not live long enough
src/edges.rs:57 .flat_map(|x| (1..2000usize).into_par_iter().map(|y| (x, y)))
^~~~~~~~~~
src/edges.rs:55:5: 57:66 note: reference must be valid for the method call at 55:4...
src/edges.rs:55 (1..1000usize)
src/edges.rs:56 .into_par_iter()
src/edges.rs:57 .flat_map(|x| (1..2000usize).into_par_iter().map(|y| (x, y)))
src/edges.rs:57:19: 57:65 note: ...but borrowed value is only valid for the scope of parameters for function at 57:18
src/edges.rs:57 .flat_map(|x| (1..2000usize).into_par_iter().map(|y| (x, y)))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `imageproc`.
To learn more, run the command again with --verbose.
Any idea how to resolve this?
from rayon.
Try: .map(move |y| (x,y))
(this moves x
into the closure, rather than accessing it by reference)
from rayon.
facepalm . That fixed it, thank you!
from rayon.
Another question just came up:
Assuming I had some more work to do in the outer loop, how would I do this with rayon? Example:
for x in 1..g.width() - 1 {
for y in 1..g.height() - 1 {
...processing....
}
...more_processing...
}
more_processing
uses results produced by processing
.
Thanks for any help in advance!
from rayon.
Depends on what sort of processing you're hoping for. You could transcribe that more literally like:
(1..g.width() - 1).into_par_iter().for_each(|x| {
(1 .. g.height() - 1).into_par_iter().for_each(|y| {
/* processing */
});
/* more processing */
});
But remember those closures have to be Fn
, not FnMut
, so not all types of processing can be made parallel directly. Maybe instead of for_each
, those should be nested .map.reduce
ops.
from rayon.
As @cuviper said, something like:
(1..g.width() - 1).into_par_iter().for_each(|i| {
let mut intermediate_result = vec![];
(1..g.height() - 1).into_par_iter().map(|j| ...).collect_into(&mut intermediate_result);
process(intermediate_result);
});
might be what you are looking for.
from rayon.
Exactly what I was looking for, thanks for your help guys!
from rayon.
Great :)
from rayon.
Related Issues (20)
- Potential `len` method ambiguity in `src/range.rs` HOT 5
- Potential to fix memory ordering for value HOT 2
- a broadcast that only gets executed on idle threads
- `in_place_scope` documentation is confusing/unclear HOT 14
- use_current_thread and the global thread pool HOT 2
- Why does ThreadPool block until a operation is finished? HOT 1
- A way to hook before/after job execution (and/or a way to see a number of tasks in the processing queue)
- Function dependencies with Rayon without blocking the thread / Rayon synchronization primitives?
- Add synchronization guarantees for `ParallelIterator::for_each*` HOT 1
- Par bridge with optional buffering IO handling HOT 2
- Thread pool without work stealing HOT 6
- Question about return types from .map() HOT 4
- Terribly inefficient design and possible solution HOT 5
- [Discussion] Is it possible to impl `IntoIterator` for `ParallelIterator`? HOT 3
- Yield locks HOT 1
- the method `par_iter` exists for struct `ChunkByMut<'_, BinF32, {[email protected]:207:78}>`, but its trait bounds were not satisfied HOT 5
- Unupdated crossbeam dependency makes MIRI angry HOT 1
- help: How to implement memoized chunks for parallel iterator? HOT 2
- wrong repo, issue deleted HOT 3
- Comparison with Spice HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rayon.