Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (94 sloc) 2.621 kb
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
1 // -*- rust -*-
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
2 // xfail-pretty
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
3
4 /*
5 A parallel version of fibonacci numbers.
6
7 This version is meant mostly as a way of stressing and benchmarking
8 the task system. It supports a lot of command-line arguments to
9 control how it runs.
10
11 */
12
13 use std;
14
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
15 use std::{time, getopts};
16 use io::WriterUtil;
17 use int::range;
18 use pipes::Port;
19 use pipes::Chan;
20 use pipes::send;
21 use pipes::recv;
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
22
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
23 use core::result;
24 use result::{Ok, Err};
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
25
df7f21d @marijnh Reformat for new syntax
marijnh authored
26 fn fib(n: int) -> int {
cfbc7cb @brson Convert core::pipes to camel case
brson authored
27 fn pfib(c: Chan<int>, n: int) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
28 if n == 0 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
29 c.send(0);
518dc52 @brson Reformat
brson authored
30 } else if n <= 2 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
31 c.send(1);
df7f21d @marijnh Reformat for new syntax
marijnh authored
32 } else {
5f57588 @eholk Update the rock-paper-scissors example in the tutorial, and rename some ...
eholk authored
33 let p = pipes::PortSet();
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
34 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
35 task::spawn(|| pfib(ch, n - 1) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
36 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
37 task::spawn(|| pfib(ch, n - 2) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
38 c.send(p.recv() + p.recv());
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
39 }
40 }
41
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
42 let (ch, p) = pipes::stream();
d1fc2b5 @brson Convert to new closure syntax
brson authored
43 let t = task::spawn(|| pfib(ch, n) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
44 p.recv()
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
45 }
46
df7f21d @marijnh Reformat for new syntax
marijnh authored
47 type config = {stress: bool};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
48
92743dc @msullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
49 fn parse_opts(argv: ~[~str]) -> config {
50 let opts = ~[getopts::optflag(~"stress")];
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
51
f05a91a @brson Rename std::ivec to std::vec
brson authored
52 let opt_args = vec::slice(argv, 1u, vec::len(argv));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
53
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
54 match getopts::getopts(opt_args, opts) {
0c6e470 @brson Convert core::result to camel case
brson authored
55 Ok(m) => { return {stress: getopts::opt_present(m, ~"stress")} }
56 Err(_) => { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
57 }
58 }
59
ebc61e3 @brson Remove temporary fn# syntax
brson authored
60 fn stress_task(&&id: int) {
dc07280 @nikomatsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
61 let mut i = 0;
321fd80 @catamorphism Add an infinite loop construct
catamorphism authored
62 loop {
df7f21d @marijnh Reformat for new syntax
marijnh authored
63 let n = 15;
64 assert (fib(n) == fib(n));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
65 i += 1;
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
66 error!("%d: Completed %d iterations", id, i);
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
67 }
68 }
69
df7f21d @marijnh Reformat for new syntax
marijnh authored
70 fn stress(num_tasks: int) {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Closes #...
msullivan authored
71 let mut results = ~[];
d1fc2b5 @brson Convert to new closure syntax
brson authored
72 for range(0, num_tasks) |i| {
18ac4a8 @bblum Change task().future_result's argument mode
bblum authored
73 do task::task().future_result(|+r| {
b30a58a @bblum Convert bench and run-pass tests to new task_builder interface
bblum authored
74 results += ~[r];
75 }).spawn {
76 stress_task(i);
77 }
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
78 }
9600744 @graydon fix more fallout from future de-mode-ing.
graydon authored
79 for results.each |r| { future::get(&r); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
80 }
81
92743dc @msullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
82 fn main(args: ~[~str]) {
83 let args = if os::getenv(~"RUST_BENCH").is_some() {
84 ~[~"", ~"20"]
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
85 } else if args.len() <= 1u {
92743dc @msullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
86 ~[~"", ~"8"]
df7f21d @marijnh Reformat for new syntax
marijnh authored
87 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
88 args
89 };
df7f21d @marijnh Reformat for new syntax
marijnh authored
90
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
91 let opts = parse_opts(args);
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
92
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
93 if opts.stress {
94 stress(2);
95 } else {
9f59131 @catamorphism Rename str::bytes to str::to_bytes
catamorphism authored
96 let max = option::get(uint::parse_buf(str::to_bytes(args[1]),
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
97 10u)) as int;
df7f21d @marijnh Reformat for new syntax
marijnh authored
98
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
99 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
100
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
101 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
102
d1fc2b5 @brson Convert to new closure syntax
brson authored
103 for range(1, max + 1) |n| {
104 for range(0, num_trials) |i| {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
105 let start = time::precise_time_ns();
106 let fibn = fib(n);
107 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
108
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
109 let elapsed = stop - start;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught ca...
graydon authored
110
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
111 out.write_line(fmt!("%d\t%d\t%s", n, fibn,
112 u64::str(elapsed)));
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
113 }
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
114 }
115 }
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
116 }
Something went wrong with that request. Please try again.