Example 2

In the following program, the pragma omp for directive has a reduction clause, reduction(+:sum), right before the parallel for loop. This tells OpenMP to generate code that uses the + operator to add together the value of sum at the end of the for loop. Otherwise the program is identical to the previous version. // File: example2.cpp // #include <stdio.h> #include <omp.h> int main() { int reps = 10000 ; long int A[reps] ; long int sum = 0 ; for (long int i=0; i < reps ; i++) { A[i] = i ; } #pragma omp parallel shared(A,sum,reps) { #pragma omp single { // only one thread has to do this omp_set_num_threads(4); printf("Number of threads = %d\n", omp_get_num_threads() ) ; } #pragma omp for schedule(static,5) reduction(+:sum) for (long int i=0; i < reps; i++) { sum += A[i] ; } } // end of parallel region printf("sum = %ld\n", sum) ; return 0 ; }

Download: example2.cpp

Download this program. Then, compile and run it a few times.

What happened? Do you get the same answer for each run?