Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

parallel_reduce : Exception in summing

pranav_vaidya
Beginner
559 Views

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;

}

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.

0 Kudos
2 Replies
Alexey-Kukanov
Employee
559 Views

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.

0 Kudos
pranav_vaidya
Beginner
559 Views
Yup, you are right. That solved my problem.
That was indeed very silly of me.

Pranav.
0 Kudos
Reply