Static splitting tbb :: parallel_for

I have a dataset that I would like to use with tbb::parallel_for with intervals of size interval_size . Each interval that my functor consumes must be interval_size , with the exception of the last partial interval, which may be less when interval_size does not evenly share my dataset.

Is there a way to use TBB for static separation this way? This test produces fewer interval_size less than interval_size on my system:

 #include <tbb/parallel_for.h> #include <iostream> struct body { void operator()(const tbb::blocked_range<size_t> &r) const { std::cout << "range size: " << r.end() - r.begin() << std::endl; } }; int main() { size_t num_intervals = 4; size_t interval_size = 3; // consume num_intervals plus a partial interval in total size_t n = num_intervals * interval_size + (interval_size - 1); tbb::parallel_for(tbb::blocked_range<size_t>(0, n, interval_size), body(), tbb::simple_partitioner()); return 0; } 

Output:

 $ g++ test_parallel_for.cpp -ltbb $ ./a.out range size: 3 range size: 2 range size: 2 range size: 3 range size: 2 range size: 2 
+4
source share
1 answer

The reason for the behavior is that a simple separator divides your range according to the following criteria:

ceil (grainsize / 2) <= chunksize <= grainsize

when used with tbb::blocked_range(i, j, grainsize) and chunksize is the size of your range.

You can check out the Tutorial for more information in section 3.2.5 of the Section Summary.

There is no easy way to get a fixed chunksize size with TBB (you can easily achieve this with OpenMP). This is because it contradicts the concepts of TBB. TBB is trying to distract all of these things from you, and the scheduler ensures that your threads are used as best as possible at runtime.

+4
source

Source: https://habr.com/ru/post/1434311/