Skip to content

Commit

Permalink
use as_parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Mar 27, 2018
1 parent 3e2facc commit 3b1fa38
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rayon-demo/src/nbody/nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ impl NBodyBenchmark {
out_bodies
}

pub fn tick_par_as_parallel(&mut self) -> &[Body] {
let (in_bodies, out_bodies) = if (self.time & 1) == 0 {
(&self.bodies.0, &mut self.bodies.1)
} else {
(&self.bodies.1, &mut self.bodies.0)
};

let time = self.time;
out_bodies
.iter_mut()
.as_parallel()
.zip(&in_bodies[..])
.for_each(|(out, prev)| {
let (vel, vel2) = next_velocity(time, prev, in_bodies);
out.velocity = vel;
out.velocity2 = vel2;

let next_velocity = vel - vel2;
out.position = prev.position + next_velocity;
});

self.time += 1;

out_bodies
}

pub fn tick_par_reduce(&mut self) -> &[Body] {
let (in_bodies, out_bodies) = if (self.time & 1) == 0 {
(&self.bodies.0, &mut self.bodies.1)
Expand Down

0 comments on commit 3b1fa38

Please sign in to comment.