Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (92 sloc) 2.607 kB
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
1 // -*- rust -*-
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
2 // xfail-pretty
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
3
4 /*
5 A parallel version of fibonacci numbers.
6
7 This version is meant mostly as a way of stressing and benchmarking
8 the task system. It supports a lot of command-line arguments to
9 control how it runs.
10
11 */
12
13 use std;
14
f05a91a @brson Rename std::ivec to std::vec
brson authored
15 import std::vec;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
16 import std::uint;
17 import std::time;
ab6bb03 @brson Rename std::istr to std::str. Issue #855
brson authored
18 import std::str;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
19 import std::int::range;
7d05da9 @brson Rename std::ioivec to std::io
brson authored
20 import std::io;
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
21 import std::getopts;
22 import std::task;
79ca673 Adding more support for working with u64s.
Eric Holk authored
23 import std::u64;
aa0a51a Converted over benchmarks.
Eric Holk authored
24 import std::comm;
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
25 import std::comm::port;
26 import std::comm::chan;
aa0a51a Converted over benchmarks.
Eric Holk authored
27 import std::comm::send;
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
28 import std::comm::recv;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
29
df7f21d @marijnh Reformat for new syntax
marijnh authored
30 fn fib(n: int) -> int {
ebc61e3 @brson Remove temporary fn# syntax
brson authored
31 fn pfib(args: (chan<int>, int)) {
ac671c3 @brson Convert tests to use bare-fn spawn
brson authored
32 let (c, n) = args;
df7f21d @marijnh Reformat for new syntax
marijnh authored
33 if n == 0 {
aa0a51a Converted over benchmarks.
Eric Holk authored
34 send(c, 0);
518dc52 @brson Reformat
brson authored
35 } else if n <= 2 {
aa0a51a Converted over benchmarks.
Eric Holk authored
36 send(c, 1);
df7f21d @marijnh Reformat for new syntax
marijnh authored
37 } else {
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
38 let p = port();
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
39
686d6a4 @brson Drop the 2 from the spawn*2 functions
brson authored
40 let t1 = task::spawn((chan(p), n - 1), pfib);
41 let t2 = task::spawn((chan(p), n - 2), pfib);
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
42
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
43 send(c, recv(p) + recv(p));
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
44 }
45 }
46
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
47 let p = port();
686d6a4 @brson Drop the 2 from the spawn*2 functions
brson authored
48 let t = task::spawn((chan(p), n), pfib);
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
49 ret recv(p);
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
50 }
51
df7f21d @marijnh Reformat for new syntax
marijnh authored
52 type config = {stress: bool};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
53
fc6b7c8 @marijnh Reformat for new mode syntax, step 1
marijnh authored
54 fn parse_opts(argv: [str]) -> config {
5c49e4f @brson Reformat. Issue #855
brson authored
55 let opts = [getopts::optflag("stress")];
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
56
f05a91a @brson Rename std::ivec to std::vec
brson authored
57 let opt_args = vec::slice(argv, 1u, vec::len(argv));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
58
df7f21d @marijnh Reformat for new syntax
marijnh authored
59
5f71a20 @brson Remove vecs from std::getopts
brson authored
60 alt getopts::getopts(opt_args, opts) {
5c49e4f @brson Reformat. Issue #855
brson authored
61 getopts::success(m) { ret {stress: getopts::opt_present(m, "stress")} }
df7f21d @marijnh Reformat for new syntax
marijnh authored
62 getopts::failure(_) { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
63 }
64 }
65
ebc61e3 @brson Remove temporary fn# syntax
brson authored
66 fn stress_task(&&id: int) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
67 let i = 0;
68 while true {
69 let n = 15;
70 assert (fib(n) == fib(n));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
71 i += 1;
418d09e @brson Convert all uses of #ifmt to #fmt. Issue #855
brson authored
72 log_err #fmt["%d: Completed %d iterations", id, i];
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
73 }
74 }
75
df7f21d @marijnh Reformat for new syntax
marijnh authored
76 fn stress(num_tasks: int) {
aa0a51a Converted over benchmarks.
Eric Holk authored
77 let tasks = [];
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
78 range(0, num_tasks) {|i|
686d6a4 @brson Drop the 2 from the spawn*2 functions
brson authored
79 tasks += [task::spawn_joinable(copy i, stress_task)];
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
80 }
2f7c583 Cleaning up task and comm exports, updating all the test cases.
Eric Holk authored
81 for t in tasks { task::join(t); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
82 }
83
5c49e4f @brson Reformat. Issue #855
brson authored
84 fn main(argv: [str]) {
f05a91a @brson Rename std::ivec to std::vec
brson authored
85 if vec::len(argv) == 1u {
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
86 assert (fib(8) == 21);
87 log fib(8);
df7f21d @marijnh Reformat for new syntax
marijnh authored
88 } else {
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
89 // Interactive mode! Wooo!!!!
f32079f @brson Convert most main functions to the ivec signature
brson authored
90 let opts = parse_opts(argv);
df7f21d @marijnh Reformat for new syntax
marijnh authored
91
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
92
df7f21d @marijnh Reformat for new syntax
marijnh authored
93 if opts.stress {
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
94 stress(2);
df7f21d @marijnh Reformat for new syntax
marijnh authored
95 } else {
ab6bb03 @brson Rename std::istr to std::str. Issue #855
brson authored
96 let max = uint::parse_buf(str::bytes(argv[1]), 10u) as int;
df7f21d @marijnh Reformat for new syntax
marijnh authored
97
98 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
99
7d05da9 @brson Rename std::ioivec to std::io
brson authored
100 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
101
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
102 range(1, max + 1) {|n|
103 range(0, num_trials) {|i|
df7f21d @marijnh Reformat for new syntax
marijnh authored
104 let start = time::precise_time_ns();
105 let fibn = fib(n);
106 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
107
df7f21d @marijnh Reformat for new syntax
marijnh authored
108 let elapsed = stop - start;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
109
5c49e4f @brson Reformat. Issue #855
brson authored
110 out.write_line(#fmt["%d\t%d\t%s", n, fibn,
111 u64::str(elapsed)]);
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
112 }
113 }
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
114 }
115 }
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
116 }
Something went wrong with that request. Please try again.