%\pagebreak
\section{\kcode{scan} Directive}
\label{sec:scan}
\index{directives!scan@\kcode{scan}}
\index{scan directive@\kcode{scan} directive}
\index{reduction clause@\kcode{reduction} clause!inscan modifier@\kcode{inscan} modifier}
\index{inscan modifier@\kcode{inscan} modifier}

The following examples illustrate how to parallelize a loop that saves 
the \emph{prefix sum} of a reduction. This is accomplished by using 
the \kcode{inscan} modifier in the \kcode{reduction} clause for the input 
variable of the scan, and specifying with a \kcode{scan} directive whether 
the storage statement includes or excludes the scan input of the present 
iteration (\ucode{k}).

\index{scan directive@\kcode{scan} directive!inclusive clause@\kcode{inclusive} clause}
\index{scan directive@\kcode{scan} directive!exclusive clause@\kcode{exclusive} clause}
\index{clauses!inclusive@\kcode{inclusive}}
\index{inclusive clause@\kcode{inclusive} clause}
\index{clauses!exclusive@\kcode{exclusive}}
\index{exclusive clause@\kcode{exclusive} clause}
Basically, the \kcode{inscan} modifier connects a loop and/or SIMD reduction to 
the scan operation, and a \kcode{scan} construct with an \kcode{inclusive} or 
\kcode{exclusive} clause specifies whether the ``scan phase'' (lexical block 
before and after the directive, respectively) is to use an \plc{inclusive} or 
\plc{exclusive} scan value for the list item (\ucode{x}).

The first example uses the \plc{inclusive} scan operation on a composite
loop-SIMD construct. The \kcode{scan} directive separates the reduction 
statement on variable \ucode{x} from the use of \ucode{x} (saving to array \ucode{b}).
The order of the statements in this example indicates that
value \ucode{a[k]} (\ucode{a(k)} in Fortran) is included in the computation of 
the prefix sum \ucode{b[k]} (\ucode{b(k)} in Fortran) for iteration \ucode{k}.

\cexample[5.0]{scan}{1}

\ffreeexample[5.0]{scan}{1}

The second example uses the \plc{exclusive} scan operation on a composite
loop-SIMD construct. The \kcode{scan} directive separates the use of \ucode{x} 
(saving to array \ucode{b}) from the reduction statement on variable \ucode{x}.
The order of the statements in this example indicates that
value \ucode{a[k]} (\ucode{a(k)} in Fortran) is excluded from the computation 
of the prefix sum \ucode{b[k]} (\ucode{b(k)} in Fortran) for iteration \ucode{k}.

\cexample[5.0]{scan}{2}

\ffreeexample[5.0]{scan}{2}

In OpenMP 6.0, the \kcode{scan} directive was extended to support 
the concept of an \plc{initialization} phase where a private variable 
can be set for later use in the \plc{input} phase of
an \plc{exclusive} scan operation.
The following example is a rewrite of the previous exclusive scan
example, which uses the \kcode{scan init_complete} directive to separate
the initialization phase from the other phases of the scan operation.
The private variable \ucode{tmp} is set in the initialization phase
and used later in the input phase to update the prefix sum stored
in variable \ucode{x}.
This case allows the same array \ucode{c} to be used for
both input and output of the scan results.

\cexample[6.0]{scan}{3}

\ffreeexample[6.0]{scan}{3}
