2

I have code where I have to run parallel_for (independent from each other) at once parallely.

Code is something like:

tbb::parallel_for(range1,func1());//first tbb::parallel_for(range2,func2());//second tbb::parallel_for(range3,func3());//third 

I have tried using task_group. Is there any other method available?

5
  • 4
    What have you tried? How did your attempt work or fail? Can you perhaps show us a Minimal, Complete, and Verifiable Example of your attempt? And please read about how to ask good questions. Commented Aug 31, 2017 at 8:41
  • Why? If the three are large enough, each will saturate your cpus and only modest "rundown"/"runup" wasted cpu will occur if you do it in series, assuming you need all 3 finished to proceed. Commented Aug 31, 2017 at 11:32
  • and why do you want another approach if task_group works well? Commented Aug 31, 2017 at 15:34
  • @Anton The "runup" I am getting with task group is marginal. Also I wanted to know which other methods will be suitable for appication like this. Commented Sep 18, 2017 at 7:13
  • It's rather not because the choice of task_group. As Yakk mentioned above, check your problem size first or because nobody's remember to warm up threads before starting to measure Commented Sep 18, 2017 at 13:06

2 Answers 2

7

There are many ways to run any parallel algorithm in parallel, you want just run it inside another parallel algorithm of your choice. task_group is just one example. The simplest approach for your case is to use parallel_invoke:

tbb::parallel_invoke([]{ tbb::parallel_for(range1,func1);//first }, []{ tbb::parallel_for(range2,func2);//second }, []{ tbb::parallel_for(range3,func3);//third } ); 

but one can choose to use another parallel_for over array of ranges and function pointers, or use parallel_pipeline, parallel_for_each, or raw low-level tbb::task.

Sign up to request clarification or add additional context in comments.

2 Comments

You should remember that the scheduling algorithm for tasks in TBB may give you unexpected results. If you want all the tasks will progress at the same rate, you may be disappointed, because TBB uses a greedy (unfair) scheduling algorithm. Your tasks will all complete, and with TBB you won't oversubscribe the system, but they may still complete one-at-a-time.
You could also schedule multiple graph nodes each with a parallel algorithm.
1

You can put them each in a single std::thread and make a join afterwards. See also also.

3 Comments

that's bad approach since it is vulnerable to oversubscription because does not use tbb's worker threads
besides over-subscription, it introduces a lot of overhead for thread creation and joining.
This critic is true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.