| 1 | <h2>Cilk keywords</h2> |
| 2 | <pre><code>cilk_scope {} |
| 3 | cilk_spawn f(); |
| 4 | cilk_spawn {} |
| 5 | cilk_sync; |
| 6 | cilk_for () {} |
| 7 | int cilk_reducer() x;</code></pre> |
| 8 | |
| 9 | <h2>Full example</h2> |
| 10 | <pre><code>#include <cilk/cilk.h> |
| 11 | |
| 12 | int fib_cilk_scope(int n) { |
| 13 | if (n < 2) |
| 14 | return; |
| 15 | int x, y; |
| 16 | cilk_scope { |
| 17 | x = cilk_spawn fib(n-1); |
| 18 | y = fib(n-2); |
| 19 | } |
| 20 | return x + y; |
| 21 | |
| 22 | int fib_cilk_sync(int n) { |
| 23 | if (n < 2) |
| 24 | return; |
| 25 | int x = cilk_spawn fib(n-1); |
| 26 | int y = fib(n-2); |
| 27 | cilk_sync; |
| 28 | return x + y; |
| 29 | } |
| 30 | |
| 31 | void zero(void *v) { |
| 32 | *(int *)v = 0; |
| 33 | } |
| 34 | void plus(void *l, void *r) { |
| 35 | *(int *)l += *(int *)r; |
| 36 | } |
| 37 | int array_sum_cilk_for_reducer(int *A, int n) { |
| 38 | int cilk_reducer(zero, plus) sum = 0; |
| 39 | cilk_for (int i = 0; i < n; ++i) |
| 40 | sum += A[i]; |
| 41 | return sum; |
| 42 | }</code></pre> |