Simple parallel adder. You pass it the pointer to the first element in the array, a pointer to the last element in the array, the element # of the first pointer, and the number of elements in this portion of the array being operated on.
double my_func (double *x, double *x_last, int first_pos, int n)
{
if (n ==1) {
return x[first_pos];
}
else if (n == 2) {
return x[first_pos] + x[first_pos+1];
}
else {
double x1,x2;
x1 = _Cilk_spawn my_func (&x[first_pos], &x[n/2+first_pos-1],first_pos, n/2);
x2 = my_func (&x[n/2+first_pos],&x[first_pos+n-1],n/2+first_pos, n-n/2);
_Cilk_sync;
return x1 + x2;
}
}
Let’s say we start with an array of size 80. This works perfectly for element #0-19 (the first quarter), then returns a bunch of 0’s/garbage for elements #20-39 and all elements after. Clearly both the x1 and x2 lines are working to some degree, but the function breaks down and I don’t know why. Any ideas?
You’re slicing the array wrong:
You can’t pass both an adjusted pointer to the slice’s first element, and that element’s global index. You end up indexing more than once, so to speak, leading to invalid addresses being dereferenced.
For the case of
n = 80, the second recursive call will passxpointing at element&x[80/2+0], i.e.&x[40], but it will also setfirst_posto40for that call. Eventually you’ll reach down ton < 3, and index using accumulatedfirst_pos.Also, the input data should be
constif it’s read-only, for clarity’s sake.