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

capping the number of threads used by tbb::flow::graph

ed_b_1
Beginner
575 Views
Is it possible to impose a maximum on the number of threads spun up by tbb as the graph is generated and edges are made? Right now, tbb creates a number of threads equal to task_scheduler_init::default_num_threads(), pretty much regardless of the complexity of my graph. Setting max_threads via tbb::task_scheduler_init() appears to have no effect on the number of threads started.
0 Kudos
3 Replies
RafSchietekat
Valued Contributor III
575 Views

Are you certain that you are setting tbb::task_scheduler_init before doing anything that might invoke the scheduler?

And that you are in fact putting the object on the stack for the duration of your application thread, rather than using it as a function, which would merely construct and immediately destroy a local object, with no lasting effect?

0 Kudos
ed_b_1
Beginner
575 Views
Thanks, worked like a charm. Put it at the top of main and declared tbb::task_scheduler_init object.
0 Kudos
Vladimir_P_1234567890
575 Views

I suppose you can use task_arena class to limit number of thread for a particular graph. More details can be found in our article http://goparallel.sourceforge.net/wp-content/uploads/2014/07/PUM18_Threading_Building_Blocks.pdf or in the reference manual.

example:

#include <tbb/task_arena.h>
    ...
    // limit arena by 4 workers
    tbb::task_arena my_arena(4);
    ...
    tbb::flow::graph g;
    ... // create the graph here
    my_arena.enqueue([&]{
        ... // start graph computations
    });
    ... // do something else
    my_arena.execute([&]{
        g.wait_for_all();
    }); // does not return until the flow graph finishes
0 Kudos
Reply