OSDN Git Service

a90a8e601c7cb70aa0cf42da8d170d0f64c3cae5
[pf3gnuchains/gcc-fork.git] / libgomp / testsuite / libgomp.c / loop-1.c
1 /* Test that all loop iterations are touched.  This doesn't verify 
2    scheduling order, merely coverage.  */
3
4 #include <omp.h>
5 #include <string.h>
6 #include <assert.h>
7 #include "libgomp_g.h"
8
9
10 #define N 10000
11 static int S, E, INCR, CHUNK, NTHR;
12 static int data[N];
13
14 static void clean_data (void)
15 {
16   memset (data, -1, sizeof (data));
17 }
18
19 static void test_data (void)
20 {
21   int i, j;
22
23   for (i = 0; i < S; ++i)
24     assert (data[i] == -1);
25
26   for (j = 0; i < E; ++i, j = (j + 1) % INCR)
27     if (j == 0)
28       assert (data[i] != -1);
29     else
30       assert (data[i] == -1);
31
32   for (; i < N; ++i)
33     assert (data[i] == -1);
34 }
35
36 static void set_data (long i, int val)
37 {
38   int old;
39   assert (i >= 0 && i < N);
40   old = __sync_lock_test_and_set (data+i, val);
41   assert (old == -1);
42 }
43   
44
45 #define TMPL_1(sched)                                           \
46 static void f_##sched##_1 (void *dummy)                         \
47 {                                                               \
48   int iam = omp_get_thread_num ();                              \
49   long s0, e0, i;                                               \
50   if (GOMP_loop_##sched##_start (S, E, INCR, CHUNK, &s0, &e0))  \
51     do                                                          \
52       {                                                         \
53         for (i = s0; i < e0; i += INCR)                         \
54           set_data (i, iam);                                    \
55       }                                                         \
56     while (GOMP_loop_##sched##_next (&s0, &e0));                \
57   GOMP_loop_end ();                                             \
58 }                                                               \
59 static void t_##sched##_1 (void)                                \
60 {                                                               \
61   clean_data ();                                                \
62   GOMP_parallel_start (f_##sched##_1, NULL, NTHR);              \
63   f_##sched##_1 (NULL);                                         \
64   GOMP_parallel_end ();                                         \
65   test_data ();                                                 \
66 }
67
68 TMPL_1(static)
69 TMPL_1(dynamic)
70 TMPL_1(guided)
71
72 #define TMPL_2(sched)                                   \
73 static void f_##sched##_2 (void *dummy)                 \
74 {                                                       \
75   int iam = omp_get_thread_num ();                      \
76   long s0, e0, i;                                       \
77   while (GOMP_loop_##sched##_next (&s0, &e0))           \
78     {                                                   \
79       for (i = s0; i < e0; i += INCR)                   \
80         set_data (i, iam);                              \
81     }                                                   \
82   GOMP_loop_end_nowait ();                              \
83 }                                                       \
84 static void t_##sched##_2 (void)                        \
85 {                                                       \
86   clean_data ();                                        \
87   GOMP_parallel_loop_##sched##_start                    \
88     (f_##sched##_2, NULL, NTHR, S, E, INCR, CHUNK);     \
89   f_##sched##_2 (NULL);                                 \
90   GOMP_parallel_end ();                                 \
91   test_data ();                                         \
92 }
93
94 TMPL_2(static)
95 TMPL_2(dynamic)
96 TMPL_2(guided)
97
98 static void test (void)
99 {
100   t_static_1 ();
101   t_dynamic_1 ();
102   t_guided_1 ();
103   t_static_2 ();
104   t_dynamic_2 ();
105   t_guided_2 ();
106 }
107
108 int main()
109 {
110   omp_set_dynamic (0);
111
112   NTHR = 4;
113
114   S = 0, E = N, INCR = 1, CHUNK = 4;
115   test ();
116
117   S = 0, E = N, INCR = 2, CHUNK = 4;
118   test ();
119
120   S = 1, E = N-1, INCR = 1, CHUNK = 5;
121   test ();
122
123   S = 1, E = N-1, INCR = 2, CHUNK = 5;
124   test ();
125
126   S = 2, E = 4, INCR = 1, CHUNK = 1;
127   test ();
128
129   S = 0, E = N, INCR = 1, CHUNK = 0;
130   t_static_1 ();
131   t_static_2 ();
132
133   S = 1, E = N-1, INCR = 1, CHUNK = 0;
134   t_static_1 ();
135   t_static_2 ();
136
137   return 0;
138 }