1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// -*- rust -*-
// xfail-pretty
/*
A parallel version of fibonacci numbers.
This version is meant mostly as a way of stressing and benchmarking
the task system. It supports a lot of command-line arguments to
control how it runs.
*/
use std;
import std::{time, getopts};
import io::writer_util;
import int::range;
import comm::port;
import comm::chan;
import comm::send;
import comm::recv;
import core::result;
import result::{ok, err};
fn fib(n: int) -> int {
fn pfib(c: chan<int>, n: int) {
if n == 0 {
send(c, 0);
} else if n <= 2 {
send(c, 1);
} else {
let p = port();
let ch = chan(p);
task::spawn(|| pfib(ch, n - 1) );
task::spawn(|| pfib(ch, n - 2) );
send(c, recv(p) + recv(p));
}
}
let p = port();
let ch = chan(p);
let t = task::spawn(|| pfib(ch, n) );
ret recv(p);
}
type config = {stress: bool};
fn parse_opts(argv: ~[str]) -> config {
let opts = ~[getopts::optflag("stress")];
let opt_args = vec::slice(argv, 1u, vec::len(argv));
alt getopts::getopts(opt_args, opts) {
ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
err(_) { fail; }
}
}
fn stress_task(&&id: int) {
let mut i = 0;
loop {
let n = 15;
assert (fib(n) == fib(n));
i += 1;
#error("%d: Completed %d iterations", id, i);
}
}
fn stress(num_tasks: int) {
let mut results = ~[];
for range(0, num_tasks) |i| {
let builder = task::builder();
results += ~[task::future_result(builder)];
task::run(builder, || stress_task(i) );
}
for results.each |r| { future::get(r); }
}
fn main(args: ~[str]) {
let args = if os::getenv("RUST_BENCH").is_some() {
~["", "20"]
} else if args.len() <= 1u {
~["", "8"]
} else {
args
};
let opts = parse_opts(args);
if opts.stress {
stress(2);
} else {
let max = option::get(uint::parse_buf(str::bytes(args[1]),
10u)) as int;
let num_trials = 10;
let out = io::stdout();
for range(1, max + 1) |n| {
for range(0, num_trials) |i| {
let start = time::precise_time_ns();
let fibn = fib(n);
let stop = time::precise_time_ns();
let elapsed = stop - start;
out.write_line(#fmt["%d\t%d\t%s", n, fibn,
u64::str(elapsed)]);
}
}
}
}
|