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

How to get value from variable inside class used by parallel_for

nuntawat
Beginner
252 Views
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-Kukanov
Employee
252 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;

0 Kudos
Reply