Skip to content

Commit

Permalink
Merge #695
Browse files Browse the repository at this point in the history
695: a few tweaks to the demos r=cuviper a=nikomatsakis

A few changes that I am making on my branch that I would like applied to master to. 

This renames some benchmarks to make them uniquely identifiable and also adds a test to measure #642. 

Co-authored-by: Niko Matsakis <niko@alum.mit.edu>
  • Loading branch information
bors[bot] and nikomatsakis committed Sep 23, 2019
2 parents 68edcf6 + d98d420 commit 572d1e0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 34 deletions.
55 changes: 55 additions & 0 deletions rayon-demo/src/cpu_time/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use time::{self, Duration};

#[cfg(windows)]
mod win;
#[cfg(windows)]
pub use self::win::get_cpu_time;

#[cfg(unix)]
mod unix;
#[cfg(unix)]
pub use self::unix::get_cpu_time;

#[cfg(not(any(unix, windows)))]
pub fn get_cpu_time() -> Option<u64> {
None
}

pub fn get_cpu_duration(start: Option<u64>, stop: Option<u64>) -> Option<Duration> {
start.and_then(|start| stop.and_then(|stop| Some(Duration::nanoseconds((stop - start) as i64))))
}

#[derive(Copy, Clone)]
pub struct CpuMeasure {
/// number of ns
pub time_duration: u64,

/// percentage (0-100) of that as cpu time
pub cpu_usage_percent: Option<f64>,
}

pub fn measure_cpu(op: impl FnOnce()) -> CpuMeasure {
let time_start = time::precise_time_ns();
let cpu_start = get_cpu_time();

op();

let cpu_stop = get_cpu_time();
let time_duration = time::precise_time_ns() - time_start;

CpuMeasure {
time_duration,
cpu_usage_percent: get_cpu_duration(cpu_start, cpu_stop)
.and_then(|cpu| cpu.num_nanoseconds())
.and_then(|cpu| Some(100.0 * cpu as f64 / time_duration as f64)),
}
}

pub fn print_time(m: CpuMeasure) {
println!(" wallclock: {} ns", m.time_duration);
if let Some(cpu_usage) = m.cpu_usage_percent {
println!(" cpu usage: {:3.1}%", cpu_usage);
} else {
println!(" cpu usage: N/A");
}
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions rayon-demo/src/life/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ fn generations(b: &mut ::test::Bencher) {
}

#[bench]
fn parallel_generations(b: &mut ::test::Bencher) {
fn par_iter_generations(b: &mut ::test::Bencher) {
b.iter(|| super::parallel_generations(Board::new(200, 200).random(), 100));
}

#[bench]
fn as_parallel_generations(b: &mut ::test::Bencher) {
fn par_bridge_generations(b: &mut ::test::Bencher) {
b.iter(|| super::par_bridge_generations(Board::new(200, 200).random(), 100));
}
20 changes: 0 additions & 20 deletions rayon-demo/src/life/cpu_time/mod.rs

This file was deleted.

17 changes: 6 additions & 11 deletions rayon-demo/src/life/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Options:
-h, --help Show this message.
";

use cpu_time::{self, CpuMeasure};
use rand::distributions::Standard;
use rand::{thread_rng, Rng};
use std::iter::repeat;
Expand All @@ -28,7 +29,6 @@ use rayon::prelude::*;

#[cfg(test)]
mod bench;
mod cpu_time;

#[derive(Deserialize)]
pub struct Args {
Expand Down Expand Up @@ -249,19 +249,14 @@ fn measure_cpu(f: fn(Board, usize, u64) -> (), args: &Args) -> CpuResult {
let (n, gens, rate) = (args.flag_size, args.flag_gens, args.flag_fps);
let interval = 1_000_000_000 / rate as u64;
let brd = Board::new(n, n).random();
let start = time::precise_time_ns();
let cpu_start = cpu_time::get_cpu_time();

f(brd, gens, interval);

let cpu_stop = cpu_time::get_cpu_time();
let duration = time::precise_time_ns() - start;
let CpuMeasure { time_duration, cpu_usage_percent } = cpu_time::measure_cpu(|| {
f(brd, gens, interval)
});

CpuResult {
actual_fps: (1_000_000_000.0 * gens as f64) / duration as f64,
cpu_usage_percent: cpu_time::get_cpu_duration(cpu_start, cpu_stop)
.and_then(|cpu| cpu.num_nanoseconds())
.and_then(|cpu| Some(100.0 * cpu as f64 / duration as f64)),
actual_fps: (1_000_000_000.0 * gens as f64) / time_duration as f64,
cpu_usage_percent,
}
}

Expand Down
4 changes: 4 additions & 0 deletions rayon-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::io;
use std::io::prelude::*;
use std::process::exit;

mod cpu_time;
mod life;
mod matmul;
mod mergesort;
mod nbody;
mod noop;
mod quicksort;
mod sieve;
mod tsp;
Expand Down Expand Up @@ -81,6 +83,7 @@ Benchmarks:
- sieve: Finding primes using a Sieve of Eratosthenes.
- matmul: Parallel matrix multiplication.
- mergesort: Parallel mergesort.
- noop: Launch empty tasks to measure CPU usage.
- quicksort: Parallel quicksort.
- tsp: Traveling salesman problem solver (sample data sets in `data/tsp`).
";
Expand All @@ -106,6 +109,7 @@ fn main() {
"sieve" => sieve::main(&args[1..]),
"tsp" => tsp::main(&args[1..]),
"life" => life::main(&args[1..]),
"noop" => noop::main(&args[1..]),
_ => usage(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion rayon-demo/src/nbody/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn nbody_seq(b: &mut ::test::Bencher) {
}

#[bench]
fn nbody_par(b: &mut ::test::Bencher) {
fn nbody_par_iter(b: &mut ::test::Bencher) {
nbody_bench(b, |n| {
n.tick_par();
});
Expand Down
33 changes: 33 additions & 0 deletions rayon-demo/src/noop/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const USAGE: &str = "
Usage: noop [--sleep N] [--iters N]
Noop loop to measure CPU usage. See rayon-rs/rayon#642.
Options:
--sleep N How long to sleep (in millis) between doing a spawn. [default: 10]
--iters N Total time to execution (in millis). [default: 100]
";

use cpu_time;
use docopt::Docopt;

#[derive(Deserialize)]
pub struct Args {
flag_sleep: u64,
flag_iters: u64,
}

pub fn main(args: &[String]) {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.argv(args).deserialize())
.unwrap_or_else(|e| e.exit());

let m = cpu_time::measure_cpu(|| {
for _ in 1..args.flag_iters {
std::thread::sleep(std::time::Duration::from_millis(args.flag_sleep));
rayon::spawn(move || { } );
}
});
println!("noop --iters={} --sleep={}", args.flag_iters, args.flag_sleep);
cpu_time::print_time(m);
}

0 comments on commit 572d1e0

Please sign in to comment.