reduction-2.c 849 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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)
  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. if (i != (16 + 0 + 1 + 2 + 3))
  32. #pragma omp atomic
  33. j |= 2;
  34. if (d != (33.0 * 1.0 * 2.0 * 3.0 * 4.0))
  35. #pragma omp atomic
  36. j |= 4;
  37. if (k != (~0 ^ 0x55 ^ (1 << 16)))
  38. #pragma omp atomic
  39. j |= 8;
  40. }
  41. }
  42. if (j)
  43. abort ();
  44. return 0;
  45. }