mirror of
https://github.com/OpenMP/Examples.git
synced 2025-04-22 22:01:23 +01:00
48 lines
2.2 KiB
TeX
48 lines
2.2 KiB
TeX
\pagebreak
|
|
\section{\code{taskloop} Construct}
|
|
\label{sec:taskloop}
|
|
\index{constructs!taskloop@\code{taskloop}}
|
|
\index{taskloop construct@\code{taskloop} construct}
|
|
\index{taskloop construct@\code{taskloop} construct!grainsize clause@\code{grainsize} clause}
|
|
\index{taskloop construct@\code{taskloop} construct!nogroup clause@\code{nogroup} clause}
|
|
\index{clauses!grainsize@\code{grainsize}}
|
|
\index{grainsize clause@\code{grainsize} clause}
|
|
\index{clauses!nogroup@\code{nogroup}}
|
|
\index{nogroup clause@\code{nogroup} clause}
|
|
|
|
The following example illustrates how to execute a long running task concurrently with tasks created
|
|
with a \code{taskloop} directive for a loop having unbalanced amounts of work for its iterations.
|
|
|
|
The \code{grainsize} clause specifies that each task is to execute at least 500 iterations of the loop.
|
|
|
|
The \code{nogroup} clause removes the implicit taskgroup of the \code{taskloop} construct; the explicit \code{taskgroup} construct in the example ensures that the function is not exited before the long-running task and the loops have finished execution.
|
|
|
|
\cexample[4.5]{taskloop}{1}
|
|
|
|
\ffreeexample[4.5]{taskloop}{1}
|
|
|
|
%\clearpage
|
|
|
|
Because a \code{taskloop} construct encloses a loop, it is often incorrectly
|
|
perceived as a worksharing construct (when it is directly nested in
|
|
a \code{parallel} region).
|
|
|
|
While a worksharing construct distributes the loop iterations across all threads in a team,
|
|
the entire loop of a \code{taskloop} construct is executed by every thread of the team.
|
|
|
|
In the example below the first taskloop occurs closely nested within
|
|
a \code{parallel} region and the entire loop is executed by each of the \plc{T} threads;
|
|
hence the reduction sum is executed \plc{T}*\plc{N} times.
|
|
|
|
The loop of the second taskloop is within a \code{single} region and is executed
|
|
by a single thread so that only \plc{N} reduction sums occur. (The other
|
|
\plc{N}-1 threads of the \code{parallel} region will participate in executing the
|
|
tasks. This is the common use case for the \code{taskloop} construct.)
|
|
|
|
In the example, the code thus prints \code{x1 = 16384} (\plc{T}*\plc{N}) and
|
|
\code{x2 = 1024} (\plc{N}).
|
|
|
|
\cexample[4.5]{taskloop}{2}
|
|
|
|
\ffreeexample[4.5]{taskloop}{2}
|