问题
The OpenMP specification document says that "When a thread encounters a task construct, it may
choose to execute the task immediately or defer its execution until a later time". However, the specification does not say how this choice is made.
How does the thread decide when to execute the task immediately and when to defer the execution? If this is implementation specific, how does compilers like gcc solve this?
回答1:
There are two conditions coming from the standard:
When an
if
clause is present and evaluates tofalse
, then the new task is a undeferred task which is executed right now by the current thread (the old task is suspended).A
final
clause makes the new task and all it's child tasks final and included. An included task is undeferred and executed immediately by the encountering thread.
The above is loosely copied from the standard
GCC (in the current master) also executes tasks immediately (GOMP_TASK_UNDEFERRED
) if
- There is no active thread team, i.e. a task is being constructed outside of a parallel region.
- The task count of the current thread team is larger than 64 * the number of threads in the team.
See also: task.c, look for GOMP_task
.
The LLVM OpenMP runtime uses a task deque that has with a maximum size, and executes tasks immediately if this deque is full. The default size is 256 (1 << 8
), but there seem to be conditions under which it doubles. I am not quite sure how exactly the task queue is organized within a team.
See also: kmp_tasking.cpp, look for TASK_NOT_PUSHED
.
来源:https://stackoverflow.com/questions/46601207/openmp-how-does-the-thread-decide-when-to-defer-a-task-and-when-to-execute-imm