omp_workshare4.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /******************************************************************************
  2. * OpenMP Example - Combined Parallel Loop Work-sharing - C/C++ Version
  3. * FILE: omp_workshare4.c
  4. * DESCRIPTION:
  5. * This is a corrected version of the omp_workshare3.c example. Corrections
  6. * include removing all statements between the parallel for construct and
  7. * the actual for loop, and introducing logic to preserve the ability to
  8. * query a thread's id and print it from inside the for loop.
  9. * SOURCE: Blaise Barney 5/99
  10. * LAST REVISED: 03/03/2002
  11. ******************************************************************************/
  12. #include <omp.h>
  13. #include <stdio.h>
  14. #define N 50
  15. #define CHUNKSIZE 5
  16. int
  17. main () {
  18. int i, chunk, tid;
  19. float a[N], b[N], c[N];
  20. char first_time;
  21. /* Some initializations */
  22. for (i=0; i < N; i++)
  23. a[i] = b[i] = i * 1.0;
  24. chunk = CHUNKSIZE;
  25. first_time = 'y';
  26. #pragma omp parallel for \
  27. shared(a,b,c,chunk) \
  28. private(i,tid) \
  29. schedule(static,chunk) \
  30. firstprivate(first_time)
  31. for (i=0; i < N; i++)
  32. {
  33. if (first_time == 'y')
  34. {
  35. tid = omp_get_thread_num();
  36. first_time = 'n';
  37. }
  38. c[i] = a[i] + b[i];
  39. printf("tid= %d i= %d c[i]= %f\n", tid, i, c[i]);
  40. }
  41. return 0;
  42. }