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

Determine number of workers in currently active task scheduler

e4lam
Beginner
473 Views

I'm having trouble finding a way in TBB to get the current number of workers in the active task scheduler. For a contrived example:

void g() {

    // how can I query from TBB here to get the maximum allowed number of workers that was set in f()?

}

void f(int num_workers) {

   tbb::task_scheduler_init init(num_workers);

   g();

}

0 Kudos
4 Replies
Nikita_P_Intel
Employee
473 Views

Hi!

You can check it with this function:

tbb::this_task_arena::max_concurrency()

Thanks,

Nikita

0 Kudos
e4lam
Beginner
473 Views

Thanks, Nikita! However, it doesn't seem to behaving as I would expect/desire. It seems that something like this doesn't work:

tbb::task_scheduler_init init1;

std::cerr << tbb::this_task_arena::max_concurrency() << "\n"; // prints 12 (the default)

tbb::task_scheduler_init init2(6);

std::cerr << tbb::this_task_arena::max_concurrency() << "\n"; // still prints 12 not 6

 

0 Kudos
Nikita_P_Intel
Employee
473 Views

This is an expected and documented behavior: "To override the automatic defaults for task scheduling, a task_scheduler_init must become active before the first use of task scheduling services. "

So, the second call will have no effect.

From task_scheduler_init class comments also: "This class allows customizing properties of the TBB task pool to some extent. For example, it can limit concurrency level of parallel work initiated by the given thread. It also can be used to specify stack size of the TBB worker threads, though this setting is not effective if the thread pool has already been created."

Please note that task_scheduler_init is merely a reference to the scheduler, so the scheduler is created when the first reference appears and only destroyed when the last reference in the entire program goes away

Nikita

 

0 Kudos
e4lam
Beginner
473 Views

Thanks for the clarification! Perhaps it would be useful if a note was added to the task_scheduler_init documentation to use a task_arena if one wants to control the amount of concurrency. While the example there is valid, I would think that the recommendation is there is to actually use a task_arena instead.

0 Kudos
Reply