The REDUCTION clause performs a reduction on the variables that appear in its list.
A private copy for each list variable is created for each thread. At the end of the reduction, the reduction variable is applied to all private copies of the shared variable, and the final result is written to the global shared variable.
REDUCTION (operator|intrinsic: list)
reduction (operator: list)
Iterations of the parallel loop will be distributed in equal sized blocks to each thread in the team (SCHEDULE STATIC)
At the end of the parallel loop construct, all threads will add their values of “result” to update the master thread’s global copy.
PROGRAM DOT_PRODUCT INTEGER N, CHUNKSIZE, CHUNK, I PARAMETER (N=100) PARAMETER (CHUNKSIZE=10) REAL A(N), B(N), RESULT ! Some initializations DO I = 1, N A(I) = I * 1.0 B(I) = I * 2.0 ENDDO RESULT= 0.0 CHUNK = CHUNKSIZE !$OMP PARALLEL DO !$OMP& DEFAULT(SHARED) PRIVATE(I) !$OMP& SCHEDULE(STATIC,CHUNK) !$OMP& REDUCTION(+:RESULT) DO I = 1, N RESULT = RESULT + (A(I) * B(I)) ENDDO !$OMP END PARALLEL DO PRINT *, 'Final Result= ', RESULT END
Variables in the list must be named scalar variables. They can not be array or structure type variables. They must also be declared SHARED in the enclosing context.
Reduction operations may not be associative for real numbers.
The REDUCTION clause is intended to be used on a region or work-sharing construct in which the reduction variable is used only in statements which have one of following forms:
Fortran | C / C++ |
---|---|
x = x operator expr x = expr operator x (except subtraction) x = intrinsic(x, expr) x = intrinsic(expr, x) | x = x op expr x = expr op x (except subtraction) x binop = expr x++ ++x x-- --x |
x is a scalar variable in the list expr is a scalar expression that does not reference x intrinsic is one of MAX, MIN, IAND, IOR, IEOR operator is one of +, *, -, .AND., .OR., .EQV., .NEQV. | x is a scalar variable in the list expr is a scalar expression that does not reference x op is not overloaded, and is one of +, *, -, /, &, ^, |, &&, || binop is not overloaded, and is one of +, *, -, /, &, ^, | |
Operated by the Lawrence Livermore National Security, LLC for the Department of Energy's National Nuclear Security Administration Learn about the Department of Energy's Vulnerability Disclosure Program