c++vectorstlcilk-plus

Using Cilk array notation and STL vectors


I'm learning about parallel programming using the Cilk Plus extension to C++ (on gcc). One of the simplest refactors that I have read about is to use Cilk array notation, namely using it performing order-independent vector operations.

i.e.: c[:] = a[:] + b[:]

rather than: for (unsigned i = 0; i < a.size(); ++i) c[i] = a[i] + b[i];

I have a large code in place heavily using std::vector objects rather than arrays. Is it possible to use this concept with the std::vector?

Considering a, b, and c to be std::vector<double> of equal length, I have tried these with no success:

1.

c[:] = a[:] + b[:]

2.

double* aArr = &a[0];
double* bArr = &b[0];
double* cArr = &c[0];
cArr[:] = aArr[:] + bArr[:]

Both return

start-index and length fields necessary for using array notations in pointers or records

I understand that approach #2 makes a pointer to the vector without length information, but is there any workaround?

Edit:

An efficient solution is preferred. It may be possible, for example, to define an array and use std::copy to copy the values from the vector to the array, perform the addition using Cilk array notation, and then copy the values back to the vector. This is probably costlier than just doing the original element-wise addition.


Solution

  • This may not be the best/most-elegant answer (please post alternatives if there are), but it does seem to work to use the above approach #2 with the index start/length specifications. Like this:

    double* aArr = &a[0];
    double* bArr = &b[0];
    double* cArr = &c[0];
    cArr[0:c.size()] = aArr[0:c.size()] + bArr[0:c.size()]
    

    Of course, I need to be careful that the vector lengths match, but it compiles, and so far, seems to return the same answers.