1 // RUN: %libomp-compile-and-run
5 #include "omp_testsuite.h"
7 #define DOUBLE_DIGITS 20 /* dt^DOUBLE_DIGITS */
9 #define KNOWN_PRODUCT 3628800 /* 10! */
11 int test_omp_for_reduction ()
29 double rounding_error
= 1.E
-9; /* over all rounding error to be
30 ignored in the double tests */
33 int logicsArray
[LOOPCOUNT
];
35 /* Variables for integer tests */
38 known_sum
= (LOOPCOUNT
* (LOOPCOUNT
+ 1)) / 2;
39 /* variabels for double tests */
40 dt
= 1. / 3.; /* base of geometric row for + and - test*/
42 /* Variabeles for logic tests */
46 /* Variabeles for bit operators tests */
49 /* Variables for exclusiv bit or */
52 /************************************************************************/
53 /** Tests for integers **/
54 /************************************************************************/
56 /**** Testing integer addition ****/
60 #pragma omp for schedule(dynamic,1) reduction(+:sum)
61 for (j
= 1; j
<= LOOPCOUNT
; j
++) {
65 if (known_sum
!= sum
) {
67 fprintf (stderr
, "Error in sum with integers: Result was %d"
68 " instead of %d.\n", sum
, known_sum
);
71 /**** Testing integer subtracton ****/
72 diff
= (LOOPCOUNT
* (LOOPCOUNT
+ 1)) / 2;
76 #pragma omp for schedule(dynamic,1) reduction(-:diff)
77 for (j
= 1; j
<= LOOPCOUNT
; j
++) {
83 fprintf (stderr
, "Error in difference with integers: Result was %d"
84 " instead of 0.\n", diff
);
87 /**** Testing integer multiplication ****/
91 #pragma omp for schedule(dynamic,1) reduction(*:product)
92 for (j
= 1; j
<= MAX_FACTOR
; j
++) {
96 known_product
= KNOWN_PRODUCT
;
97 if(known_product
!= product
) {
99 fprintf (stderr
,"Error in Product with integers: Result was %d"
100 " instead of %d\n",product
,known_product
);
103 /************************************************************************/
104 /** Tests for doubles **/
105 /************************************************************************/
107 /**** Testing double addition ****/
110 for (i
= 0; i
< DOUBLE_DIGITS
; ++i
) {
113 dknown_sum
= (1 - dpt
) / (1 - dt
);
117 #pragma omp for schedule(dynamic,1) reduction(+:dsum)
118 for (j
= 0; j
< DOUBLE_DIGITS
; j
++) {
122 if (fabs (dsum
- dknown_sum
) > rounding_error
) {
124 fprintf (stderr
, "\nError in sum with doubles: Result was %f"
125 " instead of: %f (Difference: %E)\n",
126 dsum
, dknown_sum
, dsum
-dknown_sum
);
129 /**** Testing double subtraction ****/
130 ddiff
= (1 - dpt
) / (1 - dt
);
134 #pragma omp for schedule(dynamic,1) reduction(-:ddiff)
135 for (j
= 0; j
< DOUBLE_DIGITS
; ++j
) {
136 ddiff
-= pow (dt
, j
);
139 if (fabs (ddiff
) > rounding_error
) {
141 fprintf (stderr
, "Error in Difference with doubles: Result was %E"
142 " instead of 0.0\n", ddiff
);
146 /************************************************************************/
147 /** Tests for logical values **/
148 /************************************************************************/
150 /**** Testing logic and ****/
151 for (i
= 0; i
< LOOPCOUNT
; i
++) {
158 #pragma omp for schedule(dynamic,1) reduction(&&:logic_and)
159 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
160 logic_and
= (logic_and
&& logics
[j
]);
165 fprintf (stderr
, "Error in logic AND part 1\n");
169 logics
[LOOPCOUNT
/ 2] = 0;
174 #pragma omp for schedule(dynamic,1) reduction(&&:logic_and)
175 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
176 logic_and
= logic_and
&& logics
[j
];
181 fprintf (stderr
, "Error in logic AND part 2\n");
184 /**** Testing logic or ****/
185 for (i
= 0; i
< LOOPCOUNT
; i
++) {
192 #pragma omp for schedule(dynamic,1) reduction(||:logic_or)
193 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
194 logic_or
= logic_or
|| logics
[j
];
199 fprintf (stderr
, "Error in logic OR part 1\n");
203 logics
[LOOPCOUNT
/ 2] = 1;
208 #pragma omp for schedule(dynamic,1) reduction(||:logic_or)
209 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
210 logic_or
= logic_or
|| logics
[j
];
215 fprintf (stderr
, "Error in logic OR part 2\n");
218 /************************************************************************/
219 /** Tests for bit values **/
220 /************************************************************************/
222 /**** Testing bit and ****/
223 for (i
= 0; i
< LOOPCOUNT
; ++i
) {
230 #pragma omp for schedule(dynamic,1) reduction(&:bit_and)
231 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
232 bit_and
= (bit_and
& logics
[j
]);
237 fprintf (stderr
, "Error in BIT AND part 1\n");
241 logics
[LOOPCOUNT
/ 2] = 0;
246 #pragma omp for schedule(dynamic,1) reduction(&:bit_and)
247 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
248 bit_and
= bit_and
& logics
[j
];
253 fprintf (stderr
, "Error in BIT AND part 2\n");
256 /**** Testing bit or ****/
257 for (i
= 0; i
< LOOPCOUNT
; i
++) {
264 #pragma omp for schedule(dynamic,1) reduction(|:bit_or)
265 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
266 bit_or
= bit_or
| logics
[j
];
271 fprintf (stderr
, "Error in BIT OR part 1\n");
275 logics
[LOOPCOUNT
/ 2] = 1;
280 #pragma omp for schedule(dynamic,1) reduction(|:bit_or)
281 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
282 bit_or
= bit_or
| logics
[j
];
287 fprintf (stderr
, "Error in BIT OR part 2\n");
290 /**** Testing exclusive bit or ****/
291 for (i
= 0; i
< LOOPCOUNT
; i
++) {
298 #pragma omp for schedule(dynamic,1) reduction(^:exclusiv_bit_or)
299 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
300 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[j
];
303 if (exclusiv_bit_or
) {
305 fprintf (stderr
, "Error in EXCLUSIV BIT OR part 1\n");
309 logics
[LOOPCOUNT
/ 2] = 1;
314 #pragma omp for schedule(dynamic,1) reduction(^:exclusiv_bit_or)
315 for (j
= 0; j
< LOOPCOUNT
; ++j
) {
316 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[j
];
319 if (!exclusiv_bit_or
) {
321 fprintf (stderr
, "Error in EXCLUSIV BIT OR part 2\n");
324 return (result
== 0);
333 for(i
= 0; i
< REPETITIONS
; i
++) {
334 if(!test_omp_for_reduction()) {