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

Changing number of threads in TBB

akhal
Beginner
2,209 Views
Hi

How can one control number of threads? For example, if I specify through scheduler some number of threads, e.g.,

tbb::task_scheduler_init TBBinit(nthreads);

and then I want to change available number of threads in middle of the program, how do I do that?
0 Kudos
1 Solution
Kirill_R_Intel
Employee
2,209 Views
Yes, your code for changing worker threads number is ok. There is no analog of omp_get_num_threads() in TBB, so you can't know what exactly number of threads actively work at the moment. In OpenMP number of threadsavailablefor parallel region is determined before entering to the region and is not changed during processing of the region. So calling omp_get_num_threads() inside the region can easily provide you this previosly defined value.

In TBB work load is balanced dynamically between worker threads every time.Number of worker threads processing a parallel construction always changesdepending on workload. Threads stealtasks from each otherdynamically.So there is no reason to request scheduler about currently active threads - this value can be changed next moment.

Regards,
Kirill

View solution in original post

0 Kudos
4 Replies
Kirill_R_Intel
Employee
2,209 Views

Akhal,

You can change number of threads by removing old task_scheduler_init object and creating new one after this with new number of threads. If number of threads is reduced, those threads that should be stopped will complete their current job and than exit.

Regards,
Kirill

0 Kudos
akhal
Beginner
2,209 Views
I did like,

task_scheduler_init TBBinit(nrthreads);

Then when I want to reduce or increase threads; I put these lines,

TBBinit.terminate();
TBBinit.initialize(new_nrthreads);

Is this okay? also how can I querry about current number of threads if I want to now at any time like in OpenMP, we use omp_get_num_threads(), whats TBB alternative for this function?
0 Kudos
Kirill_R_Intel
Employee
2,210 Views
Yes, your code for changing worker threads number is ok. There is no analog of omp_get_num_threads() in TBB, so you can't know what exactly number of threads actively work at the moment. In OpenMP number of threadsavailablefor parallel region is determined before entering to the region and is not changed during processing of the region. So calling omp_get_num_threads() inside the region can easily provide you this previosly defined value.

In TBB work load is balanced dynamically between worker threads every time.Number of worker threads processing a parallel construction always changesdepending on workload. Threads stealtasks from each otherdynamically.So there is no reason to request scheduler about currently active threads - this value can be changed next moment.

Regards,
Kirill
0 Kudos
akhal
Beginner
2,209 Views
Oh thank you so much. Your information is so helpful.
0 Kudos
Reply