Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 118 lines (94 sloc) 2.601 kb
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
1 // -*- rust -*-
050170d Marijn Haverbeke 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
6f5853f Graydon Hoare Libc/os/run/rand/io reorganization. Close #1373. Close #1638.
graydon authored
15 import std::{time, getopts};
34d7f05 Marijn Haverbeke Major clean-up of std::io
marijnh authored
16 import io::writer_util;
fa9ad98 Graydon Hoare 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 ta...
Eric Holk authored
22
fa27724 Stefan Plantikow std: getopts now uses result::t (fixes #1289)
boggle authored
23 import core::result;
24 import result::{ok, err};
25
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
26 fn fib(n: int) -> int {
a1ef79c Niko Matsakis update to use new spawn syntax
nikomatsakis authored
27 fn pfib(c: chan<int>, n: int) {
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
28 if n == 0 {
aa0a51a Converted over benchmarks.
Eric Holk authored
29 send(c, 0);
518dc52 Brian Anderson Reformat
brson authored
30 } else if n <= 2 {
aa0a51a Converted over benchmarks.
Eric Holk authored
31 send(c, 1);
df7f21d Marijn Haverbeke 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 Niko Matsakis update to use new spawn syntax
nikomatsakis authored
34 let ch = chan(p);
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
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 ta...
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 Niko Matsakis update to use new spawn syntax
nikomatsakis authored
42 let ch = chan(p);
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
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 ta...
Eric Holk authored
45 }
46
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
47 type config = {stress: bool};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
48
92743dc Michael J. Sullivan 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 Brian Anderson 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 Marijn Haverbeke Reformat for new syntax
marijnh authored
54
5f71a20 Brian Anderson Remove vecs from std::getopts
brson authored
55 alt getopts::getopts(opt_args, opts) {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
56 ok(m) { ret {stress: getopts::opt_present(m, ~"stress")} }
fa27724 Stefan Plantikow std: getopts now uses result::t (fixes #1289)
boggle authored
57 err(_) { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
58 }
59 }
60
ebc61e3 Brian Anderson Remove temporary fn# syntax
brson authored
61 fn stress_task(&&id: int) {
dc07280 Niko Matsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
62 let mut i = 0;
321fd80 Tim Chevalier Add an infinite loop construct
catamorphism authored
63 loop {
df7f21d Marijn Haverbeke 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 Hoare Register snapshots and switch logging over to use of log_full or #error ...
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 Marijn Haverbeke Reformat for new syntax
marijnh authored
71 fn stress(num_tasks: int) {
98e161f Michael J. Sullivan Switch the compiler over to using ~[] notation instead of []/~. Closes #...
msullivan authored
72 let mut results = ~[];
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
73 for range(0, num_tasks) |i| {
b30a58a Ben Blum Convert bench and run-pass tests to new task_builder interface
bblum authored
74 do task::task().future_result(|-r| {
75 results += ~[r];
76 }).spawn {
77 stress_task(i);
78 }
3b5b932 Marijn Haverbeke Remove some semicolons after block calls
marijnh authored
79 }
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
80 for results.each |r| { future::get(r); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
81 }
82
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
83 fn main(args: ~[~str]) {
84 let args = if os::getenv(~"RUST_BENCH").is_some() {
85 ~[~"", ~"20"]
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
86 } else if args.len() <= 1u {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
87 ~[~"", ~"8"]
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
88 } else {
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
89 args
90 };
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
91
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
92 let opts = parse_opts(args);
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
93
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
94 if opts.stress {
95 stress(2);
96 } else {
97 let max = option::get(uint::parse_buf(str::bytes(args[1]),
98 10u)) as int;
df7f21d Marijn Haverbeke Reformat for new syntax
marijnh authored
99
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
100 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
101
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
102 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
103
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
104 for range(1, max + 1) |n| {
105 for range(0, num_trials) |i| {
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
106 let start = time::precise_time_ns();
107 let fibn = fib(n);
108 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
109
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
110 let elapsed = stop - start;
39151f2 Graydon Hoare Prohibit trailing whitespace under 'tidy' script. Clean up all caught ca...
graydon authored
111
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
112 out.write_line(#fmt["%d\t%d\t%s", n, fibn,
113 u64::str(elapsed)]);
3b5b932 Marijn Haverbeke Remove some semicolons after block calls
marijnh authored
114 }
e192755 Added a new version of pfib that's better suited for benchmarking the ta...
Eric Holk authored
115 }
116 }
f91351a Lindsey Kuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
117 }
Something went wrong with that request. Please try again.