Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid local() calls for tbb enumerable_thread_specific variables

I have a code which uses tbb::enumerable_thread_specific variables, and in the deep place of the call stack the thread local variables are used. The naive implementation leads to a lot of local() function calls.

Now I want to avoid local() function calls by passing parameters hierarchically. Is there a simpler way of doing this? I have many places with local() function calls if I do not pass Foo as a parameter, but the code would be messy if I do. I have been looking for possible usage of an array with size equal to the number of threads, and use thread-id to access the thread local variable, but it seems tbb does not provide that (in contrast to omp_get_thread_num() in OpenMP).

See more descriptions here: https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/804043

like image 924
Ryan L Avatar asked Feb 11 '26 20:02

Ryan L


1 Answers

Repeating and expanding my own answer from the TBB forum:

You can use tbb::this_task_arena::max_concurrency() and tbb::this_task_arena::current_thread_index() to implement array-based custom thread local storage. The first function gives the upper limit for the number of working threads; to a degree it's TBB equivalent for omp_get_num_threads(). The second one gives an index of the current thread within the limit, similarly to omp_get_thread_num().

like image 170
Alexey Kukanov Avatar answered Feb 13 '26 09:02

Alexey Kukanov