Arrays in Cilk

Our first example demonstrates how to create and manipulate arrays in Cilk. Arrays in Cilk are similar to those in C, but with some additional features provided by the Cilk runtime.

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

int main() {
    // Here we create an array 'a' that will hold exactly 5 ints.
    // By default, an array is uninitialized, which means it may contain arbitrary values.
    int a[5];
    printf("emp: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");

    // We can set a value at an index using the array[index] = value syntax,
    // and get a value with array[index].
    a[4] = 100;
    printf("set: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    printf("get: %d\n", a[4]);

    // The sizeof operator can be used to determine the length of an array.
    printf("len: %lu\n", sizeof(a) / sizeof(a[0]));

    // Use this syntax to declare and initialize an array in one line.
    int b[] = {1, 2, 3, 4, 5};
    printf("dcl: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", b[i]);
    }
    printf("\n");

    // In Cilk, we don't have a direct equivalent to Go's [...] syntax,
    // but we can use compound literals to initialize arrays.
    int c[] = {100, 0, 0, 400, 500};
    printf("idx: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", c[i]);
    }
    printf("\n");

    // Array types are one-dimensional, but you can compose types to build multi-dimensional data structures.
    int twoD[2][3];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            twoD[i][j] = i + j;
        }
    }
    printf("2d: ");
    for (int i = 0; i < 2; i++) {
        printf("[");
        for (int j = 0; j < 3; j++) {
            printf("%d ", twoD[i][j]);
        }
        printf("] ");
    }
    printf("\n");

    // You can create and initialize multi-dimensional arrays at once too.
    int twoD2[2][3] = {{1, 2, 3}, {1, 2, 3}};
    printf("2d: ");
    for (int i = 0; i < 2; i++) {
        printf("[");
        for (int j = 0; j < 3; j++) {
            printf("%d ", twoD2[i][j]);
        }
        printf("] ");
    }
    printf("\n");

    return 0;
}

To compile and run this Cilk program, you would typically use:

$ cilk++ -O3 arrays.cilk -o arrays
$ ./arrays
emp: 0 0 0 0 0 
set: 0 0 0 0 100 
get: 100
len: 5
dcl: 1 2 3 4 5 
idx: 100 0 0 400 500 
2d: [0 1 2 ] [1 2 3 ] 
2d: [1 2 3 ] [1 2 3 ] 

Note that arrays in Cilk are similar to C arrays. They are fixed-size, and their size must be known at compile time. Cilk doesn’t provide built-in dynamic arrays, but you can use C++ std::vector if you need dynamic sizing.

Cilk arrays are zero-indexed, meaning the first element is at index 0. The size of an array can be determined using the sizeof operator, but remember to divide by the size of a single element to get the number of elements.

Cilk supports multi-dimensional arrays, which can be useful for parallel computations on matrices or other grid-like data structures.

When working with arrays in Cilk, keep in mind that Cilk’s parallel constructs (like cilk_for) can be used to process array elements in parallel, which can lead to significant performance improvements for large arrays.