Search code examples
cmultithreadingperformanceparallel-processingopenmp

Does calling a function in parallel enough to have it execute in parallel?


Let's say I have a piece of code such as the following (of course, that is not meant to be executed and only a sample to try and understand the concept as my question is a general knowledge question)

int main()
{
#pragma omp parallel 
myfunction();
return 1;
}

myfunction(){
//do some work
}

I know that means that my function will be called multiple times (depending on the default amount of threads for my computer), however, will these multiple times be executed in parallel, or I should do something like:

int main()
{
#pragma omp parallel 
myfunction();
return 1;
}

#pragma omp parallel
myfunction(){
//do some work
}

Solution

  • To execute a function in parallel do the first:

    int main()
    {
       #pragma omp parallel 
       myfunction();
       return 1;
    }
    
    myfunction(){
    //do some work
    }
    

    The OpenMP pragmas are added to a block of code, which can include also a method call. They are not added to the method signature.