2011-09-27 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / libgomp / testsuite / libgomp.c / loop-1.c
blob11348dcfe41fb5589421e7cb336042ecdbe7028c
1 /* Test that all loop iterations are touched. This doesn't verify
2 scheduling order, merely coverage. */
4 /* { dg-require-effective-target sync_int_long } */
6 #include <omp.h>
7 #include <string.h>
8 #include <assert.h>
9 #include "libgomp_g.h"
12 #define N 10000
13 static int S, E, INCR, CHUNK, NTHR;
14 static int data[N];
16 static void clean_data (void)
18 memset (data, -1, sizeof (data));
21 static void test_data (void)
23 int i, j;
25 for (i = 0; i < S; ++i)
26 assert (data[i] == -1);
28 for (j = 0; i < E; ++i, j = (j + 1) % INCR)
29 if (j == 0)
30 assert (data[i] != -1);
31 else
32 assert (data[i] == -1);
34 for (; i < N; ++i)
35 assert (data[i] == -1);
38 static void set_data (long i, int val)
40 int old;
41 assert (i >= 0 && i < N);
42 old = __sync_lock_test_and_set (data+i, val);
43 assert (old == -1);
47 #define TMPL_1(sched) \
48 static void f_##sched##_1 (void *dummy) \
49 { \
50 int iam = omp_get_thread_num (); \
51 long s0, e0, i; \
52 if (GOMP_loop_##sched##_start (S, E, INCR, CHUNK, &s0, &e0)) \
53 do \
54 { \
55 for (i = s0; i < e0; i += INCR) \
56 set_data (i, iam); \
57 } \
58 while (GOMP_loop_##sched##_next (&s0, &e0)); \
59 GOMP_loop_end (); \
60 } \
61 static void t_##sched##_1 (void) \
62 { \
63 clean_data (); \
64 GOMP_parallel_start (f_##sched##_1, NULL, NTHR); \
65 f_##sched##_1 (NULL); \
66 GOMP_parallel_end (); \
67 test_data (); \
70 TMPL_1(static)
71 TMPL_1(dynamic)
72 TMPL_1(guided)
74 #define TMPL_2(sched) \
75 static void f_##sched##_2 (void *dummy) \
76 { \
77 int iam = omp_get_thread_num (); \
78 long s0, e0, i; \
79 while (GOMP_loop_##sched##_next (&s0, &e0)) \
80 { \
81 for (i = s0; i < e0; i += INCR) \
82 set_data (i, iam); \
83 } \
84 GOMP_loop_end_nowait (); \
85 } \
86 static void t_##sched##_2 (void) \
87 { \
88 clean_data (); \
89 GOMP_parallel_loop_##sched##_start \
90 (f_##sched##_2, NULL, NTHR, S, E, INCR, CHUNK); \
91 f_##sched##_2 (NULL); \
92 GOMP_parallel_end (); \
93 test_data (); \
96 TMPL_2(static)
97 TMPL_2(dynamic)
98 TMPL_2(guided)
100 static void test (void)
102 t_static_1 ();
103 t_dynamic_1 ();
104 t_guided_1 ();
105 t_static_2 ();
106 t_dynamic_2 ();
107 t_guided_2 ();
110 int main()
112 omp_set_dynamic (0);
114 NTHR = 4;
116 S = 0, E = N, INCR = 1, CHUNK = 4;
117 test ();
119 S = 0, E = N, INCR = 2, CHUNK = 4;
120 test ();
122 S = 1, E = N-1, INCR = 1, CHUNK = 5;
123 test ();
125 S = 1, E = N-1, INCR = 2, CHUNK = 5;
126 test ();
128 S = 2, E = 4, INCR = 1, CHUNK = 1;
129 test ();
131 S = 0, E = N, INCR = 1, CHUNK = 0;
132 t_static_1 ();
133 t_static_2 ();
135 S = 1, E = N-1, INCR = 1, CHUNK = 0;
136 t_static_1 ();
137 t_static_2 ();
139 return 0;