Search code examples
openmp

Are OS threads tied to thread_num in different OpenMP parallel regions?


If I have multiple parallel regions, would same thread be used for given thread index (returned by omp_get_thread_num()) in those parallel regions?


Solution

  • It is undefined by the OpenMP standard (version 5.1), except for the primary thread (master thread) encountering the two sections (assuming they are encountered by the same thread in your application):

    The thread that encountered the parallel construct becomes the primary thread of the new team, with a thread number of zero for the duration of the new parallel region.

    Note that the this could not be true if you use untied tasks in your parallel section:

    The thread number may change during the execution of an untied task. The value returned by omp_get_thread_num is not generally useful during the execution of such a task region.