5 A parallel version of fibonacci numbers.
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
31 import result::{ok, err};
33 fn fib(n: int) -> int {
34 fn pfib(args: (chan<int>, int)) {
43 let t1 = task::spawn((chan(p), n - 1), pfib);
44 let t2 = task::spawn((chan(p), n - 2), pfib);
46 send(c, recv(p) + recv(p));
51 let t = task::spawn((chan(p), n), pfib);
55 type config = {stress: bool};
57 fn parse_opts(argv: [str]) -> config {
58 let opts = [getopts::optflag("stress")];
60 let opt_args = vec::slice(argv, 1u, vec::len(argv));
63 alt getopts::getopts(opt_args, opts) {
64 ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
69 fn stress_task(&&id: int) {
73 assert (fib(n) == fib(n));
75 #error("%d: Completed %d iterations", id, i);
79 fn stress(num_tasks: int) {
81 range(0, num_tasks) {|i|
82 tasks += [task::spawn_joinable(copy i, stress_task)];
84 for t in tasks { task::join(t); }
87 fn main(argv: [str]) {
88 if vec::len(argv) == 1u {
89 assert (fib(8) == 21);
92 // Interactive mode! Wooo!!!!
93 let opts = parse_opts(argv);
99 let max = uint::parse_buf(str::bytes(argv[1]), 10u) as int;
103 let out = io::stdout();
105 range(1, max + 1) {|n|
106 range(0, num_trials) {|i|
107 let start = time::precise_time_ns();
109 let stop = time::precise_time_ns();
111 let elapsed = stop - start;
113 out.write_line(#fmt["%d\t%d\t%s", n, fibn,