The REDUCTION clause asserts that the named variables are updated in the INDEPENDENT loop by a series of operations that are associative and commutative. Furthermore, the intermediate values of the REDUCTION variables are not used within the loop (except, of course, in the updates themselves). Thus, the value of a REDUCTION variable after the loop may be computed as the result of a reduction tree.
The semantics of reductions are discussed in detail in Section 5.1.4. This section defines correct syntax.
Any variable whose name occurs as a reduction-variable is said to be protected while the immediately following DO loop is active (i.e. being executed). It may not be referenced while the loop in which it is protected is active, with one exception. It may occur in special locations in assignment statements of a special form, and these statements must be in the range (i.e. the lexical body) of the loop. In particular, it may not occur in any HPF directive, including the variable list in a NEW clause. This includes any NEW clause in the same INDEPENDENT directive.
A reduction statement is an assignment statement of the following special form that occurs in the range of an independent DO loop for which the name of its reduction variable occurs in a reduction clause. This description is not part of the grammar of HPF; rather, it serves to define the restricted assignment statements in which reduction variables are allowed.
H505 reduction-stmt is variable = variable mult-op mult-operand
or variable = add-operand * variable
or variable = variable add-op add-operand
or variable = level-2-expr + variable
or variable = variable and-op and-operand
or variable = and-operand and-op variable
or variable = variable or-op or-operand
or variable = or-operand or-op variable
or variable = variable equiv-op equiv-operand
or variable = equiv-operand equiv-op variable
or variable = reduction-function ( variable , expr )
or variable = reduction-function ( expr , variable )
H506 reduction-function is MAX
The first two assertions of Section 5.1 account for the fact that the occurrences of reduction variables in their allowed positions in reduction statements do not cause interference between iterations of an INDEPENDENT loop. Any other assignment to or reference to a reduction variable does interfere with the reduction statement; this includes occurrences in subprograms and in the expr part of a reduction statement.
A variable that is updated by reduction statements in an independent loop must be protected by explicit appearance in a reduction clause. This clause must appear in the INDEPENDENT directive for the outermost independent loop that
!HPF$ INDEPENDENT, NEW(J), REDUCTION(X) DO I = 1, 10 !HPF$ INDEPENDENT DO J = 1, 20 X = X + J END DO END DO
It would be incorrect to move the reduction clause to the inner INDEPENDENT directive. Since X is updated by reduction operations (twenty times) for each iteration of the outer loop, it does not have a well-defined value until the completion of the outer loop. (End of advice to users.)
The reduction-variable reference may be an array element or array section. The two references that occur in a reduction statement must be lexically identical. The Fortran rules of operator precedence and the use of parentheses in the expression must ensure that the reduction operator is the top-level operator (i.e. it is evaluated last) on the right-hand side. Therefore,
X = X * A + 1
is not a correctly formed reduction statement.
Note that the syntax of the INDEPENDENT directive does not allow an array element or array section to be designated as a reduction variable in the reduction clause. Even though such a subobject may occur in a reduction statement, it is the entire array or character variable that is treated as a reduction variable.
The allowed reduction operators and functions are all associative (in their mathematical definitions, even though the usual implementations of the arithmetic operators by Fortran language processors and the underlying hardware are not).
In most cases, only one operator will be used in the reduction statements (if there are more than one) that update a given reduction variable. It is sensible, however, to use + and - together on the same reduction variable: mathematically, subtraction is just addition of the additive inverse. For example:
!HPF$ INDEPENDENT, REDUCTION(X) DO I = 1, 100 X(IDX(I,1)) = X(IDX(I,1)) + Y(I) X(IDX(I,2)) = X(IDX(I,2)) - Y(I) END DO
The same is true for multiplication (*) and division (/). No other mixing of operators is allowed.
X = I + X
is permitted as a reduction statement, for most purposes
X = X + I
is stylistically cleaner. (End of advice to users.)