Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't "omp parallel" and "omp single" cancel each other out? (OpenMP)

On my quest to learn OpenMP I've come across an example with a main that looks like this:

int main(){
    #pragma omp parallel
        #pragma omp single
        some_function(1,2);
return 0;
}

Correct me if I'm wrong but from my understanding parallel creates threads and single only lets one thread through.

So don't these two cancel each other out? What's the point of creating threads if you're only going to be using one?

some_function recursively calls itself inside a #pragma omp task if that makes any difference.

like image 669
John Slaine Avatar asked Aug 31 '25 01:08

John Slaine


1 Answers

The team of threads created by omp parallel still exist during the omp single, they just don't participate in the execution and wait for work. The omp task then makes all the difference (which is why it is so important always to include a complete example in your question)! At that point a task is created which will (most likely) be executed by one of those threads waiting for work. This is how tasks in OpenMP are usually used. Any thread in the team can create tasks and the runtime will assign them to threads in the team (including possibly the one that spawned it).

like image 143
Zulan Avatar answered Sep 02 '25 15:09

Zulan