testsuite/libgomp.c/omp_orphan.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 * FILE: omp_orphan.c
00003 * DESCRIPTION:
00004 *   OpenMP Example - Parallel region with an orphaned directive - C/C++ Version
00005 *   This example demonstrates a dot product  being performed by an orphaned
00006 *   loop reduction construct.  Scoping of the reduction variable is critical.
00007 * AUTHOR: Blaise Barney  5/99
00008 * LAST REVISED: 04/06/05
00009 ******************************************************************************/
00010 #include <omp.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #define VECLEN 100
00014 
00015 float a[VECLEN], b[VECLEN], sum;
00016 
00017 float dotprod ()
00018 {
00019 int i,tid;
00020 
00021 tid = omp_get_thread_num();
00022 #pragma omp for reduction(+:sum)
00023   for (i=0; i < VECLEN; i++)
00024     {
00025     sum = sum + (a[i]*b[i]);
00026     printf("  tid= %d i=%d\n",tid,i);
00027     }
00028 
00029 return(sum);
00030 }
00031 
00032 
00033 int main (int argc, char *argv[]) 
00034 {
00035 int i;
00036 
00037 for (i=0; i < VECLEN; i++)
00038   a[i] = b[i] = 1.0 * i;
00039 sum = 0.0;
00040 
00041 #pragma omp parallel
00042   sum = dotprod();
00043 
00044 printf("Sum = %f\n",sum);
00045 
00046   return 0;
00047 }

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