PR target/35485
[official-gcc.git] / libgomp / testsuite / libgomp.c / loop-2.c
blob4bae023c79e26a4f7c15470e727eb2253f593fb6
1 /* Validate static scheduling iteration dispatch. We only test with
2 even thread distributions here; there are multiple valid solutions
3 for uneven thread distributions. */
5 /* { dg-require-effective-target sync_int_long } */
7 #include <omp.h>
8 #include <string.h>
9 #include <assert.h>
10 #include "libgomp_g.h"
13 #define N 360
14 static int data[N][2];
15 static int INCR, NTHR, CHUNK;
17 static void clean_data (void)
19 memset (data, -1, sizeof (data));
22 static void test_data (void)
24 int n, i, c, thr, iter, chunk;
26 chunk = CHUNK;
27 if (chunk == 0)
28 chunk = N / INCR / NTHR;
30 thr = iter = c = i = 0;
32 for (n = 0; n < N; ++n)
34 if (i == 0)
36 assert (data[n][0] == thr);
37 assert (data[n][1] == iter);
39 else
41 assert (data[n][0] == -1);
42 assert (data[n][1] == -1);
45 if (++i == INCR)
47 i = 0;
48 if (++c == chunk)
50 c = 0;
51 if (++thr == NTHR)
53 thr = 0;
54 ++iter;
61 static void set_data (long i, int thr, int iter)
63 int old;
64 assert (i >= 0 && i < N);
65 old = __sync_lock_test_and_set (&data[i][0], thr);
66 assert (old == -1);
67 old = __sync_lock_test_and_set (&data[i][1], iter);
68 assert (old == -1);
71 static void f_static_1 (void *dummy)
73 int iam = omp_get_thread_num ();
74 long s0, e0, i, count = 0;
75 if (GOMP_loop_static_start (0, N, INCR, CHUNK, &s0, &e0))
78 for (i = s0; i < e0; i += INCR)
79 set_data (i, iam, count);
80 ++count;
82 while (GOMP_loop_static_next (&s0, &e0));
83 GOMP_loop_end ();
86 static void test (void)
88 clean_data ();
89 GOMP_parallel_start (f_static_1, NULL, NTHR);
90 f_static_1 (NULL);
91 GOMP_parallel_end ();
92 test_data ();
95 int main()
97 omp_set_dynamic (0);
99 NTHR = 5;
101 INCR = 1, CHUNK = 0; /* chunk = 360 / 5 = 72 */
102 test ();
104 INCR = 4, CHUNK = 0; /* chunk = 360 / 4 / 5 = 18 */
105 test ();
107 INCR = 1, CHUNK = 4; /* 1 * 4 * 5 = 20 -> 360 / 20 = 18 iterations. */
108 test ();
110 INCR = 3, CHUNK = 4; /* 3 * 4 * 5 = 60 -> 360 / 60 = 6 iterations. */
111 test ();
113 return 0;