Skip to content

Commit 572d1e0

Browse files
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 rayon-rs#642. Co-authored-by: Niko Matsakis <niko@alum.mit.edu>
2 parents 68edcf6 + d98d420 commit 572d1e0

File tree

9 files changed

+101
-34
lines changed

9 files changed

+101
-34
lines changed

rayon-demo/src/cpu_time/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use time::{self, Duration};
2+
3+
#[cfg(windows)]
4+
mod win;
5+
#[cfg(windows)]
6+
pub use self::win::get_cpu_time;
7+
8+
#[cfg(unix)]
9+
mod unix;
10+
#[cfg(unix)]
11+
pub use self::unix::get_cpu_time;
12+
13+
#[cfg(not(any(unix, windows)))]
14+
pub fn get_cpu_time() -> Option<u64> {
15+
None
16+
}
17+
18+
pub fn get_cpu_duration(start: Option<u64>, stop: Option<u64>) -> Option<Duration> {
19+
start.and_then(|start| stop.and_then(|stop| Some(Duration::nanoseconds((stop - start) as i64))))
20+
}
21+
22+
#[derive(Copy, Clone)]
23+
pub struct CpuMeasure {
24+
/// number of ns
25+
pub time_duration: u64,
26+
27+
/// percentage (0-100) of that as cpu time
28+
pub cpu_usage_percent: Option<f64>,
29+
}
30+
31+
pub fn measure_cpu(op: impl FnOnce()) -> CpuMeasure {
32+
let time_start = time::precise_time_ns();
33+
let cpu_start = get_cpu_time();
34+
35+
op();
36+
37+
let cpu_stop = get_cpu_time();
38+
let time_duration = time::precise_time_ns() - time_start;
39+
40+
CpuMeasure {
41+
time_duration,
42+
cpu_usage_percent: get_cpu_duration(cpu_start, cpu_stop)
43+
.and_then(|cpu| cpu.num_nanoseconds())
44+
.and_then(|cpu| Some(100.0 * cpu as f64 / time_duration as f64)),
45+
}
46+
}
47+
48+
pub fn print_time(m: CpuMeasure) {
49+
println!(" wallclock: {} ns", m.time_duration);
50+
if let Some(cpu_usage) = m.cpu_usage_percent {
51+
println!(" cpu usage: {:3.1}%", cpu_usage);
52+
} else {
53+
println!(" cpu usage: N/A");
54+
}
55+
}
File renamed without changes.

rayon-demo/src/life/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ fn generations(b: &mut ::test::Bencher) {
66
}
77

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

1313
#[bench]
14-
fn as_parallel_generations(b: &mut ::test::Bencher) {
14+
fn par_bridge_generations(b: &mut ::test::Bencher) {
1515
b.iter(|| super::par_bridge_generations(Board::new(200, 200).random(), 100));
1616
}

rayon-demo/src/life/cpu_time/mod.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

rayon-demo/src/life/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Options:
1414
-h, --help Show this message.
1515
";
1616

17+
use cpu_time::{self, CpuMeasure};
1718
use rand::distributions::Standard;
1819
use rand::{thread_rng, Rng};
1920
use std::iter::repeat;
@@ -28,7 +29,6 @@ use rayon::prelude::*;
2829

2930
#[cfg(test)]
3031
mod bench;
31-
mod cpu_time;
3232

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

257-
let cpu_stop = cpu_time::get_cpu_time();
258-
let duration = time::precise_time_ns() - start;
253+
let CpuMeasure { time_duration, cpu_usage_percent } = cpu_time::measure_cpu(|| {
254+
f(brd, gens, interval)
255+
});
259256

260257
CpuResult {
261-
actual_fps: (1_000_000_000.0 * gens as f64) / duration as f64,
262-
cpu_usage_percent: cpu_time::get_cpu_duration(cpu_start, cpu_stop)
263-
.and_then(|cpu| cpu.num_nanoseconds())
264-
.and_then(|cpu| Some(100.0 * cpu as f64 / duration as f64)),
258+
actual_fps: (1_000_000_000.0 * gens as f64) / time_duration as f64,
259+
cpu_usage_percent,
265260
}
266261
}
267262

rayon-demo/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use std::io;
55
use std::io::prelude::*;
66
use std::process::exit;
77

8+
mod cpu_time;
89
mod life;
910
mod matmul;
1011
mod mergesort;
1112
mod nbody;
13+
mod noop;
1214
mod quicksort;
1315
mod sieve;
1416
mod tsp;
@@ -81,6 +83,7 @@ Benchmarks:
8183
- sieve: Finding primes using a Sieve of Eratosthenes.
8284
- matmul: Parallel matrix multiplication.
8385
- mergesort: Parallel mergesort.
86+
- noop: Launch empty tasks to measure CPU usage.
8487
- quicksort: Parallel quicksort.
8588
- tsp: Traveling salesman problem solver (sample data sets in `data/tsp`).
8689
";
@@ -106,6 +109,7 @@ fn main() {
106109
"sieve" => sieve::main(&args[1..]),
107110
"tsp" => tsp::main(&args[1..]),
108111
"life" => life::main(&args[1..]),
112+
"noop" => noop::main(&args[1..]),
109113
_ => usage(),
110114
}
111115
}

rayon-demo/src/nbody/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn nbody_seq(b: &mut ::test::Bencher) {
2828
}
2929

3030
#[bench]
31-
fn nbody_par(b: &mut ::test::Bencher) {
31+
fn nbody_par_iter(b: &mut ::test::Bencher) {
3232
nbody_bench(b, |n| {
3333
n.tick_par();
3434
});

rayon-demo/src/noop/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const USAGE: &str = "
2+
Usage: noop [--sleep N] [--iters N]
3+
4+
Noop loop to measure CPU usage. See rayon-rs/rayon#642.
5+
6+
Options:
7+
--sleep N How long to sleep (in millis) between doing a spawn. [default: 10]
8+
--iters N Total time to execution (in millis). [default: 100]
9+
";
10+
11+
use cpu_time;
12+
use docopt::Docopt;
13+
14+
#[derive(Deserialize)]
15+
pub struct Args {
16+
flag_sleep: u64,
17+
flag_iters: u64,
18+
}
19+
20+
pub fn main(args: &[String]) {
21+
let args: Args = Docopt::new(USAGE)
22+
.and_then(|d| d.argv(args).deserialize())
23+
.unwrap_or_else(|e| e.exit());
24+
25+
let m = cpu_time::measure_cpu(|| {
26+
for _ in 1..args.flag_iters {
27+
std::thread::sleep(std::time::Duration::from_millis(args.flag_sleep));
28+
rayon::spawn(move || { } );
29+
}
30+
});
31+
println!("noop --iters={} --sleep={}", args.flag_iters, args.flag_sleep);
32+
cpu_time::print_time(m);
33+
}

0 commit comments

Comments
 (0)