Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
Intel Customer Support will be observing the Martin Luther King holiday on Monday, Jan. 17, and will return on Tues. Jan. 18.
For the latest information on Intel’s response to the Log4j/Log4Shell vulnerability, please see Intel-SA-00646

current_thread_index()?

erling_andersen
New Contributor I
1,056 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_ar...

 

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
Employee
701 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

9 Replies
erling_andersen
New Contributor I
1,057 Views

Btw I also read the ProTBB book to get wiser.

NoorjahanSk_Intel
Moderator
1,023 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.

 

 

NoorjahanSk_Intel
Moderator
969 Views

Hi,

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


Thanks & Regards

Noorjahan.


erling_andersen
New Contributor I
958 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.

 

 

 

NoorjahanSk_Intel
Moderator
933 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

 

 

erling_andersen
New Contributor I
902 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. 

NoorjahanSk_Intel
Moderator
755 Views

Hi,

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


Thanks & Regards

Noorjahan


Mark_L_Intel
Employee
702 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

erling_andersen
New Contributor I
666 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.

Reply