Search code examples
c++parallel-processingopenmpcpu-cores

Executing a particular function on specified core using openMP


Is it possible to fix one function to run on a particular core using OpenMP?

For example: I have two functions namely: Foo1 and Foo2

And my computer (Linux OS) has two cores: core0 and core1.

So, How can I run application Foo1 always on core0 and Foo2 always on core1 simultaneously?

I am using C++ for coding.


Solution

  • Thanks to @Novelocrat and @Joachim for their useful comments:

    I did it like this:

    int main() {
    
    #pragma omp parallel num_threads(2)
    {
        #pragma omp sections
        {
            #pragma omp section
            {
                foo1();
            }
            #pragma omp section
            {
                foo2();
            }
        }
    }
    
    return 0; }
    

    Now foo1() runs always on thread 1 and foo2() runs always on thread 2.

    At run time set Environment Variable:GOMP_CPU_AFFINITY="0 1"

    which binds thread 1 to core0 and thread 2 to core1.