source:
doc/papers/concurrency/examples/Fib2.cfa@
9f5a71eb
| Last change on this file since 9f5a71eb was 600d7be, checked in by , 7 years ago | |
|---|---|
|
|
| File size: 1.2 KB | |
| Rev | Line | |
|---|---|---|
| [1e5d0f0c] | 1 | // |
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo | |
| 3 | // | |
| 4 | // The contents of this file are covered under the licence agreement in the | |
| 5 | // file "LICENCE" distributed with Cforall. | |
| 6 | // | |
| 7 | // fibonacci_1.cfa -- 1-state finite-state machine: precomputed first two states returning f(n - 1) | |
| 8 | // | |
| 9 | // Author : Peter A. Buhr | |
| 10 | // Created On : Thu Apr 26 23:20:08 2018 | |
| 11 | // Last Modified By : Peter A. Buhr | |
| [600d7be] | 12 | // Last Modified On : Sat May 18 08:55:59 2019 |
| 13 | // Update Count : 36 | |
| [1e5d0f0c] | 14 | // |
| 15 | ||
| 16 | #include <fstream.hfa> | |
| 17 | #include <coroutine.hfa> | |
| 18 | ||
| [600d7be] | 19 | coroutine Fibonacci { int fn1, fn; }; // used for communication |
| [1e5d0f0c] | 20 | |
| 21 | void main( Fibonacci & fib ) with( fib ) { // called on first resume | |
| [600d7be] | 22 | [fn1, fn] = [1, 0]; // precompute first two states |
| [1e5d0f0c] | 23 | for () { |
| 24 | suspend(); // restart last resume | |
| 25 | [fn1, fn] = [fn, fn1 + fn]; // general case | |
| 26 | } // for | |
| 27 | } | |
| 28 | ||
| 29 | int ?()( Fibonacci & fib ) with( fib ) { // function call operator | |
| [600d7be] | 30 | return resume( fib ).fn1; // restart last suspend |
| [1e5d0f0c] | 31 | } |
| 32 | ||
| 33 | int main() { | |
| 34 | Fibonacci f1, f2; | |
| 35 | for ( 10 ) { // print N Fibonacci values | |
| [600d7be] | 36 | sout | resume( f1 ).fn | resume( f2 ).fn; |
| [1e5d0f0c] | 37 | } // for |
| 38 | } | |
| 39 | ||
| 40 | // Local Variables: // | |
| 41 | // tab-width: 4 // | |
| [600d7be] | 42 | // compile-command: "cfa Fib2.cfa" // |
| [1e5d0f0c] | 43 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.