summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorEric Holk <eholk@mozilla.com>2011-06-17 16:19:04 -0700
committerGraydon Hoare <graydon@mozilla.com>2011-06-27 09:58:39 -0700
commit91eadfd1ea1544513258fc30bf94ef384db2ad90 (patch)
tree26ce2e1ae07e155b6d0a57d9e69929efa987840f /src/test
parentbea28ea5376cba26713c4e49bc1d6fa2642ab419 (diff)
downloadrust-91eadfd1ea1544513258fc30bf94ef384db2ad90.tar.gz
rust-91eadfd1ea1544513258fc30bf94ef384db2ad90.zip
Added a parallel fibonacci program. It doesn't actually run in parallel yet, but it will give us something fun to test threading with.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/bench/shootout/pfib.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/test/bench/shootout/pfib.rs b/src/test/bench/shootout/pfib.rs
new file mode 100644
index 00000000000..60f2d3779c1
--- /dev/null
+++ b/src/test/bench/shootout/pfib.rs
@@ -0,0 +1,41 @@
+// -*- rust -*-
+
+/*
+  A parallel version of fibonacci numbers.
+*/
+
+fn recv[T](&port[T] p) -> T {
+  let T x;
+  p |> x;
+  ret x;
+}
+
+fn fib(int n) -> int {
+  fn pfib(chan[int] c, int n) {
+    if (n == 0) {
+      c <| 0;
+    }
+    else if (n <= 2) {
+      c <| 1;
+    }
+    else {
+      let port[int] p = port();
+      
+      auto t1 = spawn pfib(chan(p), n - 1);
+      auto t2 = spawn pfib(chan(p), n - 2);
+
+      c <| recv(p) + recv(p);
+    }
+  }
+
+  let port[int] p = port();
+  auto t = spawn pfib(chan(p), n);
+  ret recv(p);
+}
+
+fn main() {
+    assert (fib(8) == 21);
+    assert (fib(15) == 610);
+    log fib(8);
+    log fib(15);
+}