1 // RUN: %libomp-compile-and-run
4 #include "omp_testsuite.h"
6 #define DOUBLE_DIGITS 20 /* dt^DOUBLE_DIGITS */
8 #define KNOWN_PRODUCT 3628800 /* 10! */
10 int test_omp_parallel_reduction()
16 double dt
=0.5; /* base of geometric row for + and - test*/
17 double rounding_error
= 1.E
-9;
27 int logics
[LOOPCOUNT
];
42 known_sum
= (LOOPCOUNT
*(LOOPCOUNT
+1))/2;
44 /* Tests for integers */
45 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(+:sum)
46 for (i
=1;i
<=LOOPCOUNT
;i
++) {
52 fprintf(stderr
,"Error in sum with integers: Result was %d instead of %d\n",sum
,known_sum
);
55 diff
= (LOOPCOUNT
*(LOOPCOUNT
+1))/2;
56 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(-:diff)
57 for (i
=1;i
<=LOOPCOUNT
;++i
) {
63 fprintf(stderr
,"Error in difference with integers: Result was %d instead of 0.\n",diff
);
66 /* Tests for doubles */
69 for (i
=0;i
<DOUBLE_DIGITS
;++i
) {
72 dknown_sum
= (1-dpt
)/(1-dt
);
73 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(+:dsum)
74 for (i
=0;i
<DOUBLE_DIGITS
;++i
) {
78 if( fabs(dsum
-dknown_sum
) > rounding_error
) {
80 fprintf(stderr
,"Error in sum with doubles: Result was %f instead of %f (Difference: %E)\n",dsum
,dknown_sum
, dsum
-dknown_sum
);
85 for (i
=0;i
<DOUBLE_DIGITS
;++i
) {
89 ddiff
= (1-dpt
)/(1-dt
);
90 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(-:ddiff)
91 for (i
=0;i
<DOUBLE_DIGITS
;++i
) {
94 if( fabs(ddiff
) > rounding_error
) {
96 fprintf(stderr
,"Error in Difference with doubles: Result was %E instead of 0.0\n",ddiff
);
99 /* Tests for product of integers */
100 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(*:product)
101 for(i
=1;i
<=MAX_FACTOR
;i
++) {
105 known_product
= KNOWN_PRODUCT
;
106 if(known_product
!= product
) {
108 fprintf(stderr
,"Error in Product with integers: Result was %d instead of %d\n\n",product
,known_product
);
111 /* Tests for logical and */
112 for(i
=0;i
<LOOPCOUNT
;i
++) {
116 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(&&:logic_and)
117 for(i
=0;i
<LOOPCOUNT
;++i
) {
118 logic_and
= (logic_and
&& logics
[i
]);
122 fprintf(stderr
,"Error in logic AND part 1.\n");
126 logics
[LOOPCOUNT
/2]=0;
128 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(&&:logic_and)
129 for(i
=0;i
<LOOPCOUNT
;++i
) {
130 logic_and
= logic_and
&& logics
[i
];
134 fprintf(stderr
,"Error in logic AND part 2.\n");
137 /* Tests for logical or */
138 for(i
=0;i
<LOOPCOUNT
;i
++) {
142 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(||:logic_or)
143 for(i
=0;i
<LOOPCOUNT
;++i
) {
144 logic_or
= logic_or
|| logics
[i
];
148 fprintf(stderr
,"Error in logic OR part 1.\n");
151 logics
[LOOPCOUNT
/2]=1;
153 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(||:logic_or)
154 for(i
=0;i
<LOOPCOUNT
;++i
) {
155 logic_or
= logic_or
|| logics
[i
];
159 fprintf(stderr
,"Error in logic OR part 2.\n");
162 /* Tests for bitwise and */
163 for(i
=0;i
<LOOPCOUNT
;++i
) {
167 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(&:bit_and)
168 for(i
=0;i
<LOOPCOUNT
;++i
) {
169 bit_and
= (bit_and
& logics
[i
]);
173 fprintf(stderr
,"Error in BIT AND part 1.\n");
177 logics
[LOOPCOUNT
/2]=0;
179 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(&:bit_and)
180 for(i
=0;i
<LOOPCOUNT
;++i
) {
181 bit_and
= bit_and
& logics
[i
];
185 fprintf(stderr
,"Error in BIT AND part 2.\n");
188 for(i
=0;i
<LOOPCOUNT
;i
++) {
192 /* Tests for bitwise or */
193 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(|:bit_or)
194 for(i
=0;i
<LOOPCOUNT
;++i
) {
195 bit_or
= bit_or
| logics
[i
];
199 fprintf(stderr
,"Error in BIT OR part 1\n");
202 logics
[LOOPCOUNT
/2]=1;
204 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(|:bit_or)
205 for(i
=0;i
<LOOPCOUNT
;++i
) {
206 bit_or
= bit_or
| logics
[i
];
210 fprintf(stderr
,"Error in BIT OR part 2\n");
213 for(i
=0;i
<LOOPCOUNT
;i
++) {
217 /* Tests for bitwise xor */
218 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(^:exclusiv_bit_or)
219 for(i
=0;i
<LOOPCOUNT
;++i
) {
220 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
222 if(exclusiv_bit_or
) {
224 fprintf(stderr
,"Error in EXCLUSIV BIT OR part 1\n");
228 logics
[LOOPCOUNT
/2]=1;
230 #pragma omp parallel for schedule(dynamic,1) private(i) reduction(^:exclusiv_bit_or)
231 for(i
=0;i
<LOOPCOUNT
;++i
) {
232 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
234 if(!exclusiv_bit_or
) {
236 fprintf(stderr
,"Error in EXCLUSIV BIT OR part 2\n");
239 /*printf("\nResult:%d\n",result);*/
248 for(i
= 0; i
< REPETITIONS
; i
++) {
249 if(!test_omp_parallel_reduction()) {