parallel_reduce : Exception in summing

parallel_reduce : Exception in summing

Hello,

I am a relative newbie to Threading Building blocks and I have been trying to implement some code whose both versions I understand logically but I am not sure why one is throwing an exception while the other one is not.

I was using parallel_reduce to implement the summing of all elements of the a float array. Well. Here is the code for the parallel summer class.

class ParallelSummer

{

public:

ParallelSummer(float* arr) {this->arr = arr;sum = 0;}

ParallelSummer(ParallelSummer& parsum, split) {sum=0;}

void join (ParallelSummer& parsum){

sum+=parsum.sum;

}

long double get_sum(){return sum;}

void operator()(const blocked_range<float*>& range){

long double local_sum = sum;

float* end = (float*)range.end();

for(float* i=range.begin();i!=end;++i){

local_sum+=*i;

}

sum = local_sum;

}

private:

float* arr;

long double sum;

};

long double ParSum(float* arr, size_t n)

{

ParallelSummer parsum(arr);

parallel_reduce(blocked_range(arr,arr+n,n/8),parsum);

//parallel_reduce(blocked_range<long int>(0,n,n/8),parsum);

return parsum.get_sum();

}

Well this code works fine and I get speedups of about 2-3X and sometimes higher. What I dont understand is why the following sample does not work and gives me an exception.

void operator()(const blocked_range<long int>& range){

long double local_sum = 0;

long int end = (long int)range.end();

for(long int i=range.begin();i!=end;++i){

local_sum+=arr[i];

}

sum = local_sum;

}

long double ParSum(float* arr, size_t n)

{

ParallelSummer parsum(arr);

//parallel_reduce(blocked_range(arr,arr+n,n/8),parsum);

parallel_reduce(blocked_range<long int>(0,n,n/8),parsum);

return parsum.get_sum();

}

Am I missing something here ?

Thanks in Advance,

Pranav.

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In your splitting constructor, you did not initialize arr. And later in operator() you dereference a pointer that contains some garbage, thus getting an exception.

In the blocked_range case, proper pointers to iterate over are passed via range objects so arr is unnecessary.

Yup, you are right. That solved my problem.
That was indeed very silly of me.

Pranav.

Login to leave a comment.