omp_orphan.c 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. float a[VECLEN], b[VECLEN], sum;
  15. float dotprod ()
  16. {
  17. int i,tid;
  18. tid = omp_get_thread_num();
  19. #pragma omp for reduction(+:sum)
  20. for (i=0; i < VECLEN; i++)
  21. {
  22. sum = sum + (a[i]*b[i]);
  23. printf(" tid= %d i=%d\n",tid,i);
  24. }
  25. return(sum);
  26. }
  27. int main (int argc, char *argv[])
  28. {
  29. int i;
  30. for (i=0; i < VECLEN; i++)
  31. a[i] = b[i] = 1.0 * i;
  32. sum = 0.0;
  33. #pragma omp parallel
  34. sum = dotprod();
  35. printf("Sum = %f\n",sum);
  36. return 0;
  37. }