xref: /llvm-project/openmp/runtime/test/worksharing/for/omp_for_schedule_auto.c (revision 373107699709f6fb06992bf6b76274091570aaf2)
1 // RUN: %libomp-compile-and-run
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <math.h>
5 #include "omp_testsuite.h"
6 
7 int sum1;
8 #pragma omp threadprivate(sum1)
9 
test_omp_for_auto()10 int test_omp_for_auto()
11 {
12   int j;
13   int sum;
14   int sum0;
15   int known_sum;
16   int threadsnum;
17 
18   sum = 0;
19   sum0 = 12345;
20 
21   // array which keeps track of which threads participated in the for loop
22   // e.g., given 4 threads, [ 0 | 1 | 1 | 0 ] implies
23   //       threads 0 and 3 did not, threads 1 and 2 did
24   int max_threads = omp_get_max_threads();
25   int* active_threads = (int*)malloc(sizeof(int)*max_threads);
26   for(j = 0; j < max_threads; j++)
27     active_threads[j] = 0;
28 
29   #pragma omp parallel
30   {
31     int i;
32     sum1 = 0;
33     #pragma omp for firstprivate(sum0) schedule(auto)
34     for (i = 1; i <= LOOPCOUNT; i++) {
35       active_threads[omp_get_thread_num()] = 1;
36       sum0 = sum0 + i;
37       sum1 = sum0;
38     }
39 
40     #pragma omp critical
41     {
42       sum = sum + sum1;
43     }
44   }
45 
46   // count the threads that participated (sum is stored in threadsnum)
47   threadsnum=0;
48   for(j = 0; j < max_threads; j++) {
49     if(active_threads[j])
50       threadsnum++;
51   }
52   free(active_threads);
53 
54   known_sum = 12345 * threadsnum + (LOOPCOUNT * (LOOPCOUNT + 1)) / 2;
55   return (known_sum == sum);
56 }
57 
main()58 int main()
59 {
60   int i;
61   int num_failed=0;
62 
63   for(i = 0; i < REPETITIONS; i++) {
64     if(!test_omp_for_auto()) {
65       num_failed++;
66     }
67   }
68   return num_failed;
69 }
70