testsuite/libgomp.c/loop-1.c

Go to the documentation of this file.
00001 /* Test that all loop iterations are touched.  This doesn't verify 
00002    scheduling order, merely coverage.  */
00003 
00004 /* { dg-require-effective-target sync_int_long } */
00005 
00006 #include <omp.h>
00007 #include <string.h>
00008 #include <assert.h>
00009 #include "libgomp_g.h"
00010 
00011 
00012 #define N 10000
00013 static int S, E, INCR, CHUNK, NTHR;
00014 static int data[N];
00015 
00016 static void clean_data (void)
00017 {
00018   memset (data, -1, sizeof (data));
00019 }
00020 
00021 static void test_data (void)
00022 {
00023   int i, j;
00024 
00025   for (i = 0; i < S; ++i)
00026     assert (data[i] == -1);
00027 
00028   for (j = 0; i < E; ++i, j = (j + 1) % INCR)
00029     if (j == 0)
00030       assert (data[i] != -1);
00031     else
00032       assert (data[i] == -1);
00033 
00034   for (; i < N; ++i)
00035     assert (data[i] == -1);
00036 }
00037 
00038 static void set_data (long i, int val)
00039 {
00040   int old;
00041   assert (i >= 0 && i < N);
00042   old = __sync_lock_test_and_set (data+i, val);
00043   assert (old == -1);
00044 }
00045   
00046 
00047 #define TMPL_1(sched)                       \
00048 static void f_##sched##_1 (void *dummy)             \
00049 {                               \
00050   int iam = omp_get_thread_num ();              \
00051   long s0, e0, i;                       \
00052   if (GOMP_loop_##sched##_start (S, E, INCR, CHUNK, &s0, &e0))  \
00053     do                              \
00054       {                             \
00055     for (i = s0; i < e0; i += INCR)             \
00056       set_data (i, iam);                    \
00057       }                             \
00058     while (GOMP_loop_##sched##_next (&s0, &e0));        \
00059   GOMP_loop_end ();                     \
00060 }                               \
00061 static void t_##sched##_1 (void)                \
00062 {                               \
00063   clean_data ();                        \
00064   GOMP_parallel_start (f_##sched##_1, NULL, NTHR);      \
00065   f_##sched##_1 (NULL);                     \
00066   GOMP_parallel_end ();                     \
00067   test_data ();                         \
00068 }
00069 
00070 TMPL_1(static)
00071 TMPL_1(dynamic)
00072 TMPL_1(guided)
00073 
00074 #define TMPL_2(sched)                   \
00075 static void f_##sched##_2 (void *dummy)         \
00076 {                           \
00077   int iam = omp_get_thread_num ();          \
00078   long s0, e0, i;                   \
00079   while (GOMP_loop_##sched##_next (&s0, &e0))       \
00080     {                           \
00081       for (i = s0; i < e0; i += INCR)           \
00082     set_data (i, iam);              \
00083     }                           \
00084   GOMP_loop_end_nowait ();              \
00085 }                           \
00086 static void t_##sched##_2 (void)            \
00087 {                           \
00088   clean_data ();                    \
00089   GOMP_parallel_loop_##sched##_start            \
00090     (f_##sched##_2, NULL, NTHR, S, E, INCR, CHUNK); \
00091   f_##sched##_2 (NULL);                 \
00092   GOMP_parallel_end ();                 \
00093   test_data ();                     \
00094 }
00095 
00096 TMPL_2(static)
00097 TMPL_2(dynamic)
00098 TMPL_2(guided)
00099 
00100 static void test (void)
00101 {
00102   t_static_1 ();
00103   t_dynamic_1 ();
00104   t_guided_1 ();
00105   t_static_2 ();
00106   t_dynamic_2 ();
00107   t_guided_2 ();
00108 }
00109 
00110 int main()
00111 {
00112   omp_set_dynamic (0);
00113 
00114   NTHR = 4;
00115 
00116   S = 0, E = N, INCR = 1, CHUNK = 4;
00117   test ();
00118 
00119   S = 0, E = N, INCR = 2, CHUNK = 4;
00120   test ();
00121 
00122   S = 1, E = N-1, INCR = 1, CHUNK = 5;
00123   test ();
00124 
00125   S = 1, E = N-1, INCR = 2, CHUNK = 5;
00126   test ();
00127 
00128   S = 2, E = 4, INCR = 1, CHUNK = 1;
00129   test ();
00130 
00131   S = 0, E = N, INCR = 1, CHUNK = 0;
00132   t_static_1 ();
00133   t_static_2 ();
00134 
00135   S = 1, E = N-1, INCR = 1, CHUNK = 0;
00136   t_static_1 ();
00137   t_static_2 ();
00138 
00139   return 0;
00140 }

Generated on Fri Apr 5 05:38:10 2013 for Libgomp by  doxygen 1.4.7