tiki
tiki

Reputation: 429

OpenMP parallel for region thread affinity

Let's say I have the following OpenMP region:

omp_set_num_threads(3);
#pragma omp parallel for
{
 //start
 ...
 //somewhere in the middle
 ...
 //end
}

Let's say I have 8-core system. For example, after "start" let's say thread 0 is running on core 4, thread 1 runs on core 5, and thread 2 runs on core 6. Is it possible "somewhere in the middle" before the "end" that threads migrate from their respective cores that they were assigned after "start"? I.e. is it possible that threads 0-2 are assigned to cores 4-5 after "start", and "somewhere in the middle" threads say migrate to cores 5-7, respectively? And will even be possible that threads may reside just before the "end" on cores 0-2? Thanks.

Upvotes: 2

Views: 3147

Answers (1)

Massimiliano
Massimiliano

Reputation: 8032

To the best of my knowledge, OpenMP 3.1 specifications do not provide any means to re-bind threads.

In fact, the only way to have some control over thread binding is through the OMP_PROC_BIND environment variable:

The OMP_PROC_BIND environment variable sets the value of the global bind-var ICV. The value of this environment variable must be true or false. If the environment variable is set to true, the execution environment should not move OpenMP threads between processors. If the environment variable is set to false, the execution environment may move OpenMP threads between processors. The behavior of the program is implementation defined if the value of OMP_PROC_BIND is neither true nor false.

The OpenMP 4.0 draft extends the possible values of OMP_PROC_BIND and adds the OMP_PLACES environment variable, which permits to select how threads are bound to resources. Still, there is no standard way of re-binding the threads.

If this behavior is for you absolutely necessary, you may think of using the hwloc library, in particular the CPU binding part.

Upvotes: 3

Related Questions