Community
cancel
Showing results for 
Search instead for 
Did you mean: 
nuntawat
Beginner
36 Views

How to get value from variable inside class used by parallel_for

I would like to return the result inside parallel_for operation. How do I do?(For example, if I have variable type int named a that is variable of class C used by parallel_for. How do I get value of a?)
0 Kudos
1 Reply
Alexey_K_Intel3
Employee
36 Views

Quoting - nuntawat
I would like to return the result inside parallel_for operation. How do I do?(For example, if I have variable type int named a that is variable of class C used by parallel_for. How do I get value of a?)


Do you mean that you try to accumulate some result in the body class you use with parallel_for, and then want it back?
That simply won't work well with parallel_for, because this algorithm copies body objects all the time, so any attempt to accumulate something in a non-static member variable would just fail. If you need the total result back, use parallel_reduce, which body is supposed to have the method join()that merges with the data accumulated by in another body object. To get the accumulated value after parallel_reduce completes, instantiate the body object as a non-temporary:

MyBodyClass accumulator;
tbb::parallel_reduce( blocked_range(0,N), accumulator );
cout << accumulator.result << endl;

Reply