2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2011-06-29 12:29:02 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
A parallel version of fibonacci numbers.
|
|
|
|
|
|
|
|
This version is meant mostly as a way of stressing and benchmarking
|
2013-01-28 23:54:39 -08:00
|
|
|
the task system. It supports a lot of old command-line arguments to
|
2011-06-29 12:29:02 -07:00
|
|
|
control how it runs.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
extern mod extra;
|
2011-06-29 12:29:02 -07:00
|
|
|
|
2013-05-20 17:07:24 -07:00
|
|
|
use extra::{time, getopts};
|
2013-10-02 20:00:54 -07:00
|
|
|
use std::comm::{stream, SharedChan};
|
2013-05-20 17:07:24 -07:00
|
|
|
use std::io::WriterUtil;
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::io;
|
|
|
|
use std::os;
|
2013-05-20 17:07:24 -07:00
|
|
|
use std::result::{Ok, Err};
|
2013-05-24 19:35:29 -07:00
|
|
|
use std::task;
|
|
|
|
use std::uint;
|
2011-12-16 01:11:29 +01:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn fib(n: int) -> int {
|
2013-07-30 23:39:58 -07:00
|
|
|
fn pfib(c: &SharedChan<int>, n: int) {
|
2011-07-27 14:19:39 +02:00
|
|
|
if n == 0 {
|
2012-08-01 15:28:28 -07:00
|
|
|
c.send(0);
|
2011-08-19 15:16:48 -07:00
|
|
|
} else if n <= 2 {
|
2012-08-01 15:28:28 -07:00
|
|
|
c.send(1);
|
2011-07-27 14:19:39 +02:00
|
|
|
} else {
|
2013-07-30 23:39:58 -07:00
|
|
|
let (pp, cc) = stream();
|
|
|
|
let cc = SharedChan::new(cc);
|
|
|
|
let ch = cc.clone();
|
2013-04-26 14:04:39 -07:00
|
|
|
task::spawn(|| pfib(&ch, n - 1) );
|
2013-07-30 23:39:58 -07:00
|
|
|
let ch = cc.clone();
|
2013-04-26 14:04:39 -07:00
|
|
|
task::spawn(|| pfib(&ch, n - 2) );
|
2013-07-30 23:39:58 -07:00
|
|
|
c.send(pp.recv() + pp.recv());
|
2011-06-29 12:29:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 03:10:12 -08:00
|
|
|
let (p, ch) = stream();
|
2013-07-30 23:39:58 -07:00
|
|
|
let ch = SharedChan::new(ch);
|
2013-04-26 14:04:39 -07:00
|
|
|
let _t = task::spawn(|| pfib(&ch, n) );
|
2012-08-01 15:28:28 -07:00
|
|
|
p.recv()
|
2011-06-29 12:29:02 -07:00
|
|
|
}
|
|
|
|
|
2013-01-28 18:55:44 -08:00
|
|
|
struct Config {
|
|
|
|
stress: bool
|
|
|
|
}
|
2011-07-07 17:28:20 -07:00
|
|
|
|
2013-01-28 18:55:44 -08:00
|
|
|
fn parse_opts(argv: ~[~str]) -> Config {
|
2013-07-17 15:31:20 -04:00
|
|
|
let opts = ~[getopts::optflag("stress")];
|
2011-07-07 17:28:20 -07:00
|
|
|
|
2013-06-27 19:48:50 +10:00
|
|
|
let opt_args = argv.slice(1, argv.len());
|
2011-07-07 17:28:20 -07:00
|
|
|
|
2012-08-06 12:34:08 -07:00
|
|
|
match getopts::getopts(opt_args, opts) {
|
2013-01-28 18:55:44 -08:00
|
|
|
Ok(ref m) => {
|
2013-09-18 03:42:23 +02:00
|
|
|
return Config {stress: m.opt_present("stress")}
|
2013-01-28 18:55:44 -08:00
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
Err(_) => { fail!(); }
|
2011-07-07 17:28:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 17:03:17 -07:00
|
|
|
fn stress_task(id: int) {
|
2012-03-22 08:39:41 -07:00
|
|
|
let mut i = 0;
|
2012-03-09 16:11:56 -08:00
|
|
|
loop {
|
2011-07-27 14:19:39 +02:00
|
|
|
let n = 15;
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(fib(n), fib(n));
|
2011-07-07 17:28:20 -07:00
|
|
|
i += 1;
|
2013-10-21 13:08:31 -07:00
|
|
|
error!("{}: Completed {} iterations", id, i);
|
2011-07-07 17:28:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn stress(num_tasks: int) {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut results = ~[];
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0, num_tasks) {
|
2013-05-06 19:29:04 -07:00
|
|
|
let mut builder = task::task();
|
2013-10-18 10:38:46 +02:00
|
|
|
results.push(builder.future_result());
|
2013-05-06 19:29:04 -07:00
|
|
|
do builder.spawn {
|
2012-07-23 19:58:47 -04:00
|
|
|
stress_task(i);
|
|
|
|
}
|
2011-10-21 14:12:12 +02:00
|
|
|
}
|
2013-08-03 12:45:23 -04:00
|
|
|
for r in results.iter() {
|
2013-05-06 19:29:04 -07:00
|
|
|
r.recv();
|
|
|
|
}
|
2011-07-07 17:28:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2013-07-17 15:31:20 -04:00
|
|
|
let args = if os::getenv("RUST_BENCH").is_some() {
|
2012-07-13 22:57:48 -07:00
|
|
|
~[~"", ~"20"]
|
2012-05-23 22:53:50 -07:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-13 22:57:48 -07:00
|
|
|
~[~"", ~"8"]
|
2011-07-27 14:19:39 +02:00
|
|
|
} else {
|
2012-05-23 22:53:50 -07:00
|
|
|
args
|
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
let opts = parse_opts(args.clone());
|
2011-06-29 12:29:02 -07:00
|
|
|
|
2012-05-23 22:53:50 -07:00
|
|
|
if opts.stress {
|
|
|
|
stress(2);
|
|
|
|
} else {
|
2013-08-04 01:59:24 +02:00
|
|
|
let max = uint::parse_bytes(args[1].as_bytes(), 10u).unwrap() as int;
|
2011-07-27 14:19:39 +02:00
|
|
|
|
2012-05-23 22:53:50 -07:00
|
|
|
let num_trials = 10;
|
2011-06-29 12:29:02 -07:00
|
|
|
|
2012-05-23 22:53:50 -07:00
|
|
|
let out = io::stdout();
|
2011-06-29 12:29:02 -07:00
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for n in range(1, max + 1) {
|
|
|
|
for _ in range(0, num_trials) {
|
2012-05-23 22:53:50 -07:00
|
|
|
let start = time::precise_time_ns();
|
|
|
|
let fibn = fib(n);
|
|
|
|
let stop = time::precise_time_ns();
|
2011-06-29 12:29:02 -07:00
|
|
|
|
2012-05-23 22:53:50 -07:00
|
|
|
let elapsed = stop - start;
|
2011-07-13 15:44:09 -07:00
|
|
|
|
2013-09-29 23:13:47 -07:00
|
|
|
out.write_line(format!("{}\t{}\t{}", n, fibn,
|
|
|
|
elapsed.to_str()));
|
2011-10-21 14:12:12 +02:00
|
|
|
}
|
2011-06-29 12:29:02 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-15 21:54:52 -07:00
|
|
|
}
|