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?

easythrees
  • 1,530
  • 3
  • 16
  • 43
  • shouldn't std::this_thread::get_id() just work? – Severin Pappadeux Dec 24 '19 at 03:15
  • I don't think so, at least per this: https://software.intel.com/en-us/node/506336 – easythrees Dec 24 '19 at 22:39
  • Well, that what I was reading" Replacement -> std::thread means "std::thread::get_id() should behave as expected from wherever it is called." (https://software.intel.com/en-us/forums/intel-threading-building-blocks/topic/392381) – Severin Pappadeux Dec 24 '19 at 23:15
  • Ah, I must’ve misread it, then. My apologies. – easythrees Dec 25 '19 at 03:50
  • 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 myself – Severin Pappadeux Dec 25 '19 at 05:34

2 Answers2

8

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.

Anton
  • 6,349
  • 1
  • 25
  • 53
  • 1
    This is the solution I ended up using, the "IDs" make much more sense. – easythrees Dec 24 '19 at 22:38
  • 1
    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 storage – user3208430 Oct 13 '21 at 16:57
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

easythrees
  • 1,530
  • 3
  • 16
  • 43