Kaelan Mikowicz
Kaelan Mikowicz

Reputation: 355

TBB Parallel Pipeline seems to run in-order?

I am working on a data processing pipeline with some OpenCV code, after implementing my pipeline I found no speedup, also no slowdown. I am trying to investigate why this is so.

I came up with the following example:

int start = 0;
tbb::parallel_pipeline(16,
    tbb::make_filter<void, int>(tbb::filter::serial_out_of_order, [&](tbb::flow_control& fc){
        if(start < 1000) {
            return start++;
        }
        fc.stop();
        return start;
    }) &
    tbb::make_filter<int, int>(tbb::filter::parallel, [](int num){
        std::cout << num << std::endl;
        return num + 1;
    }) &
    tbb::make_filter<int, void>(tbb::filter::parallel, [](int num){
    })
);

When this code executes, 1-1000 is printed sequentially. Is this correct behavior? Or do I have an issue with my environment?

Upvotes: 0

Views: 755

Answers (1)

Alexey Kukanov
Alexey Kukanov

Reputation: 12784

Reordering is rather unlikely to be seen at the start of the second filter in practice.

The parallel_pipeline works in such a way that the same thread puts a given item through the pipeline for as long as possible (in your pipeline, all filters after the first are parallel, so the same thread will execute all three filters for an item). The overhead for a thread to move an item to the next filter is much less than what another thread needs to steal a task for the next item, process the first filter, and then also move to the second one. Reordering is still possible if e.g. the first thread is preempted by OS, but rather unlikely.

For better chances to observe out-of-order execution, move your print statements to the third filter and add some random amount of "work" to the second one, so that the time for it to process an item varies.

Upvotes: 2

Related Questions