TBB: Possible to get Thread IDs?
Asked Answered
C

2

6

I have a very simple parallel_for loop

    tbb::parallel_for(tbb::blocked_range<int>(0, values.size()),
    [&](tbb::blocked_range<int> r)
    {
        for (int i = r.begin(); i < r.end(); ++i)
        {
            values[i] = std::sin(i * 0.001);
        }
    });

Where 'values' is a vector of doubles. What I'd like to know is which threads work on which range in the loop. Is it possible to get a thread ID of some kind from TBB?

Cephalopod answered 24/12, 2019 at 2:51 Comment(5)
shouldn't std::this_thread::get_id() just work?Cruzcruzado
I don't think so, at least per this: software.intel.com/en-us/node/506336Cephalopod
Well, that what I was reading" Replacement -> std::thread means "std::thread::get_id() should behave as expected from wherever it is called." (software.intel.com/en-us/forums/intel-threading-building-blocks/…)Cruzcruzado
Ah, I must’ve misread it, then. My apologies.Cephalopod
If you're dealing with quite fresh TBB installation you could try to call it and see how it is going. If it works, just post it as your own answer, I would happily endorse it, it is useful to know that it is working solution. I don't have anything with TBB right now, couldn't test myselfCruzcruzado
D
10

Also, if you want to know a relative number of worker thread in current task_arena, which goes from 0 to the arena concurrency level, use this:

int worker_index = tbb::task_arena::current_thread_index();

The range of index values can be contiguous if all the threads will get to work at the same time.

Dissymmetry answered 24/12, 2019 at 15:42 Comment(2)
This is the solution I ended up using, the "IDs" make much more sense.Cephalopod
This works well, also tbb::this_task_arena::max_concurrency() is useful in combination with current_thread_index to get the maximum number of threads that may run to setup per-thread storagePhyllous
C
3

Looks like the solution is to use

tbb::this_tbb_thread::get_id()

in tbb_thread.h. See this for more details:

https://software.intel.com/en-us/node/506336

Cephalopod answered 24/12, 2019 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.