Skip to content

Commit

Permalink
make a version of nbody that uses as_parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Mar 27, 2018
1 parent 3b1fa38 commit 1758509
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions rayon-demo/src/nbody/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn nbody_par(b: &mut ::test::Bencher) {
nbody_bench(b, |n| { n.tick_par(); });
}

#[bench]
fn nbody_par_as_parallel(b: &mut ::test::Bencher) {
nbody_bench(b, |n| { n.tick_par_as_parallel(); });
}

#[bench]
fn nbody_parreduce(b: &mut ::test::Bencher) {
nbody_bench(b, |n| { n.tick_par_reduce(); });
Expand Down
5 changes: 4 additions & 1 deletion rayon-demo/src/nbody/nbody.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use cgmath::{InnerSpace, Point3, Vector3, Zero};
use rand::{Rand, Rng};
use rayon::prelude::*;
#[cfg(test)]
use rayon::iter::AsParallel;
use std::f64::consts::PI;

const INITIAL_VELOCITY: f64 = 8.0; // set to 0.0 to turn off.
Expand Down Expand Up @@ -111,6 +113,7 @@ impl NBodyBenchmark {
out_bodies
}

#[cfg(test)]
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)
Expand All @@ -121,8 +124,8 @@ impl NBodyBenchmark {
let time = self.time;
out_bodies
.iter_mut()
.as_parallel()
.zip(&in_bodies[..])
.as_parallel()
.for_each(|(out, prev)| {
let (vel, vel2) = next_velocity(time, prev, in_bodies);
out.velocity = vel;
Expand Down

0 comments on commit 1758509

Please sign in to comment.