简体   繁体   中英

OpenMP task if or final

if I want no more tasks to be created if (array length < 100). Is if(r - l >= 100) or final(r - l < 100) satisfying this condition? (l = minIndex; r = maxIndex)

They both work. Relevant parts of the specification are:

undeferred task A task for which execution is not deferred with respect to its generating task region. That is, its generating task region is suspended until execution of the undeferred task is completed.

included task A task for which execution is sequentially included in the generating task region. That is, an included task is undeferred and executed immediately by the encountering thread.

final task A task that forces all of its child tasks to become final and included tasks.

[...]

When an if clause is present on a task construct, and the if clause expression evaluates to false, an undeferred task is generated, and the encountering thread must suspend the current task region, for which execution cannot be resumed until the generated task is completed. The use of a variable in an if clause expression of a task construct causes an implicit reference to the variable in all enclosing constructs.

When a final clause is present on a task construct and the final clause expression evaluates to true, the generated task will be a final task. All task constructs encountered during execution of a final task will generate final and included tasks. Note that the use of a variable in a final clause expression of a task construct causes an implicit reference to the variable in all enclosing constructs.

----- OpenMP Architecture Review Board. “OpenMP Application Programming Interface.” Specification Version 4.5, November 2015.

This means that if(false) and final(true) both execute task's content immediately. The only difference is if there is another task construct inside your task.

#pragma omp task if(0)
{
     // this task is created and executed normally
     #pragma omp task
     foo();
}
#pragma omp task final(1)
{
     // this task is "included", i.e. executed sequentially and immediately
     #pragma omp task
     foo();
}

From the wording, it also seems that if(false) will create a task and run it immediately, while final will simply run the code sequentially without creating a task. However I'm not sure that this is true, nor that there are performance implications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM