Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ed_b_1
Beginner
163 Views

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

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
Black Belt
163 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?

ed_b_1
Beginner
163 Views

Thanks, worked like a charm. Put it at the top of main and declared tbb::task_scheduler_init object.
163 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
Reply