2011-06-29 14:29:02 -05:00
|
|
|
// -*- rust -*-
|
2011-10-21 06:14:28 -05:00
|
|
|
// xfail-pretty
|
2011-06-29 14:29:02 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
2012-01-11 08:15:54 -06:00
|
|
|
import std::{time, io, getopts};
|
|
|
|
import io::writer_util;
|
2011-12-13 18:25:51 -06:00
|
|
|
import int::range;
|
|
|
|
import comm::port;
|
|
|
|
import comm::chan;
|
|
|
|
import comm::send;
|
|
|
|
import comm::recv;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2011-12-15 18:11:29 -06:00
|
|
|
import core::result;
|
|
|
|
import result::{ok, err};
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn fib(n: int) -> int {
|
2012-01-06 22:55:56 -06:00
|
|
|
fn pfib(c: chan<int>, n: int) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if n == 0 {
|
2011-08-13 18:03:28 -05:00
|
|
|
send(c, 0);
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if n <= 2 {
|
2011-08-13 18:03:28 -05:00
|
|
|
send(c, 1);
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2011-08-25 13:20:43 -05:00
|
|
|
let p = port();
|
2012-01-06 22:55:56 -06:00
|
|
|
let ch = chan(p);
|
|
|
|
task::spawn {|| pfib(ch, n - 1); };
|
|
|
|
task::spawn {|| pfib(ch, n - 2); };
|
2011-08-25 13:20:43 -05:00
|
|
|
send(c, recv(p) + recv(p));
|
2011-06-29 14:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 13:20:43 -05:00
|
|
|
let p = port();
|
2012-01-06 22:55:56 -06:00
|
|
|
let ch = chan(p);
|
|
|
|
let t = task::spawn {|| pfib(ch, n); };
|
2011-08-25 13:20:43 -05:00
|
|
|
ret recv(p);
|
2011-06-29 14:29:02 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type config = {stress: bool};
|
2011-07-07 19:28:20 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn parse_opts(argv: [str]) -> config {
|
2011-09-02 17:34:58 -05:00
|
|
|
let opts = [getopts::optflag("stress")];
|
2011-07-07 19:28:20 -05:00
|
|
|
|
2011-08-15 18:38:23 -05:00
|
|
|
let opt_args = vec::slice(argv, 1u, vec::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-12-15 18:11:29 -06:00
|
|
|
ok(m) { ret {stress: getopts::opt_present(m, "stress")} }
|
|
|
|
err(_) { fail; }
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 22:34:04 -05:00
|
|
|
fn stress_task(&&id: int) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0;
|
|
|
|
while true {
|
|
|
|
let n = 15;
|
|
|
|
assert (fib(n) == fib(n));
|
2011-07-07 19:28:20 -05:00
|
|
|
i += 1;
|
2011-12-22 16:42:52 -06:00
|
|
|
#error("%d: Completed %d iterations", id, i);
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn stress(num_tasks: int) {
|
2012-02-18 18:34:42 -06:00
|
|
|
let results = [];
|
2011-10-21 06:14:28 -05:00
|
|
|
range(0, num_tasks) {|i|
|
2012-02-18 18:34:42 -06:00
|
|
|
let builder = task::mk_task_builder();
|
|
|
|
results += [task::future_result(builder)];
|
|
|
|
task::run(builder) {|| stress_task(i); }
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
2012-02-18 18:34:42 -06:00
|
|
|
for r in results { future::get(r); }
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
fn main(argv: [str]) {
|
2011-08-15 18:38:23 -05:00
|
|
|
if vec::len(argv) == 1u {
|
2011-06-29 14:29:02 -05:00
|
|
|
assert (fib(8) == 21);
|
2011-12-22 19:53:53 -06:00
|
|
|
log(debug, fib(8));
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2011-06-29 14:29:02 -05:00
|
|
|
// Interactive mode! Wooo!!!!
|
2011-08-14 00:34:16 -05:00
|
|
|
let opts = parse_opts(argv);
|
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-09-01 19:27:58 -05:00
|
|
|
let max = uint::parse_buf(str::bytes(argv[1]), 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-10-21 06:14:28 -05:00
|
|
|
range(1, max + 1) {|n|
|
|
|
|
range(0, num_trials) {|i|
|
2011-07-27 07:19:39 -05:00
|
|
|
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
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
out.write_line(#fmt["%d\t%d\t%s", n, fibn,
|
|
|
|
u64::str(elapsed)]);
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
|
|
|
}
|
2011-06-29 14:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 23:54:52 -05:00
|
|
|
}
|