reduction-3.c 876 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <omp.h>
  2. #include <stdlib.h>
  3. int
  4. main (void)
  5. {
  6. int i = 0, j = 0, k = ~0, l;
  7. double d = 1.0;
  8. #pragma omp parallel num_threads(4)
  9. {
  10. #pragma omp single
  11. {
  12. i = 16;
  13. k ^= (1 << 16);
  14. d += 32.0;
  15. }
  16. #pragma omp for reduction(+:i) reduction(*:d) reduction(&:k) nowait
  17. for (l = 0; l < 4; l++)
  18. {
  19. if (omp_get_num_threads () == 4 && (i != 0 || d != 1.0 || k != ~0))
  20. #pragma omp atomic
  21. j |= 1;
  22. if (l == omp_get_thread_num ())
  23. {
  24. i = omp_get_thread_num ();
  25. d = i + 1;
  26. k = ~(1 << (2 * i));
  27. }
  28. }
  29. if (omp_get_num_threads () == 4)
  30. {
  31. #pragma omp barrier
  32. if (i != (16 + 0 + 1 + 2 + 3))
  33. #pragma omp atomic
  34. j |= 2;
  35. if (d != (33.0 * 1.0 * 2.0 * 3.0 * 4.0))
  36. #pragma omp atomic
  37. j |= 4;
  38. if (k != (~0 ^ 0x55 ^ (1 << 16)))
  39. #pragma omp atomic
  40. j |= 8;
  41. }
  42. }
  43. if (j)
  44. abort ();
  45. return 0;
  46. }