1 // RUN: %libomp-compile-and-run
4 #include "omp_testsuite.h"
6 int test_omp_parallel_sections_reduction()
13 double dt
=0.5; /* base of geometric row for + and - test*/
14 double rounding_error
= 1.E
-5;
41 // Test summation of integers
42 known_sum
= (999*1000)/2+7;
43 #pragma omp parallel sections private(i) reduction(+:sum)
53 for (i
=300;i
<700;i
++) {
59 for (i
=700;i
<1000;i
++) {
66 fprintf(stderr
,"Error in sum with integers: Result was %d"
67 " instead of %d.\n",sum
, known_sum
);
70 // Test differences of integers
72 #pragma omp parallel sections private(i) reduction(-:diff)
82 for (i
=300;i
<700;i
++) {
88 for (i
=700;i
<1000;i
++) {
95 fprintf(stderr
,"Error in Difference with integers: Result was %d"
96 " instead of 0.\n",diff
);
99 // Test summation of doubles
103 dknown_sum
= (1-dpt
)/(1-dt
);
104 #pragma omp parallel sections private(i) reduction(+:dsum)
120 for (i
=12;i
<20;++i
) {
125 if( fabs(dsum
-dknown_sum
) > rounding_error
) {
127 fprintf(stderr
,"Error in sum with doubles: Result was %f"
128 " instead of %f (Difference: %E)\n",
129 dsum
, dknown_sum
, dsum
-dknown_sum
);
132 // Test differences of doubles
137 fprintf(stderr
,"\n");
138 ddiff
= (1-dpt
)/(1-dt
);
139 #pragma omp parallel sections private(i) reduction(-:ddiff)
155 for (i
=12;i
<20;++i
) {
160 if( fabs(ddiff
) > rounding_error
) {
162 fprintf(stderr
,"Error in Difference with doubles: Result was %E"
163 " instead of 0.0\n",ddiff
);
166 // Test product of integers
167 known_product
= 3628800;
168 #pragma omp parallel sections private(i) reduction(*:product)
189 if(known_product
!= product
) {
191 fprintf(stderr
,"Error in Product with integers: Result was %d"
192 " instead of %d\n",product
,known_product
);
196 for(i
=0;i
<1000;i
++) {
200 #pragma omp parallel sections private(i) reduction(&&:logic_and)
204 for (i
=1;i
<300;i
++) {
205 logic_and
= (logic_and
&& logics
[i
]);
210 for (i
=300;i
<700;i
++) {
211 logic_and
= (logic_and
&& logics
[i
]);
216 for (i
=700;i
<1000;i
++) {
217 logic_and
= (logic_and
&& logics
[i
]);
223 fprintf(stderr
,"Error in logic AND part 1\n");
228 #pragma omp parallel sections private(i) reduction(&&:logic_and)
232 for (i
=1;i
<300;i
++) {
233 logic_and
= (logic_and
&& logics
[i
]);
238 for (i
=300;i
<700;i
++) {
239 logic_and
= (logic_and
&& logics
[i
]);
244 for (i
=700;i
<1000;i
++) {
245 logic_and
= (logic_and
&& logics
[i
]);
251 fprintf(stderr
,"Error in logic AND part 2");
255 for(i
=0;i
<1000;i
++) {
259 #pragma omp parallel sections private(i) reduction(||:logic_or)
263 for (i
=1;i
<300;i
++) {
264 logic_or
= (logic_or
|| logics
[i
]);
269 for (i
=300;i
<700;i
++) {
270 logic_or
= (logic_or
|| logics
[i
]);
275 for (i
=700;i
<1000;i
++) {
276 logic_or
= (logic_or
|| logics
[i
]);
282 fprintf(stderr
,"Error in logic OR part 1\n");
288 #pragma omp parallel sections private(i) reduction(||:logic_or)
292 for (i
=1;i
<300;i
++) {
293 logic_or
= (logic_or
|| logics
[i
]);
298 for (i
=300;i
<700;i
++) {
299 logic_or
= (logic_or
|| logics
[i
]);
304 for (i
=700;i
<1000;i
++) {
305 logic_or
= (logic_or
|| logics
[i
]);
311 fprintf(stderr
,"Error in logic OR part 2\n");
315 for(i
=0;i
<1000;++i
) {
319 #pragma omp parallel sections private(i) reduction(&:bit_and)
324 bit_and
= (bit_and
& logics
[i
]);
329 for(i
=300;i
<700;++i
) {
330 bit_and
= (bit_and
& logics
[i
]);
335 for(i
=700;i
<1000;++i
) {
336 bit_and
= (bit_and
& logics
[i
]);
342 fprintf(stderr
,"Error in BIT AND part 1\n");
348 #pragma omp parallel sections private(i) reduction(&:bit_and)
353 bit_and
= bit_and
& logics
[i
];
358 for(i
=300;i
<700;++i
) {
359 bit_and
= bit_and
& logics
[i
];
364 for(i
=700;i
<1000;++i
) {
365 bit_and
= bit_and
& logics
[i
];
371 fprintf(stderr
,"Error in BIT AND part 2");
375 for(i
=0;i
<1000;i
++) {
379 #pragma omp parallel sections private(i) reduction(|:bit_or)
384 bit_or
= bit_or
| logics
[i
];
389 for(i
=300;i
<700;++i
) {
390 bit_or
= bit_or
| logics
[i
];
395 for(i
=700;i
<1000;++i
) {
396 bit_or
= bit_or
| logics
[i
];
402 fprintf(stderr
,"Error in BIT OR part 1\n");
407 #pragma omp parallel sections private(i) reduction(|:bit_or)
412 bit_or
= bit_or
| logics
[i
];
417 for(i
=300;i
<700;++i
) {
418 bit_or
= bit_or
| logics
[i
];
423 for(i
=700;i
<1000;++i
) {
424 bit_or
= bit_or
| logics
[i
];
430 fprintf(stderr
,"Error in BIT OR part 2\n");
434 for(i
=0;i
<1000;i
++) {
438 #pragma omp parallel sections private(i) reduction(^:exclusiv_bit_or)
443 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
448 for(i
=300;i
<700;++i
) {
449 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
454 for(i
=700;i
<1000;++i
) {
455 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
459 if(exclusiv_bit_or
) {
461 fprintf(stderr
,"Error in EXCLUSIV BIT OR part 1\n");
467 #pragma omp parallel sections private(i) reduction(^:exclusiv_bit_or)
472 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
477 for(i
=300;i
<700;++i
) {
478 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
483 for(i
=700;i
<1000;++i
) {
484 exclusiv_bit_or
= exclusiv_bit_or
^ logics
[i
];
488 if(!exclusiv_bit_or
) {
490 fprintf(stderr
,"Error in EXCLUSIV BIT OR part 2\n");
493 /*printf("\nResult:%d\n",result);*/
502 for(i
= 0; i
< REPETITIONS
; i
++) {
503 if(!test_omp_parallel_sections_reduction()) {