Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (92 sloc) 2.603 kB
e192755 Added a new version of pfib that's better suited for benchmarking the…
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…
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
6f5853f @graydon Libc/os/run/rand/io reorganization. Close #1373. Close #1638.
graydon authored
15 import std::{time, getopts};
34d7f05 @marijnh Major clean-up of std::io
marijnh authored
16 import io::writer_util;
fa9ad98 @graydon Copy first batch of material from libstd to libcore.
graydon authored
17 import int::range;
18 import comm::port;
19 import comm::chan;
20 import comm::send;
21 import comm::recv;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
22
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
23 import core::result;
24 import result::{ok, err};
25
df7f21d @marijnh Reformat for new syntax
marijnh authored
26 fn fib(n: int) -> int {
a1ef79c @nikomatsakis update to use new spawn syntax
nikomatsakis authored
27 fn pfib(c: chan<int>, n: int) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
28 if n == 0 {
aa0a51a Converted over benchmarks.
Eric Holk authored
29 send(c, 0);
518dc52 @brson Reformat
brson authored
30 } else if n <= 2 {
aa0a51a Converted over benchmarks.
Eric Holk authored
31 send(c, 1);
df7f21d @marijnh Reformat for new syntax
marijnh authored
32 } else {
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
33 let p = port();
a1ef79c @nikomatsakis update to use new spawn syntax
nikomatsakis authored
34 let ch = chan(p);
35 task::spawn {|| pfib(ch, n - 1); };
36 task::spawn {|| pfib(ch, n - 2); };
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
37 send(c, recv(p) + recv(p));
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
38 }
39 }
40
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
41 let p = port();
a1ef79c @nikomatsakis update to use new spawn syntax
nikomatsakis authored
42 let ch = chan(p);
43 let t = task::spawn {|| pfib(ch, n); };
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
44 ret recv(p);
e192755 Added a new version of pfib that's better suited for benchmarking the…
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
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
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
df7f21d @marijnh Reformat for new syntax
marijnh authored
54
5f71a20 @brson Remove vecs from std::getopts
brson authored
55 alt getopts::getopts(opt_args, opts) {
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
56 ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
57 err(_) { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
58 }
59 }
60
ebc61e3 @brson Remove temporary fn# syntax
brson authored
61 fn stress_task(&&id: int) {
dc07280 @nikomatsakis make --enforce-mut-vars always on, add mut annotations to remaining f…
nikomatsakis authored
62 let mut i = 0;
321fd80 @catamorphism Add an infinite loop construct
catamorphism authored
63 loop {
df7f21d @marijnh Reformat for new syntax
marijnh authored
64 let n = 15;
65 assert (fib(n) == fib(n));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
66 i += 1;
8b58095 @graydon Register snapshots and switch logging over to use of log_full or #err…
graydon authored
67 #error("%d: Completed %d iterations", id, i);
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
68 }
69 }
70
df7f21d @marijnh Reformat for new syntax
marijnh authored
71 fn stress(num_tasks: int) {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
72 let mut results = ~[];
432c6cb @brson core: Make range follow the for loop protocol
brson authored
73 for range(0, num_tasks) {|i|
98b07dd @catamorphism Rename task::task_builder to task::builder
catamorphism authored
74 let builder = task::builder();
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
75 results += ~[task::future_result(builder)];
4220dcf @brson core: New task API
brson authored
76 task::run(builder) {|| stress_task(i); }
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
77 }
c902eaf @marijnh Convert old-style for loops to new-style
marijnh authored
78 for results.each {|r| future::get(r); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
79 }
80
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
81 fn main(args: ~[str]) {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
82 let args = if os::getenv("RUST_BENCH").is_some() {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
83 ~["", "20"]
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
84 } else if args.len() <= 1u {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
85 ~["", "8"]
df7f21d @marijnh Reformat for new syntax
marijnh authored
86 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
87 args
88 };
df7f21d @marijnh Reformat for new syntax
marijnh authored
89
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
90 let opts = parse_opts(args);
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
91
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
92 if opts.stress {
93 stress(2);
94 } else {
95 let max = option::get(uint::parse_buf(str::bytes(args[1]),
96 10u)) as int;
df7f21d @marijnh Reformat for new syntax
marijnh authored
97
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
98 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
99
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
100 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
101
432c6cb @brson core: Make range follow the for loop protocol
brson authored
102 for range(1, max + 1) {|n|
103 for range(0, num_trials) {|i|
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
104 let start = time::precise_time_ns();
105 let fibn = fib(n);
106 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
107
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
108 let elapsed = stop - start;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
109
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
110 out.write_line(#fmt["%d\t%d\t%s", n, fibn,
111 u64::str(elapsed)]);
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
112 }
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
113 }
114 }
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
115 }
Something went wrong with that request. Please try again.