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

pipeline pulus parallel for

Xingjing_Lu
Beginner
595 Views

hi,

in my app, I want to parallelize it with pipeline and parallel for. Each stage characters serial(There are dependence between different items of the same stage), and in each stage, I want to process the data in parallel, now, I do it with parallel_for. But it seems does not work. 

My questions are: TBB does not support task parallelism in producer-consumer patter. So, how should I do it?

stage1 // serial

parallel_for

parallel_for

stage2 // serial

parallel_for

stage3 // serial

 

0 Kudos
1 Solution
Alexey-Kukanov
Employee
595 Views

Xingjing Lu wrote:

In the above example, I set the Number of threads to execute the pipeline like below:

pipeline.run(TheadNum); // ThreadNum = 3, the same number as the number of stages.

If I set up the task schedule for each parallel_for in different stages as:

task_scheduler_init init(4);

parallel_for( blocked_range<int>(0, 4), PASSIN);

what will happened to stages and parallel_for, are there 3 threads for each stage, and another 4 threads for each parallel_for? or there are only 3 threads totally?

It's a common mistake to think that the argument to pipeline.run() is the number of threads. It's not; it's maximal number of items that are allowed to flow through the pipeline at a time. And the motivation for this argument is not restricting concurrency but bounding resource usage (which could otherwise grow "over the top" when a serial filter is the bottleneck in the pipeline).

So if you instruct the task scheduler (via task_scheduler_init) to use 4 threads, that's what you will get (unless the scheduler was initialized earlier, possibly implicitly), no matter what token limit is set for pipeline.

View solution in original post

0 Kudos
6 Replies
RafSchietekat
Valued Contributor III
595 Views

I'm not sure I understand, but you should not have a problem calling parallel_for() from within a (parallel_)pipeline stage.

Are you saying that it works with a serial loop but not with a parallel loop, or is it something else?

0 Kudos
jimdempseyatthecove
Honored Contributor III
595 Views

Xingjing Lu wrote:

hi,

in my app, I want to parallelize it with pipeline and parallel for. Each stage characters serial(There are dependence between different items of the same stage), and in each stage, I want to process the data in parallel, now, I do it with parallel_for. But it seems does not work. 

My questions are: TBB does not support task parallelism in producer-consumer patter. So, how should I do it?

stage1 // serial

parallel_for

parallel_for

stage2 // serial

parallel_for

stage3 // serial

 

I think you want something like:

stage1 // serial
stage 2 // parallel_for
stage 3 // parallel_for
stage 4 // serial (was stage2)
stage 5 // parallel_for
stage 6 // serial (was stage3)

Jim Dempsey

0 Kudos
Xingjing_Lu
Beginner
595 Views

Raf Schietekat wrote:

I'm not sure I understand, but you should not have a problem calling parallel_for() from within a (parallel_)pipeline stage.

Are you saying that it works with a serial loop but not with a parallel loop, or is it something else?

Yes, I made some mistakes when calling parallel_for().

But I still have another problem:

In the above example, I set the Number of threads to execute the pipeline like below:

pipeline.run(TheadNum); // ThreadNum = 3, the same number as the number of stages.

If I set up the task schedule for each parallel_for in different stages as:

task_scheduler_init init(4);

 parallel_for( blocked_range<int>(0, 4), PASSIN);

what will happened to stages and parallel_for, are there 3 threads for each stage, and another 4 threads for each parallel_for? or there are only 3 threads totally?

Thanks!

0 Kudos
Xingjing_Lu
Beginner
595 Views

jimdempseyatthecove wrote:

Quote:

Xingjing Lu wrote:

hi,

in my app, I want to parallelize it with pipeline and parallel for. Each stage characters serial(There are dependence between different items of the same stage), and in each stage, I want to process the data in parallel, now, I do it with parallel_for. But it seems does not work. 

My questions are: TBB does not support task parallelism in producer-consumer patter. So, how should I do it?

stage1 // serial

parallel_for

parallel_for

stage2 // serial

parallel_for

stage3 // serial

 

 

I think you want something like:

stage1 // serial
stage 2 // parallel_for
stage 3 // parallel_for
stage 4 // serial (was stage2)
stage 5 // parallel_for
stage 6 // serial (was stage3)

Jim Dempsey

Yes, What I want to do is  similar what you said above, but I cannot do this directly, because there are order constrains between different tokens.

 

0 Kudos
RafSchietekat
Valued Contributor III
595 Views

Just to be sure, order constraints that are not addressed by serial_in_order?

(2014-08-09 Added after #7) Apparently I overlooked #4...

0 Kudos
Alexey-Kukanov
Employee
596 Views

Xingjing Lu wrote:

In the above example, I set the Number of threads to execute the pipeline like below:

pipeline.run(TheadNum); // ThreadNum = 3, the same number as the number of stages.

If I set up the task schedule for each parallel_for in different stages as:

task_scheduler_init init(4);

parallel_for( blocked_range<int>(0, 4), PASSIN);

what will happened to stages and parallel_for, are there 3 threads for each stage, and another 4 threads for each parallel_for? or there are only 3 threads totally?

It's a common mistake to think that the argument to pipeline.run() is the number of threads. It's not; it's maximal number of items that are allowed to flow through the pipeline at a time. And the motivation for this argument is not restricting concurrency but bounding resource usage (which could otherwise grow "over the top" when a serial filter is the bottleneck in the pipeline).

So if you instruct the task scheduler (via task_scheduler_init) to use 4 threads, that's what you will get (unless the scheduler was initialized earlier, possibly implicitly), no matter what token limit is set for pipeline.

0 Kudos
Reply