- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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//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_rangeparallel_reduce(blocked_range<

long int>(0,n,n/8),parsum); return parsum.get_sum();}

Am I missing something here ?

Thanks in Advance,

Pranav.

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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*arr* is unnecessary.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

That was indeed very silly of me.

Pranav.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page