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

current_thread_index()?

erling_andersen
New Contributor I
2,763 Views

Hi,

 

I coming from Cilk that had a function to obtain the worker id (something like thread id).  I minimized the use of that but for some stuff it was handy. So I am looking for something similarly in TBB.

 

What I found  is

 

https://spec.oneapi.io/versions/latest/elements/oneTBB/source/task_scheduler/task_arena/this_task_arena_ns.html?highlight=this_task_arena

 

But what I am left with a message is that you should NOT use

 

current_thread_index()

 

since the current_thread_index() may refer to to another arena that I am thinking about and there so many ifs and buts. In general it seems very hard for me to reason about this feature.

So it is correct current_thread_index is an obscure feature that it is better not to use.

 

Erling

 

 

 

 

0 Kudos
1 Solution
Mark_L_Intel
Moderator
2,408 Views

TBB abstracts out threads/OS/hardware so the developers work with TBB tasks instead. So, the current_thread_index() is not supposed to be often used in a sense that thread IDs normally not needed if we use TBB main model of working with tasks. However, for some workloads, for example, typical HPC workload which is regular (i.e., for loops) instead of irregular workloads TBB was designed for, TBB team published examples that assign thread affinities and use current_thread_index() together with other APIs for that.    

 

I suggest to download a free copy of proTBB book from https://www.apress.com/gp/book/9781484243978. From page 360 there:

 

"

Figure 13-1 shows a simple example of how to use a task_scheduler_observer object to pin threads to cores on Linux. In this example, we use the sched_setaffinity function to set the CPU mask for each thread as it joins the default arena. In Chapter 20, we show an example that assigns affinity using the hwloc software package. In the example in Figure 13-1, we use tbb::this_task_arena::max_concurrency() to find the number of slots in the arena and tbb::this_task_arena::current_thread_index() to find the slot that the calling thread is assigned to. Since we know there will be the same number of slots in the default arena as the number of logical cores, we pin each thread to the logical core that matches its slot number.

 

proTBB Fig 13.1.PNG

 

 

View solution in original post

0 Kudos
9 Replies
erling_andersen
New Contributor I
2,764 Views

Btw I also read the ProTBB book to get wiser.

0 Kudos
NoorjahanSk_Intel
Moderator
2,730 Views

Hi,

Thanks for reaching out to us.

Are you facing any issues while using current_thread_index(), if yes could you please elaborate more on your issue so that it will help us to get some more insights.

 

Thanks & Regards

Noorjahan.

 

 

0 Kudos
NoorjahanSk_Intel
Moderator
2,676 Views

Hi,

 Reminder: Could you please confirm whether your issue is resolved. If not please elaborate more on your issue.


Thanks & Regards

Noorjahan.


0 Kudos
erling_andersen
New Contributor I
2,665 Views

My point is that your documentation is so unclear that it is very hard to conclude anything definite about how

 

current_thread_index()

 

works. Your replies just confirm that. So in that sense my question has been answered.

I will avoid using it.

 

 

 

0 Kudos
NoorjahanSk_Intel
Moderator
2,640 Views

Hi,

 

We apologize for asking whether your issue is resolved.

However, we would like to make our documentation better and user-friendly. We would like to know which part of the documentation you found confusing/ difficult to interpret.

>> So it is correct current_thread_index is an obscure feature that it is better not to use.

Since you mentioned that current_thread_index() shouldn't be used, could you please elaborate more on it which helps us to understand more about the problem. Also, please let us know why you concluded that the current_thread_index() shouldn't be used?

 If possible could you please explain the scenario where you would like to use this current_thread_index() so that we can help you better.

 

Thanks & Regards

Noorjahan

 

 

0 Kudos
erling_andersen
New Contributor I
2,609 Views

I read

 

https://spec.oneapi.io/versions/latest/elements/oneTBB/source/task_scheduler/task_arena/this_task_arena_ns.html?highlight=this_task_arena

 

and  I do not quite understand the ramifications of the notes.  Maybe it is just me who find this hard to grasp completely.

 

In any case I have found a way to not use this feature. 

0 Kudos
NoorjahanSk_Intel
Moderator
2,462 Views

Hi,

We are working on it. we will get back to you soon.


Thanks & Regards

Noorjahan


0 Kudos
Mark_L_Intel
Moderator
2,409 Views

TBB abstracts out threads/OS/hardware so the developers work with TBB tasks instead. So, the current_thread_index() is not supposed to be often used in a sense that thread IDs normally not needed if we use TBB main model of working with tasks. However, for some workloads, for example, typical HPC workload which is regular (i.e., for loops) instead of irregular workloads TBB was designed for, TBB team published examples that assign thread affinities and use current_thread_index() together with other APIs for that.    

 

I suggest to download a free copy of proTBB book from https://www.apress.com/gp/book/9781484243978. From page 360 there:

 

"

Figure 13-1 shows a simple example of how to use a task_scheduler_observer object to pin threads to cores on Linux. In this example, we use the sched_setaffinity function to set the CPU mask for each thread as it joins the default arena. In Chapter 20, we show an example that assigns affinity using the hwloc software package. In the example in Figure 13-1, we use tbb::this_task_arena::max_concurrency() to find the number of slots in the arena and tbb::this_task_arena::current_thread_index() to find the slot that the calling thread is assigned to. Since we know there will be the same number of slots in the default arena as the number of logical cores, we pin each thread to the logical core that matches its slot number.

 

proTBB Fig 13.1.PNG

 

 

0 Kudos
erling_andersen
New Contributor I
2,373 Views

So you confirm:

 

So it is correct current_thread_index is an obscure feature that it is better not to use.

 

that I wrote initially. Thanks.

0 Kudos
Reply