shared-2.c 695 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdio.h>
  2. #include <omp.h>
  3. extern void abort (void);
  4. void
  5. parallel (int a, int b)
  6. {
  7. int bad, LASTPRIV, LASTPRIV_SEC;
  8. int i;
  9. a = b = 3;
  10. bad = 0;
  11. #pragma omp parallel firstprivate (a,b) shared (bad) num_threads (5)
  12. {
  13. if (a != 3 || b != 3)
  14. bad = 1;
  15. #pragma omp for lastprivate (LASTPRIV)
  16. for (i = 0; i < 10; i++)
  17. LASTPRIV = i;
  18. #pragma omp sections lastprivate (LASTPRIV_SEC)
  19. {
  20. #pragma omp section
  21. { LASTPRIV_SEC = 3; }
  22. #pragma omp section
  23. { LASTPRIV_SEC = 42; }
  24. }
  25. }
  26. if (LASTPRIV != 9)
  27. abort ();
  28. if (LASTPRIV_SEC != 42)
  29. abort ();
  30. if (bad)
  31. abort ();
  32. }
  33. int main()
  34. {
  35. parallel (1, 2);
  36. return 0;
  37. }