1 /****************************************************************************** 2 * FILE: omp_orphan.c 3 * DESCRIPTION: 4 * OpenMP Example - Parallel region with an orphaned directive - C/C++ Version 5 * This example demonstrates a dot product being performed by an orphaned 6 * loop reduction construct. Scoping of the reduction variable is critical. 7 * AUTHOR: Blaise Barney 5/99 8 * LAST REVISED: 04/06/05 9 ******************************************************************************/ 10 #include <omp.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #define VECLEN 100 14 15 float a[VECLEN], b[VECLEN], sum; 16 17 float dotprod () 18 { 19 int i,tid; 20 21 tid = omp_get_thread_num(); 22 #pragma omp for reduction(+:sum) 23 for (i=0; i < VECLEN; i++) 24 { 25 sum = sum + (a[i]*b[i]); 26 printf(" tid= %d i=%d\n",tid,i); 27 } 28 29 return(sum); 30 } 31 32 33 int main (int argc, char *argv[]) 34 { 35 int i; 36 37 for (i=0; i < VECLEN; i++) 38 a[i] = b[i] = 1.0 * i; 39 sum = 0.0; 40 41 #pragma omp parallel 42 sum = dotprod(); 43 44 printf("Sum = %f\n",sum); 45 46 return 0; 47 } 48