[memprof] Upgrade a unit test to MemProf Version 3 (#117063)
[llvm-project.git] / openmp / runtime / test / worksharing / sections / omp_sections_reduction.c
blob1fdb5ecdfeb86d174def8bf5e6cf1b9a00d9c834
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include <math.h>
4 #include "omp_testsuite.h"
6 int test_omp_sections_reduction()
8 int sum;
9 int known_sum;
10 double dpt,dsum;
11 double dknown_sum;
12 double dt=0.5; /* base of geometric row for + and - test*/
13 double rounding_error= 1.E-9;
14 int diff;
15 double ddiff;
16 int product;
17 int known_product;
18 int logic_and;
19 int bit_and;
20 int logic_or;
21 int bit_or;
22 int exclusiv_bit_or;
23 int logics[1000];
24 int i;
25 int result;
26 /* int my_islarger; */
27 /*int is_larger=1;*/
28 sum =7;
29 dpt =1;
30 dsum=0;
31 product =1;
32 logic_and=1;
33 bit_and=1;
34 logic_or=0;
35 bit_or=0;
36 exclusiv_bit_or=0;
37 result = 0;
38 dt = 1./3.;
40 known_sum = (999*1000)/2+7;
41 #pragma omp parallel
43 #pragma omp sections private(i) reduction(+:sum)
45 #pragma omp section
47 for (i=1;i<300;i++) {
48 sum=sum+i;
51 #pragma omp section
53 for (i=300;i<700;i++) {
54 sum=sum+i;
57 #pragma omp section
59 for (i=700;i<1000;i++) {
60 sum=sum+i;
65 if(known_sum!=sum) {
66 ++result;
67 fprintf(stderr,"Error in sum with integers: Result was %d"
68 " instead of %d\n", sum,known_sum);
71 diff = (999*1000)/2;
72 #pragma omp parallel
74 #pragma omp sections private(i) reduction(-:diff)
76 #pragma omp section
78 for (i=1;i<300;i++) {
79 diff=diff-i;
82 #pragma omp section
84 for (i=300;i<700;i++) {
85 diff=diff-i;
88 #pragma omp section
90 for (i=700;i<1000;i++) {
91 diff=diff-i;
96 if(diff != 0) {
97 result++;
98 fprintf(stderr,"Error in Difference with integers: Result was %d"
99 " instead of 0.\n",diff);
102 for (i=0;i<20;++i) {
103 dpt*=dt;
105 dknown_sum = (1-dpt)/(1-dt);
106 #pragma omp parallel
108 #pragma omp sections private(i) reduction(+:dsum)
110 #pragma omp section
112 for (i=0;i<6;++i) {
113 dsum += pow(dt,i);
116 #pragma omp section
118 for (i=6;i<12;++i) {
119 dsum += pow(dt,i);
122 #pragma omp section
124 for (i=12;i<20;++i) {
125 dsum += pow(dt,i);
130 if( fabs(dsum-dknown_sum) > rounding_error ) {
131 result++;
132 fprintf(stderr,"Error in sum with doubles: Result was %f"
133 " instead of %f (Difference: %E)\n",
134 dsum, dknown_sum, dsum-dknown_sum);
137 dpt=1;
138 for (i=0;i<20;++i) {
139 dpt*=dt;
141 fprintf(stderr,"\n");
142 ddiff = (1-dpt)/(1-dt);
143 #pragma omp parallel
145 #pragma omp sections private(i) reduction(-:ddiff)
147 #pragma omp section
149 for (i=0;i<6;++i) {
150 ddiff -= pow(dt,i);
153 #pragma omp section
155 for (i=6;i<12;++i) {
156 ddiff -= pow(dt,i);
159 #pragma omp section
161 for (i=12;i<20;++i) {
162 ddiff -= pow(dt,i);
168 if(fabs(ddiff) > rounding_error) {
169 result++;
170 fprintf(stderr,"Error in Difference with doubles: Result was %E"
171 " instead of 0.0\n",ddiff);
174 known_product = 3628800;
175 #pragma omp parallel
177 #pragma omp sections private(i) reduction(*:product)
179 #pragma omp section
181 for(i=1;i<3;i++) {
182 product *= i;
185 #pragma omp section
187 for(i=3;i<7;i++) {
188 product *= i;
191 #pragma omp section
193 for(i=7;i<11;i++) {
194 product *= i;
199 if(known_product != product) {
200 result++;
201 fprintf(stderr,"Error in Product with integers: Result was %d"
202 " instead of %d\n",product,known_product);
205 for(i=0;i<1000;i++) {
206 logics[i]=1;
209 #pragma omp parallel
211 #pragma omp sections private(i) reduction(&&:logic_and)
213 #pragma omp section
215 for (i=1;i<300;i++) {
216 logic_and = (logic_and && logics[i]);
219 #pragma omp section
221 for (i=300;i<700;i++) {
222 logic_and = (logic_and && logics[i]);
225 #pragma omp section
227 for (i=700;i<1000;i++) {
228 logic_and = (logic_and && logics[i]);
233 if(!logic_and) {
234 result++;
235 fprintf(stderr,"Error in logic AND part 1\n");
238 logic_and = 1;
239 logics[501] = 0;
241 #pragma omp parallel
243 #pragma omp sections private(i) reduction(&&:logic_and)
245 #pragma omp section
247 for (i=1;i<300;i++) {
248 logic_and = (logic_and && logics[i]);
251 #pragma omp section
253 for (i=300;i<700;i++) {
254 logic_and = (logic_and && logics[i]);
257 #pragma omp section
259 for (i=700;i<1000;i++) {
260 logic_and = (logic_and && logics[i]);
265 if(logic_and) {
266 result++;
267 fprintf(stderr,"Error in logic AND part 2\n");
270 for(i=0;i<1000;i++) {
271 logics[i]=0;
274 #pragma omp parallel
276 #pragma omp sections private(i) reduction(||:logic_or)
278 #pragma omp section
280 for (i=1;i<300;i++) {
281 logic_or = (logic_or || logics[i]);
284 #pragma omp section
286 for (i=300;i<700;i++) {
287 logic_or = (logic_or || logics[i]);
290 #pragma omp section
292 for (i=700;i<1000;i++) {
293 logic_or = (logic_or || logics[i]);
298 if(logic_or) {
299 result++;
300 fprintf(stderr,"\nError in logic OR part 1\n");
303 logic_or = 0;
304 logics[501]=1;
306 #pragma omp parallel
308 #pragma omp sections private(i) reduction(||:logic_or)
310 #pragma omp section
312 for (i=1;i<300;i++) {
313 logic_or = (logic_or || logics[i]);
316 #pragma omp section
318 for (i=300;i<700;i++) {
319 logic_or = (logic_or || logics[i]);
322 #pragma omp section
324 for (i=700;i<1000;i++) {
325 logic_or = (logic_or || logics[i]);
330 if(!logic_or) {
331 result++;
332 fprintf(stderr,"Error in logic OR part 2\n");
335 for(i=0;i<1000;++i) {
336 logics[i]=1;
339 #pragma omp parallel
341 #pragma omp sections private(i) reduction(&:bit_and)
343 #pragma omp section
345 for(i=0;i<300;++i) {
346 bit_and = (bit_and & logics[i]);
349 #pragma omp section
351 for(i=300;i<700;++i) {
352 bit_and = (bit_and & logics[i]);
355 #pragma omp section
357 for(i=700;i<1000;++i) {
358 bit_and = (bit_and & logics[i]);
363 if(!bit_and) {
364 result++;
365 fprintf(stderr,"Error in BIT AND part 1\n");
368 bit_and = 1;
369 logics[501]=0;
371 #pragma omp parallel
373 #pragma omp sections private(i) reduction(&:bit_and)
375 #pragma omp section
377 for(i=0;i<300;++i) {
378 bit_and = bit_and & logics[i];
381 #pragma omp section
383 for(i=300;i<700;++i) {
384 bit_and = bit_and & logics[i];
387 #pragma omp section
389 for(i=700;i<1000;++i) {
390 bit_and = bit_and & logics[i];
395 if(bit_and) {
396 result++;
397 fprintf(stderr,"Error in BIT AND part 2\n");
400 for(i=0;i<1000;i++) {
401 logics[i]=0;
404 #pragma omp parallel
406 #pragma omp sections private(i) reduction(|:bit_or)
408 #pragma omp section
410 for(i=0;i<300;++i) {
411 bit_or = bit_or | logics[i];
414 #pragma omp section
416 for(i=300;i<700;++i) {
417 bit_or = bit_or | logics[i];
420 #pragma omp section
422 for(i=700;i<1000;++i) {
423 bit_or = bit_or | logics[i];
428 if(bit_or) {
429 result++;
430 fprintf(stderr,"Error in BIT OR part 1\n");
432 bit_or = 0;
433 logics[501]=1;
435 #pragma omp parallel
437 #pragma omp sections private(i) reduction(|:bit_or)
439 #pragma omp section
441 for(i=0;i<300;++i) {
442 bit_or = bit_or | logics[i];
445 #pragma omp section
447 for(i=300;i<700;++i) {
448 bit_or = bit_or | logics[i];
451 #pragma omp section
453 for(i=700;i<1000;++i) {
454 bit_or = bit_or | logics[i];
459 if(!bit_or) {
460 result++;
461 fprintf(stderr,"Error in BIT OR part 2\n");
464 for(i=0;i<1000;i++) {
465 logics[i]=0;
468 #pragma omp parallel
470 #pragma omp sections private(i) reduction(^:exclusiv_bit_or)
472 #pragma omp section
474 for(i=0;i<300;++i) {
475 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
478 #pragma omp section
480 for(i=300;i<700;++i) {
481 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
484 #pragma omp section
486 for(i=700;i<1000;++i) {
487 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
492 if(exclusiv_bit_or) {
493 result++;
494 fprintf(stderr,"Error in EXCLUSIV BIT OR part 1\n");
497 exclusiv_bit_or = 0;
498 logics[501]=1;
500 #pragma omp parallel
502 #pragma omp sections private(i) reduction(^:exclusiv_bit_or)
504 #pragma omp section
506 for(i=0;i<300;++i) {
507 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
510 #pragma omp section
512 for(i=300;i<700;++i) {
513 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
516 #pragma omp section
518 for(i=700;i<1000;++i) {
519 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
524 if(!exclusiv_bit_or) {
525 result++;
526 fprintf(stderr,"Error in EXCLUSIV BIT OR part 2\n");
529 /*printf("\nResult:%d\n",result);*/
530 return (result==0);
532 int main()
534 int i;
535 int num_failed=0;
537 for(i = 0; i < REPETITIONS; i++) {
538 if(!test_omp_sections_reduction()) {
539 num_failed++;
542 return num_failed;