testsuite/libgomp.c/omp_reduction.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 * FILE: omp_reduction.c
00003 * DESCRIPTION:
00004 *   OpenMP Example - Combined Parallel Loop Reduction - C/C++ Version
00005 *   This example demonstrates a sum reduction within a combined parallel loop
00006 *   construct.  Notice that default data element scoping is assumed - there
00007 *   are no clauses specifying shared or private variables.  OpenMP will 
00008 *   automatically make loop index variables private within team threads, and
00009 *   global variables shared.
00010 * AUTHOR: Blaise Barney  5/99
00011 * LAST REVISED: 04/06/05
00012 ******************************************************************************/
00013 #include <omp.h>
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 
00017 int main (int argc, char *argv[]) {
00018 
00019 int   i, n;
00020 float a[100], b[100], sum; 
00021 
00022 /* Some initializations */
00023 n = 100;
00024 for (i=0; i < n; i++)
00025   a[i] = b[i] = i * 1.0;
00026 sum = 0.0;
00027 
00028 #pragma omp parallel for reduction(+:sum)
00029   for (i=0; i < n; i++)
00030     sum = sum + (a[i] * b[i]);
00031 
00032 printf("   Sum = %f\n",sum);
00033 
00034   return 0;
00035 }

Generated on Fri Apr 5 05:38:10 2013 for Libgomp by  doxygen 1.4.7