1*404b540aSrobert /****************************************************************************** 2*404b540aSrobert * FILE: omp_reduction.c 3*404b540aSrobert * DESCRIPTION: 4*404b540aSrobert * OpenMP Example - Combined Parallel Loop Reduction - C/C++ Version 5*404b540aSrobert * This example demonstrates a sum reduction within a combined parallel loop 6*404b540aSrobert * construct. Notice that default data element scoping is assumed - there 7*404b540aSrobert * are no clauses specifying shared or private variables. OpenMP will 8*404b540aSrobert * automatically make loop index variables private within team threads, and 9*404b540aSrobert * global variables shared. 10*404b540aSrobert * AUTHOR: Blaise Barney 5/99 11*404b540aSrobert * LAST REVISED: 04/06/05 12*404b540aSrobert ******************************************************************************/ 13*404b540aSrobert #include <omp.h> 14*404b540aSrobert #include <stdio.h> 15*404b540aSrobert #include <stdlib.h> 16*404b540aSrobert main(int argc,char * argv[])17*404b540aSrobertint main (int argc, char *argv[]) { 18*404b540aSrobert 19*404b540aSrobert int i, n; 20*404b540aSrobert float a[100], b[100], sum; 21*404b540aSrobert 22*404b540aSrobert /* Some initializations */ 23*404b540aSrobert n = 100; 24*404b540aSrobert for (i=0; i < n; i++) 25*404b540aSrobert a[i] = b[i] = i * 1.0; 26*404b540aSrobert sum = 0.0; 27*404b540aSrobert 28*404b540aSrobert #pragma omp parallel for reduction(+:sum) 29*404b540aSrobert for (i=0; i < n; i++) 30*404b540aSrobert sum = sum + (a[i] * b[i]); 31*404b540aSrobert 32*404b540aSrobert printf(" Sum = %f\n",sum); 33*404b540aSrobert 34*404b540aSrobert return 0; 35*404b540aSrobert } 36