Sei sulla pagina 1di 1

Effective use of Reduction: Loops that reduce a collection of values to a single value are fairly common.

Consider the following simple loop that calculates the sum of the return value of the integer-type function call func(k) with the loop index value as input data.
sum = 0; for ( k = 0; k < 100; k++ ){ sum = sum + func(k); // func has no side-effects}

Instead of Providing Synchronisation use Reduction:


sum = 0; #pragma omp parallel for reduction(+ sum) for (k = 0; k < 100; k++) { sum = sum + func(k); !

Given the reduction clause, the compiler creates private copies of the variable sum for each thread, and when the loop completes, it adds the values together and places the result in the original variable. For each variable specified in a reduction clause, a private copy is created, one for each thread, as if the pri"ate clause is used. he private copy is then initiali!ed to the initiali!ation value for the operator."t the end of the region or the loop for which the reduction clause was specified, the original reduction variable is updated by combining its original value with the final value from each thread.

#hile identifying the opportunities to explore the use of the reduction clause for threading, you should $eep the following three points in mind.
he value of the original reduction variable becomes undefined when the first thread reaches the region or loop that specifies the reduction clause and remains so until the reduction computation is completed. %f the reduction clause is used on a loop to which the no#ait is also applied, the value of original reduction variable remains undefined until a barrier synchroni!ation is performed to ensure that all threads have completed the reduction. he order in which the values are combined is unspecified. herefore, comparing se&uential and parallel runs, even between two parallel runs, does not guarantee that bit-identical results will be obtained or that side effects, such as floating-point exceptions,will be identical.

Department CSE, SCAD CET

Potrebbero piacerti anche