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

Question about task affinity issue

irisshinra308
Beginner
381 Views
Hi~
I am trying to added a structure to group the tasks according to their affinity_id.
However, the allocate_task() in the task.cpp set all tasks' affinity_id to zero.
I am wondering where all tasks' affinity_id has been set and then I could maintain the affinity information with my structure.

Another question is, why the get_task() in the task.cpp does not search the task with the same affinity_id first?
I am really confused about how the TBB task scheduler taking care of the task affinity issue.
Maybe there is some document or other resources could help me understanding how the mechanism works?
Any help would be grateful!

Thanks

Dennis
0 Kudos
4 Replies
Alexey-Kukanov
Employee
381 Views
By default, tasks has no affinity in TBB. After the task is allocated, and before it is spawned, one can use set_affinity() to specify desired affinity_id. Each worker thread has some affinity_id associated with it. When the task with non-zero affinity_id is spawned, a notification is "mailed" to the thread with the same id, so that it could take this task when its own pool is empty. The affinity is "soft" however, which means that the task is not guaranteed to execute on the thread with givenaffinity_id; it also can be taken for execution by the thread that created it, or it can be stolen by a third thread, whatever happens first. There are some additional heuristics used in the scheduler to increase chances for affinitized execution, e.g. affinitized tasks could be omitted during stealing.

get_task() does not take affinity_id into account, it just takes a task from the pool of the current thread. So locally spawned tasks take priority over affinitized tasks, which take priority over stealing a task from another thread.
0 Kudos
irisshinra308
Beginner
381 Views
Thanks for replying!

I have an additional question here.
I have read the spawn() in the task.cpp.
In this function, it use the affinity_id to identify the corresponding mailbox.
Does this means that the affinity_id is something like the thread id?
However, I ran the seismic example provided in TBB package and find out that the affinity_id is ranged from 0 to 4 on my dual core laptop.
How does the affinity_id maps to the exact thread context?

Any help would be grateful!

Dennis
0 Kudos
Alexey-Kukanov
Employee
381 Views
While currently there is 1-1 mapping between affinity_id and mailboxes, in future it might change. E.g. we may adjust affinity rules for multicore processors with shared L2 cache.

Think of affinity_id as the hint you provide to the scheduler that some task shares data with an earlier executed task. You get the affinity_id via note_affinity, record it, and provide as the hint later. E.g. affinity_partitioner for parallel loops in TBB basically works this way.
0 Kudos
ARCH_R_Intel
Employee
381 Views
http://www.threadingbuildingblocks.org/ver.php?fid=106has a PowerPoint presentation on the affinity mechanism. As Alexey remarked, it is only a hint (not a command) for executing a task where some other task with a similar id executed before.

0 Kudos
Reply