rust/src/test/bench/shootout-pfib.rs

117 lines
2.5 KiB
Rust
Raw Normal View History

// -*- 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;
2011-08-15 18:38:23 -05:00
import std::vec;
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;
import std::u64;
2011-08-13 18:03:28 -05:00
import std::comm;
import std::comm::port;
import std::comm::chan;
2011-08-13 18:03:28 -05:00
import std::comm::send;
import std::comm::recv;
2011-07-27 07:19:39 -05:00
fn fib(n: int) -> int {
2011-10-20 22:34:04 -05:00
fn pfib(args: (chan<int>, int)) {
let (c, n) = args;
2011-07-27 07:19:39 -05:00
if n == 0 {
2011-08-13 18:03:28 -05:00
send(c, 0);
} else if n <= 2 {
2011-08-13 18:03:28 -05:00
send(c, 1);
2011-07-27 07:19:39 -05:00
} else {
let p = port();
let t1 = task::spawn((chan(p), n - 1), pfib);
let t2 = task::spawn((chan(p), n - 2), pfib);
send(c, recv(p) + recv(p));
}
}
let p = port();
let t = task::spawn((chan(p), n), pfib);
ret recv(p);
}
2011-07-27 07:19:39 -05:00
type config = {stress: bool};
2011-07-07 19:28:20 -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-09-02 17:34:58 -05:00
getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
2011-07-27 07:19:39 -05:00
getopts::failure(_) { 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;
log_err #fmt["%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) {
2011-08-13 18:03:28 -05:00
let tasks = [];
range(0, num_tasks) {|i|
tasks += [task::spawn_joinable(copy i, stress_task)];
}
for t in tasks { task::join(t); }
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 {
assert (fib(8) == 21);
log fib(8);
2011-07-27 07:19:39 -05:00
} else {
// Interactive mode! Wooo!!!!
let opts = parse_opts(argv);
2011-07-27 07:19:39 -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 {
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-08-11 21:14:38 -05:00
let out = io::stdout();
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-07-27 07:19:39 -05:00
let elapsed = stop - start;
2011-09-02 17:34:58 -05:00
out.write_line(#fmt["%d\t%d\t%s", n, fibn,
u64::str(elapsed)]);
}
}
}
}
}