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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With