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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-09-18 17:52:21 -05:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::{time, getopts};
|
|
|
|
use io::WriterUtil;
|
|
|
|
use int::range;
|
|
|
|
use pipes::Port;
|
|
|
|
use pipes::Chan;
|
|
|
|
use pipes::send;
|
|
|
|
use pipes::recv;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use core::result;
|
|
|
|
use result::{Ok, Err};
|
2011-12-15 18:11:29 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn fib(n: int) -> int {
|
2012-08-28 13:11:15 -05:00
|
|
|
fn pfib(c: Chan<int>, n: int) {
|
2011-07-27 07:19:39 -05:00
|
|
|
if n == 0 {
|
2012-08-01 17:28:28 -05:00
|
|
|
c.send(0);
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if n <= 2 {
|
2012-08-01 17:28:28 -05:00
|
|
|
c.send(1);
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2012-08-16 19:50:21 -05:00
|
|
|
let p = pipes::PortSet();
|
2012-08-01 17:28:28 -05:00
|
|
|
let ch = p.chan();
|
2012-06-30 18:19:07 -05:00
|
|
|
task::spawn(|| pfib(ch, n - 1) );
|
2012-08-01 17:28:28 -05:00
|
|
|
let ch = p.chan();
|
2012-06-30 18:19:07 -05:00
|
|
|
task::spawn(|| pfib(ch, n - 2) );
|
2012-08-01 17:28:28 -05:00
|
|
|
c.send(p.recv() + p.recv());
|
2011-06-29 14:29:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:28:28 -05:00
|
|
|
let (ch, p) = pipes::stream();
|
2012-06-30 18:19:07 -05:00
|
|
|
let t = task::spawn(|| pfib(ch, n) );
|
2012-08-01 17:28:28 -05:00
|
|
|
p.recv()
|
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
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_opts(argv: ~[~str]) -> config {
|
|
|
|
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
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match getopts::getopts(opt_args, opts) {
|
2012-08-26 18:54:31 -05:00
|
|
|
Ok(m) => { return {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) {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2012-03-09 18:11:56 -06:00
|
|
|
loop {
|
2011-07-27 07:19:39 -05:00
|
|
|
let n = 15;
|
|
|
|
assert (fib(n) == fib(n));
|
2011-07-07 19:28:20 -05:00
|
|
|
i += 1;
|
2012-08-22 19:24:52 -05: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-06-29 18:26:56 -05:00
|
|
|
let mut results = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for range(0, num_tasks) |i| {
|
2012-08-07 13:26:03 -05:00
|
|
|
do task::task().future_result(|+r| {
|
2012-09-07 17:59:14 -05:00
|
|
|
vec::push(results, r);
|
2012-07-23 18:58:47 -05:00
|
|
|
}).spawn {
|
|
|
|
stress_task(i);
|
|
|
|
}
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
2012-09-19 18:55:01 -05:00
|
|
|
for results.each |r| { future::get(r); }
|
2011-07-07 19:28:20 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn main(args: ~[~str]) {
|
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"20"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"8"]
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2012-05-24 00:53:50 -05:00
|
|
|
args
|
|
|
|
};
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
let opts = parse_opts(args);
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
if opts.stress {
|
|
|
|
stress(2);
|
|
|
|
} else {
|
2012-09-14 11:55:33 -05:00
|
|
|
let max = option::get(uint::parse_bytes(str::to_bytes(args[1]),
|
|
|
|
10u)) as int;
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
let num_trials = 10;
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
let out = io::stdout();
|
2011-06-29 14:29:02 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for range(1, max + 1) |n| {
|
|
|
|
for range(0, num_trials) |i| {
|
2012-05-24 00:53:50 -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
|
|
|
|
2012-05-24 00:53:50 -05:00
|
|
|
let elapsed = stop - start;
|
2011-07-13 17:44:09 -05:00
|
|
|
|
2012-08-22 19:24:52 -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
|
|
|
}
|