2011-06-29 14:29:02 -05:00
|
|
|
// -*- rust -*-
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
2011-08-11 18:36:20 -05:00
|
|
|
import std::ivec;
|
2011-06-29 14:29:02 -05:00
|
|
|
import std::uint;
|
|
|
|
import std::time;
|
|
|
|
import std::str;
|
|
|
|
import std::int::range;
|
2011-08-11 21:14:38 -05:00
|
|
|
import std::io;
|
2011-07-07 19:28:20 -05:00
|
|
|
import std::getopts;
|
|
|
|
import std::task;
|
2011-07-08 14:18:54 -05:00
|
|
|
import std::u64;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn recv[T](p: &port[T]) -> T { let x: T; p |> x; ret x; }
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn fib(n: int) -> int {
|
|
|
|
fn pfib(c: chan[int], n: int) {
|
|
|
|
if n == 0 {
|
2011-06-29 14:29:02 -05:00
|
|
|
c <| 0;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else if (n <= 2) {
|
2011-06-29 14:29:02 -05:00
|
|
|
c <| 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
|
|
|
let p = port();
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let t1 = spawn pfib(chan(p), n - 1);
|
|
|
|
let t2 = spawn pfib(chan(p), n - 2);
|
2011-06-29 14:29:02 -05:00
|
|
|
|
|
|
|
c <| recv(p) + recv(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let p = port();
|
|
|
|
let t = spawn pfib(chan(p), n);
|
2011-06-29 14:29:02 -05:00
|
|
|
ret recv(p);
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type config = {stress: bool};
|
2011-07-07 19:28:20 -05:00
|
|
|
|
2011-08-11 23:37:27 -05:00
|
|
|
fn parse_opts(argv: [str]) -> config {
|
|
|
|
let opts = ~[getopts::optflag("stress")];
|
2011-07-07 19:28:20 -05:00
|
|
|
|
2011-08-11 23:37:27 -05:00
|
|
|
let opt_args = ivec::slice(argv, 1u, ivec::len(argv));
|
2011-07-07 19:28:20 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-08-12 01:27:32 -05:00
|
|
|
alt getopts::getopts(opt_args, opts) {
|
2011-07-27 07:19:39 -05:00
|
|
|
getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
|
|
|
|
getopts::failure(_) { fail; }
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn stress_task(id: int) {
|
|
|
|
let i = 0;
|
|
|
|
while true {
|
|
|
|
let n = 15;
|
|
|
|
assert (fib(n) == fib(n));
|
2011-07-07 19:28:20 -05:00
|
|
|
i += 1;
|
|
|
|
log_err #fmt("%d: Completed %d iterations", id, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn stress(num_tasks: int) {
|
|
|
|
let tasks = [];
|
|
|
|
for each i: int in range(0, num_tasks) {
|
2011-07-07 19:28:20 -05:00
|
|
|
tasks += [spawn stress_task(i)];
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
for each i: int in range(0, num_tasks) { task::join(tasks.(i)); }
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn main(argv: vec[str]) {
|
2011-08-11 23:37:27 -05:00
|
|
|
let iargv = ivec::from_vec(argv);
|
|
|
|
if ivec::len(iargv) == 1u {
|
2011-06-29 14:29:02 -05:00
|
|
|
assert (fib(8) == 21);
|
|
|
|
log fib(8);
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2011-06-29 14:29:02 -05:00
|
|
|
// Interactive mode! Wooo!!!!
|
2011-08-11 23:37:27 -05:00
|
|
|
let opts = parse_opts(iargv);
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
if opts.stress {
|
2011-07-07 19:28:20 -05:00
|
|
|
stress(2);
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2011-08-11 23:37:27 -05:00
|
|
|
let max = uint::parse_buf(ivec::to_vec(str::bytes(iargv.(1))),
|
2011-08-11 18:36:20 -05:00
|
|
|
10u) as int;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
|
|
|
let num_trials = 10;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-08-11 21:14:38 -05:00
|
|
|
let out = io::stdout();
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
for each n: int in range(1, max + 1) {
|
|
|
|
for each i: int in range(0, num_trials) {
|
|
|
|
let start = time::precise_time_ns();
|
|
|
|
let fibn = fib(n);
|
|
|
|
let stop = time::precise_time_ns();
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let elapsed = stop - start;
|
2011-07-13 17:44:09 -05:00
|
|
|
|
|
|
|
out.write_line(#fmt("%d\t%d\t%s", n, fibn,
|
2011-07-08 14:18:54 -05:00
|
|
|
u64::str(elapsed)));
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
2011-06-29 14:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|