1

Hi I have recently started programming in openMP. I wanted to know is there any directive available to associate a particular thread to a particular every time it is relaunched.

Suppose, I have 4 cores and 4 threads for each core. Core 1 - Thread 1 Core 2 - Thread 2 Core 3 - Thread 3 Core 4 - Thread 4.

Now, what I want is that, every time thread 1 is scheduled/started it should start on core 1 itself. It like thread binding.

2 Answers2

2

There is no such pragma in the current OpenMP revision, but a standard way to specify bindings is coming in OpenMP 4.0. Until then, you can use the vendor-specific binding extensions like GNU's GOMP_CPU_AFFINITY or Intel's KMP_AFFINITY.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
0

You can create a wrapper which calls 4 functions namely F1() , F2() , F3() and F4().

Now using,

#pragma omp sections
{
    #pragma omp section 
    {
        F1()
    } 
    #pragma omp section 
    { 
        F2()
    }
    #pragma omp section 
    { 
        F3()
    }
    #pragma omp section 
    { 
        F4()
    }
}

Here F1() will always run on thread0, F2() on thread1 and so on.

Now do this in the terminal:

export GOMP_CPU_AFFINITY="0 1 2 3"

Now Thread0 is attached to Core0 , Thread1 to Core1 and so on.

Aizen
  • 561
  • 1
  • 9
  • 19
  • The statement that `F1` will always run on thread 0, `F2` on thread 1 and so is not true. The OpenMP standard says that the method of scheduling the sections among the threads in the team is implementation defined. If one wants to fix the scheduling, one has to implement a `switch` over the return value of `omp_get_thread_num()`. โ€“ Hristo Iliev Jan 31 '13 at 09:44
  • Check this: http://stackoverflow.com/questions/2770911/how-does-the-sections-directive-in-openmp-distribute-work?rq=1 โ€“ Aizen Jan 31 '13 at 10:41
  • 1
    The OpenMP standard (ยง2.5.2, p.49, ll.13-14) states: _The method of scheduling the structured blocks among the threads in the team is implementation defined._ And `libgomp` (whose affinity setting you cite) specifically is one of the best contra-examples to your statement as it assigns sections on first-come first-served basis. Intel's OpenMP on the other side uses round-robin assignment. โ€“ Hristo Iliev Jan 31 '13 at 11:14