reduction-3.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* C / C++'s logical AND and OR operators take any scalar argument
  2. which compares (un)equal to 0 - the result 1 or 0 and of type int.
  3. In this testcase, the int result is again converted to a floating-poing
  4. or complex type.
  5. While having a floating-point/complex array element with || and && can make
  6. sense, having a non-integer/non-bool reduction variable is odd but valid.
  7. Test: integer reduction variable + FP array. */
  8. #define N 1024
  9. _Complex float rcf[N];
  10. _Complex double rcd[N];
  11. float rf[N];
  12. double rd[N];
  13. int
  14. reduction_or ()
  15. {
  16. char orf = 0;
  17. short ord = 0;
  18. int orfc = 0;
  19. long ordc = 0;
  20. #pragma omp parallel reduction(||: orf)
  21. for (int i=0; i < N; ++i)
  22. orf = orf || rf[i];
  23. #pragma omp parallel for reduction(||: ord)
  24. for (int i=0; i < N; ++i)
  25. ord = ord || rcd[i];
  26. #pragma omp parallel for simd reduction(||: orfc)
  27. for (int i=0; i < N; ++i)
  28. orfc = orfc || rcf[i];
  29. #pragma omp parallel loop reduction(||: ordc)
  30. for (int i=0; i < N; ++i)
  31. ordc = ordc || rcd[i];
  32. return orf + ord + __real__ orfc + __real__ ordc;
  33. }
  34. int
  35. reduction_or_teams ()
  36. {
  37. char orf = 0;
  38. short ord = 0;
  39. int orfc = 0;
  40. long ordc = 0;
  41. #pragma omp teams distribute parallel for reduction(||: orf)
  42. for (int i=0; i < N; ++i)
  43. orf = orf || rf[i];
  44. #pragma omp teams distribute parallel for simd reduction(||: ord)
  45. for (int i=0; i < N; ++i)
  46. ord = ord || rcd[i];
  47. #pragma omp teams distribute parallel for reduction(||: orfc)
  48. for (int i=0; i < N; ++i)
  49. orfc = orfc || rcf[i];
  50. #pragma omp teams distribute parallel for simd reduction(||: ordc)
  51. for (int i=0; i < N; ++i)
  52. ordc = ordc || rcd[i];
  53. return orf + ord + __real__ orfc + __real__ ordc;
  54. }
  55. int
  56. reduction_and ()
  57. {
  58. unsigned char andf = 1;
  59. unsigned short andd = 1;
  60. unsigned int andfc = 1;
  61. unsigned long anddc = 1;
  62. #pragma omp parallel reduction(&&: andf)
  63. for (int i=0; i < N; ++i)
  64. andf = andf && rf[i];
  65. #pragma omp parallel for reduction(&&: andd)
  66. for (int i=0; i < N; ++i)
  67. andd = andd && rcd[i];
  68. #pragma omp parallel for simd reduction(&&: andfc)
  69. for (int i=0; i < N; ++i)
  70. andfc = andfc && rcf[i];
  71. #pragma omp parallel loop reduction(&&: anddc)
  72. for (int i=0; i < N; ++i)
  73. anddc = anddc && rcd[i];
  74. return andf + andd + __real__ andfc + __real__ anddc;
  75. }
  76. int
  77. reduction_and_teams ()
  78. {
  79. unsigned char andf = 1;
  80. unsigned short andd = 1;
  81. unsigned int andfc = 1;
  82. unsigned long anddc = 1;
  83. #pragma omp teams distribute parallel for reduction(&&: andf)
  84. for (int i=0; i < N; ++i)
  85. andf = andf && rf[i];
  86. #pragma omp teams distribute parallel for simd reduction(&&: andd)
  87. for (int i=0; i < N; ++i)
  88. andd = andd && rcd[i];
  89. #pragma omp teams distribute parallel for reduction(&&: andfc)
  90. for (int i=0; i < N; ++i)
  91. andfc = andfc && rcf[i];
  92. #pragma omp teams distribute parallel for simd reduction(&&: anddc)
  93. for (int i=0; i < N; ++i)
  94. anddc = anddc && rcd[i];
  95. return andf + andd + __real__ andfc + __real__ anddc;
  96. }
  97. int
  98. main ()
  99. {
  100. for (int i = 0; i < N; ++i)
  101. {
  102. rf[i] = 0;
  103. rd[i] = 0;
  104. rcf[i] = 0;
  105. rcd[i] = 0;
  106. }
  107. if (reduction_or () != 0)
  108. __builtin_abort ();
  109. if (reduction_or_teams () != 0)
  110. __builtin_abort ();
  111. if (reduction_and () != 0)
  112. __builtin_abort ();
  113. if (reduction_and_teams () != 0)
  114. __builtin_abort ();
  115. rf[10] = 1.0;
  116. rd[15] = 1.0;
  117. rcf[10] = 1.0;
  118. rcd[15] = 1.0i;
  119. if (reduction_or () != 4)
  120. __builtin_abort ();
  121. if (reduction_or_teams () != 4)
  122. __builtin_abort ();
  123. if (reduction_and () != 0)
  124. __builtin_abort ();
  125. if (reduction_and_teams () != 0)
  126. __builtin_abort ();
  127. for (int i = 0; i < N; ++i)
  128. {
  129. rf[i] = 1;
  130. rd[i] = 1;
  131. rcf[i] = 1;
  132. rcd[i] = 1;
  133. }
  134. if (reduction_or () != 4)
  135. __builtin_abort ();
  136. if (reduction_or_teams () != 4)
  137. __builtin_abort ();
  138. if (reduction_and () != 4)
  139. __builtin_abort ();
  140. if (reduction_and_teams () != 4)
  141. __builtin_abort ();
  142. rf[10] = 0.0;
  143. rd[15] = 0.0;
  144. rcf[10] = 0.0;
  145. rcd[15] = 0.0;
  146. if (reduction_or () != 4)
  147. __builtin_abort ();
  148. if (reduction_or_teams () != 4)
  149. __builtin_abort ();
  150. if (reduction_and () != 0)
  151. __builtin_abort ();
  152. if (reduction_and_teams () != 0)
  153. __builtin_abort ();
  154. return 0;
  155. }