Why I'm getting "error expected an expression" while compile cilk program

1.1k views Asked by At

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..

2

There are 2 answers

1
Barry Tannenbaum - Intel On

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:

int fib(int n) {
    if (n < 2)
        return n;
    int x = cilk_spawn fib(n-1);
    int y = fib(n-2);
    cilk_sync;
    return x+y;
}

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.

1
Sayu Sekhar On

Not sure if you can use cilk_spawn with the += operator. Does the following code work?

        int rst = 0;
        int temp = 0;
        temp = cilk_spawn fib(n-1);
        rst += temp;
        temp = cilk_spawn fib(n-2);
        rst += temp;
        cilk_sync;
        return rst;