testsuite/libgomp.c/loop-2.c

Go to the documentation of this file.
00001 /* Validate static scheduling iteration dispatch.  We only test with
00002    even thread distributions here; there are multiple valid solutions
00003    for uneven thread distributions.  */
00004 
00005 /* { dg-require-effective-target sync_int_long } */
00006 
00007 #include <omp.h>
00008 #include <string.h>
00009 #include <assert.h>
00010 #include "libgomp_g.h"
00011 
00012 
00013 #define N 360
00014 static int data[N][2];
00015 static int INCR, NTHR, CHUNK;
00016 
00017 static void clean_data (void)
00018 {
00019   memset (data, -1, sizeof (data));
00020 }
00021 
00022 static void test_data (void)
00023 {
00024   int n, i, c, thr, iter, chunk;
00025 
00026   chunk = CHUNK;
00027   if (chunk == 0)
00028     chunk = N / INCR / NTHR;
00029 
00030   thr = iter = c = i = 0;
00031 
00032   for (n = 0; n < N; ++n)
00033     {
00034       if (i == 0)
00035     {
00036       assert (data[n][0] == thr);
00037       assert (data[n][1] == iter);
00038     }
00039       else
00040     {
00041       assert (data[n][0] == -1);
00042       assert (data[n][1] == -1);
00043     }
00044 
00045       if (++i == INCR)
00046     {
00047       i = 0;
00048       if (++c == chunk)
00049         {
00050           c = 0;
00051           if (++thr == NTHR)
00052         {
00053           thr = 0;
00054           ++iter;
00055         }
00056         }
00057     }
00058     }
00059 }
00060 
00061 static void set_data (long i, int thr, int iter)
00062 {
00063   int old;
00064   assert (i >= 0 && i < N);
00065   old = __sync_lock_test_and_set (&data[i][0], thr);
00066   assert (old == -1);
00067   old = __sync_lock_test_and_set (&data[i][1], iter);
00068   assert (old == -1);
00069 }
00070   
00071 static void f_static_1 (void *dummy)
00072 {
00073   int iam = omp_get_thread_num ();
00074   long s0, e0, i, count = 0;
00075   if (GOMP_loop_static_start (0, N, INCR, CHUNK, &s0, &e0))
00076     do
00077       {
00078     for (i = s0; i < e0; i += INCR)
00079       set_data (i, iam, count);
00080     ++count;    
00081       }
00082     while (GOMP_loop_static_next (&s0, &e0));
00083   GOMP_loop_end ();
00084 }
00085 
00086 static void test (void)
00087 {
00088   clean_data ();
00089   GOMP_parallel_start (f_static_1, NULL, NTHR);
00090   f_static_1 (NULL);
00091   GOMP_parallel_end ();
00092   test_data ();
00093 }
00094 
00095 int main()
00096 {
00097   omp_set_dynamic (0);
00098 
00099   NTHR = 5;
00100 
00101   INCR = 1, CHUNK = 0;  /* chunk = 360 / 5 = 72 */
00102   test ();
00103 
00104   INCR = 4, CHUNK = 0;  /* chunk = 360 / 4 / 5 = 18 */
00105   test ();
00106 
00107   INCR = 1, CHUNK = 4;  /* 1 * 4 * 5 = 20 -> 360 / 20 = 18 iterations.  */
00108   test ();
00109 
00110   INCR = 3, CHUNK = 4;  /* 3 * 4 * 5 = 60 -> 360 / 60 = 6 iterations.  */
00111   test ();
00112 
00113   return 0;
00114 }

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