Coder Social home page Coder Social logo

Comments (9)

nikomatsakis avatar nikomatsakis commented on July 18, 2024

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.

nicokoch avatar nicokoch commented on July 18, 2024

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.

cuviper avatar cuviper commented on July 18, 2024

Try: .map(move |y| (x,y))
(this moves x into the closure, rather than accessing it by reference)

from rayon.

nicokoch avatar nicokoch commented on July 18, 2024

facepalm . That fixed it, thank you!

from rayon.

nicokoch avatar nicokoch commented on July 18, 2024

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.

cuviper avatar cuviper commented on July 18, 2024

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.

nikomatsakis avatar nikomatsakis commented on July 18, 2024

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.

nicokoch avatar nicokoch commented on July 18, 2024

Exactly what I was looking for, thanks for your help guys!

from rayon.

nikomatsakis avatar nikomatsakis commented on July 18, 2024

Great :)

from rayon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.