%\pagebreak
\section{\kcode{workshare} Construct}
\fortranspecificstart
\label{sec:workshare}
\index{constructs!workshare@\kcode{workshare}}
\index{workshare construct@\kcode{workshare} construct}

The following are examples of the \kcode{workshare} construct. 

In the following example, \kcode{workshare} spreads work across the threads executing 
the \kcode{parallel} region, and there is a barrier after the last statement. 
Implementations must enforce Fortran execution rules inside of the \kcode{workshare} 
block.

\fnexample{workshare}{1}

In the following example, the barrier at the end of the first \kcode{workshare} 
region is eliminated with a \kcode{nowait} clause. Threads doing \ucode{CC = 
DD} immediately begin work on \ucode{EE = FF} when they are done with \ucode{CC 
= DD}.

\pagebreak
\fnexample{workshare}{2}
\topmarker{Fortran}

The following example shows the use of an \kcode{atomic} directive inside a \kcode{workshare} 
construct. The computation of \ucode{SUM(AA)} is workshared, but the update to 
\ucode{R} is atomic.

\fnexample{workshare}{3}

Fortran \bcode{WHERE} and \bcode{FORALL} statements are \emph{compound statements}, 
made up of a \emph{control} part and a \emph{statement} part. When \kcode{workshare} 
is applied to one of these compound statements, both the control and the statement 
parts are workshared. The following example shows the use of a \bcode{WHERE} statement 
in a \kcode{workshare} construct.

Each task gets worked on in order by the threads:

\ucode{AA = BB} then
\\
\ucode{CC = DD} then
\\
\ucode{EE .ne. 0} then
\\
\ucode{FF = 1 / EE} then
\\
\ucode{GG = HH}

\fnexample{workshare}{4}
\topmarker{Fortran}

In the following example, an assignment to a shared scalar variable is performed 
by one thread in a \kcode{workshare} while all other threads in the team wait.

\fnexample{workshare}{5}

The following example contains an assignment to a private scalar variable, which 
is performed by one thread in a \kcode{workshare} while all other threads wait. 
It is non-conforming because the private scalar variable is undefined after the 
assignment statement. 

\fnexample{workshare}{6}

Fortran execution rules must be enforced inside a \kcode{workshare} construct. 
In the following example, the same result is produced in the following program 
fragment regardless of whether the code is executed sequentially or inside an OpenMP 
program with multiple threads:

\fnexample{workshare}{7}
\fortranspecificend


