arraystbb

Does parallel_for_each (tbb) process each element of array in a different thread?


From the documentation of oneapi:

The sequence form parallel_for_each(first, last, body) applies a function object body over a sequence [first,last). Items may be processed in parallel.

What does this mean exactly? I'm studying TBB for one of my classes and it says that the iteration is done serially. Is this basically, for example, I have an array

int* myarray = a really big array

and for each element in my array I want to do

myarray[i] = some function that takes a really long time to execute

and I have 4 CPU cores that are available.

will parallel_for_each basically take 4 threads and each thread will process one element? like

array[0] = result from thread 1
array[1] = result from thread 2
array[2] = result from thread 3
array[3] = result from thread 4

and so on?

Thanks!


Solution

  • parallel_for_each processes work items in parallel.It creates a thread for each single element of your array. Refer to below link for more details: https://oneapi-src.github.io/oneTBB/main/tbb_userguide/Cook_Until_Done_parallel_do.html

    Use parallel_for_each when each objects work is relatively large and number of objects is few and each object has un-equal work.