OSDN Git Service

2006-01-18 Richard Henderson <rth@redhat.com>
[pf3gnuchains/gcc-fork.git] / libgomp / testsuite / libgomp.c / loop-2.c
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.  */
4
5 #include <omp.h>
6 #include <string.h>
7 #include <assert.h>
8 #include "libgomp_g.h"
9
10
11 #define N 360
12 static int data[N][2];
13 static int INCR, NTHR, CHUNK;
14
15 static void clean_data (void)
16 {
17   memset (data, -1, sizeof (data));
18 }
19
20 static void test_data (void)
21 {
22   int n, i, c, thr, iter, chunk;
23
24   chunk = CHUNK;
25   if (chunk == 0)
26     chunk = N / INCR / NTHR;
27
28   thr = iter = c = i = 0;
29
30   for (n = 0; n < N; ++n)
31     {
32       if (i == 0)
33         {
34           assert (data[n][0] == thr);
35           assert (data[n][1] == iter);
36         }
37       else
38         {
39           assert (data[n][0] == -1);
40           assert (data[n][1] == -1);
41         }
42
43       if (++i == INCR)
44         {
45           i = 0;
46           if (++c == chunk)
47             {
48               c = 0;
49               if (++thr == NTHR)
50                 {
51                   thr = 0;
52                   ++iter;
53                 }
54             }
55         }
56     }
57 }
58
59 static void set_data (long i, int thr, int iter)
60 {
61   int old;
62   assert (i >= 0 && i < N);
63   old = __sync_lock_test_and_set (&data[i][0], thr);
64   assert (old == -1);
65   old = __sync_lock_test_and_set (&data[i][1], iter);
66   assert (old == -1);
67 }
68   
69 static void f_static_1 (void *dummy)
70 {
71   int iam = omp_get_thread_num ();
72   long s0, e0, i, count = 0;
73   if (GOMP_loop_static_start (0, N, INCR, CHUNK, &s0, &e0))
74     do
75       {
76         for (i = s0; i < e0; i += INCR)
77           set_data (i, iam, count);
78         ++count;        
79       }
80     while (GOMP_loop_static_next (&s0, &e0));
81   GOMP_loop_end ();
82 }
83
84 static void test (void)
85 {
86   clean_data ();
87   GOMP_parallel_start (f_static_1, NULL, NTHR);
88   f_static_1 (NULL);
89   GOMP_parallel_end ();
90   test_data ();
91 }
92
93 int main()
94 {
95   omp_set_dynamic (0);
96
97   NTHR = 5;
98
99   INCR = 1, CHUNK = 0;  /* chunk = 360 / 5 = 72 */
100   test ();
101
102   INCR = 4, CHUNK = 0;  /* chunk = 360 / 4 / 5 = 18 */
103   test ();
104
105   INCR = 1, CHUNK = 4;  /* 1 * 4 * 5 = 20 -> 360 / 20 = 18 iterations.  */
106   test ();
107
108   INCR = 3, CHUNK = 4;  /* 3 * 4 * 5 = 60 -> 360 / 60 = 6 iterations.  */
109   test ();
110
111   return 0;
112 }