The following variant of the first code has the same critical path as the first code:
void recursive_task(int level)
{
if (level == 0){
usleep(1000);
return;
}
else
{
recursive_task(level-1);
#pragma omp task
{
recursive_task(level-1);
}
#pragma omp task if(0)
{
recursive_task(level-1);
}
#pragma omp taskwait
recursive_task(level-1);
}
}
Due to the taskwait following the two tasks, execution time will not improve, if a thread different from the encountering thread would execute the second task. Using if(0)
encourages the OpenMP runtime to execute this task immediately rather than possibly scheduling other tasks first. Using if(0)
rather than dropping the task construct completely ensures the scoping as described in @JérômeRichard's answer.