How to get value from variable inside class used by parallel_for

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?)

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

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;

Leave a Comment

Please sign in to add a comment. Not a member? Join today