[memprof] Upgrade a unit test to MemProf Version 3 (#117063)
[llvm-project.git] / openmp / runtime / test / worksharing / sections / omp_parallel_sections_reduction.c
blob0d49865ee43f92fe705a14f441b18ae8b8008792
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include <math.h>
4 #include "omp_testsuite.h"
6 int test_omp_parallel_sections_reduction()
8 int sum;
9 int known_sum;
10 double dpt;
11 double dsum;
12 double dknown_sum;
13 double dt=0.5; /* base of geometric row for + and - test*/
14 double rounding_error= 1.E-5;
15 int diff;
16 double ddiff;
17 int product;
18 int known_product;
19 int logic_and;
20 int bit_and;
21 int logic_or;
22 int bit_or;
23 int exclusiv_bit_or;
24 int logics[1000];
25 int i;
26 int result;
28 sum = 7;
29 dsum=0;
30 product =1;
31 dpt = 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 /* int my_islarger;*/
39 /*int is_larger=1;*/
41 // Test summation of integers
42 known_sum = (999*1000)/2+7;
43 #pragma omp parallel 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;
64 if(known_sum!=sum) {
65 result++;
66 fprintf(stderr,"Error in sum with integers: Result was %d"
67 " instead of %d.\n",sum, known_sum);
70 // Test differences of integers
71 diff = (999*1000)/2;
72 #pragma omp parallel sections private(i) reduction(-:diff)
74 #pragma omp section
76 for (i=1;i<300;i++) {
77 diff=diff-i;
80 #pragma omp section
82 for (i=300;i<700;i++) {
83 diff=diff-i;
86 #pragma omp section
88 for (i=700;i<1000;i++) {
89 diff=diff-i;
93 if(diff != 0) {
94 result++;
95 fprintf(stderr,"Error in Difference with integers: Result was %d"
96 " instead of 0.\n",diff);
99 // Test summation of doubles
100 for (i=0;i<20;++i) {
101 dpt*=dt;
103 dknown_sum = (1-dpt)/(1-dt);
104 #pragma omp parallel sections private(i) reduction(+:dsum)
106 #pragma omp section
108 for (i=0;i<6;++i) {
109 dsum += pow(dt,i);
112 #pragma omp section
114 for (i=6;i<12;++i) {
115 dsum += pow(dt,i);
118 #pragma omp section
120 for (i=12;i<20;++i) {
121 dsum += pow(dt,i);
125 if( fabs(dsum-dknown_sum) > rounding_error ) {
126 result++;
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
133 dpt=1;
134 for (i=0;i<20;++i) {
135 dpt*=dt;
137 fprintf(stderr,"\n");
138 ddiff = (1-dpt)/(1-dt);
139 #pragma omp parallel sections private(i) reduction(-:ddiff)
141 #pragma omp section
143 for (i=0;i<6;++i) {
144 ddiff -= pow(dt,i);
147 #pragma omp section
149 for (i=6;i<12;++i) {
150 ddiff -= pow(dt,i);
153 #pragma omp section
155 for (i=12;i<20;++i) {
156 ddiff -= pow(dt,i);
160 if( fabs(ddiff) > rounding_error) {
161 result++;
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)
170 #pragma omp section
172 for(i=1;i<3;i++) {
173 product *= i;
176 #pragma omp section
178 for(i=3;i<7;i++) {
179 product *= i;
182 #pragma omp section
184 for(i=7;i<11;i++) {
185 product *= i;
189 if(known_product != product) {
190 result++;
191 fprintf(stderr,"Error in Product with integers: Result was %d"
192 " instead of %d\n",product,known_product);
195 // Test logical AND
196 for(i=0;i<1000;i++) {
197 logics[i]=1;
200 #pragma omp parallel sections private(i) reduction(&&:logic_and)
202 #pragma omp section
204 for (i=1;i<300;i++) {
205 logic_and = (logic_and && logics[i]);
208 #pragma omp section
210 for (i=300;i<700;i++) {
211 logic_and = (logic_and && logics[i]);
214 #pragma omp section
216 for (i=700;i<1000;i++) {
217 logic_and = (logic_and && logics[i]);
221 if(!logic_and) {
222 result++;
223 fprintf(stderr,"Error in logic AND part 1\n");
225 logic_and = 1;
226 logics[501] = 0;
228 #pragma omp parallel sections private(i) reduction(&&:logic_and)
230 #pragma omp section
232 for (i=1;i<300;i++) {
233 logic_and = (logic_and && logics[i]);
236 #pragma omp section
238 for (i=300;i<700;i++) {
239 logic_and = (logic_and && logics[i]);
242 #pragma omp section
244 for (i=700;i<1000;i++) {
245 logic_and = (logic_and && logics[i]);
249 if(logic_and) {
250 result++;
251 fprintf(stderr,"Error in logic AND part 2");
254 // Test logical OR
255 for(i=0;i<1000;i++) {
256 logics[i]=0;
259 #pragma omp parallel sections private(i) reduction(||:logic_or)
261 #pragma omp section
263 for (i=1;i<300;i++) {
264 logic_or = (logic_or || logics[i]);
267 #pragma omp section
269 for (i=300;i<700;i++) {
270 logic_or = (logic_or || logics[i]);
273 #pragma omp section
275 for (i=700;i<1000;i++) {
276 logic_or = (logic_or || logics[i]);
280 if(logic_or) {
281 result++;
282 fprintf(stderr,"Error in logic OR part 1\n");
285 logic_or = 0;
286 logics[501]=1;
288 #pragma omp parallel sections private(i) reduction(||:logic_or)
290 #pragma omp section
292 for (i=1;i<300;i++) {
293 logic_or = (logic_or || logics[i]);
296 #pragma omp section
298 for (i=300;i<700;i++) {
299 logic_or = (logic_or || logics[i]);
302 #pragma omp section
304 for (i=700;i<1000;i++) {
305 logic_or = (logic_or || logics[i]);
309 if(!logic_or) {
310 result++;
311 fprintf(stderr,"Error in logic OR part 2\n");
314 // Test bitwise AND
315 for(i=0;i<1000;++i) {
316 logics[i]=1;
319 #pragma omp parallel sections private(i) reduction(&:bit_and)
321 #pragma omp section
323 for(i=0;i<300;++i) {
324 bit_and = (bit_and & logics[i]);
327 #pragma omp section
329 for(i=300;i<700;++i) {
330 bit_and = (bit_and & logics[i]);
333 #pragma omp section
335 for(i=700;i<1000;++i) {
336 bit_and = (bit_and & logics[i]);
340 if(!bit_and) {
341 result++;
342 fprintf(stderr,"Error in BIT AND part 1\n");
345 bit_and = 1;
346 logics[501]=0;
348 #pragma omp parallel sections private(i) reduction(&:bit_and)
350 #pragma omp section
352 for(i=0;i<300;++i) {
353 bit_and = bit_and & logics[i];
356 #pragma omp section
358 for(i=300;i<700;++i) {
359 bit_and = bit_and & logics[i];
362 #pragma omp section
364 for(i=700;i<1000;++i) {
365 bit_and = bit_and & logics[i];
369 if(bit_and) {
370 result++;
371 fprintf(stderr,"Error in BIT AND part 2");
374 // Test bitwise OR
375 for(i=0;i<1000;i++) {
376 logics[i]=0;
379 #pragma omp parallel sections private(i) reduction(|:bit_or)
381 #pragma omp section
383 for(i=0;i<300;++i) {
384 bit_or = bit_or | logics[i];
387 #pragma omp section
389 for(i=300;i<700;++i) {
390 bit_or = bit_or | logics[i];
393 #pragma omp section
395 for(i=700;i<1000;++i) {
396 bit_or = bit_or | logics[i];
400 if(bit_or) {
401 result++;
402 fprintf(stderr,"Error in BIT OR part 1\n");
404 bit_or = 0;
405 logics[501]=1;
407 #pragma omp parallel sections private(i) reduction(|:bit_or)
409 #pragma omp section
411 for(i=0;i<300;++i) {
412 bit_or = bit_or | logics[i];
415 #pragma omp section
417 for(i=300;i<700;++i) {
418 bit_or = bit_or | logics[i];
421 #pragma omp section
423 for(i=700;i<1000;++i) {
424 bit_or = bit_or | logics[i];
428 if(!bit_or) {
429 result++;
430 fprintf(stderr,"Error in BIT OR part 2\n");
433 // Test bitwise XOR
434 for(i=0;i<1000;i++) {
435 logics[i]=0;
438 #pragma omp parallel sections private(i) reduction(^:exclusiv_bit_or)
440 #pragma omp section
442 for(i=0;i<300;++i) {
443 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
446 #pragma omp section
448 for(i=300;i<700;++i) {
449 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
452 #pragma omp section
454 for(i=700;i<1000;++i) {
455 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
459 if(exclusiv_bit_or) {
460 result++;
461 fprintf(stderr,"Error in EXCLUSIV BIT OR part 1\n");
464 exclusiv_bit_or = 0;
465 logics[501]=1;
467 #pragma omp parallel sections private(i) reduction(^:exclusiv_bit_or)
469 #pragma omp section
471 for(i=0;i<300;++i) {
472 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
475 #pragma omp section
477 for(i=300;i<700;++i) {
478 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
481 #pragma omp section
483 for(i=700;i<1000;++i) {
484 exclusiv_bit_or = exclusiv_bit_or ^ logics[i];
488 if(!exclusiv_bit_or) {
489 result++;
490 fprintf(stderr,"Error in EXCLUSIV BIT OR part 2\n");
493 /*printf("\nResult:%d\n",result);*/
494 return (result==0);
497 int main()
499 int i;
500 int num_failed=0;
502 for(i = 0; i < REPETITIONS; i++) {
503 if(!test_omp_parallel_sections_reduction()) {
504 num_failed++;
507 return num_failed;