Hello World in Cilk

Our first program will print the classic “hello world” message. Here’s the full source code.

#include <cilk/cilk.h>
#include <stdio.h>

void hello() {
    printf("hello world\n");
}

int main() {
    cilk_spawn hello();
    cilk_sync;
    return 0;
}

To run the program, save the code in hello-world.c and use a compatible Cilk compiler like gcc with Cilkplus support to compile and run it.

$ gcc -fcilkplus hello-world.c -o hello-world
$ ./hello-world
hello world

In some environments, you might need to link against Cilk libraries explicitly. For example:

$ gcc -fcilkplus hello-world.c -o hello-world -lcilkrts
$ ./hello-world
hello world

Sometimes we’ll want to optimize our programs for parallel execution. Cilk provides parallel constructs such as cilk_spawn and cilk_sync to facilitate this.

Now that we can run and build basic Cilk programs, let’s learn more about the language.