I'm using Linux, Eclipse and Intel compiler V14.
I'm trying to compile the basic fibonacci example:
#include <iostream>
#include <cilk/cilk.h>
using namespace std;
int fib(int n) {
if (n < 2) return 1;
else {
int rst = 0;
rst += cilk_spawn fib(n-1);
rst += cilk_spawn fib(n-2);
cilk_sync;
return rst;
}
}
int main() {
int res = fib(9);
return 0;
}
And getting compile error:
error expected an expression
I tried to use:
cilk_spawn
Cilk_spawn
_cilk_spawn
_Cilk_spawn
but same error..
The Intel compiler automatically enables Cilk Plus. The message "symbol _Cilk_spawn could not be resolved" indicates that you're not using the Intel compiler. Check your compilation command and make sure that it's invoking icc instead of gcc.
Also, your original example contains a race. The correct implementation of the classic fibonacci in Cilk Plus is:
You need to use two independent variables to which are only summed after the sync to avoid the race.
You should also avoid the cilk_spawn on the second recursive call to fib(). Remember that the Cilk runtime implements stealing of the continuation. So what would be stolen if you used a second cilk_spawn is the code from the ; of the second fib() call until the sync. It will just slow your program down. MIT Cilk required that all calls to Cilk functions be spawned. Intel Cilk Plus removed that requirement.