testsuite/libgomp.c++/loop-1.C

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <omp.h>
00005 
00006 #define MAX 1000
00007 
00008 void main1()
00009 {
00010   int i, N1, N2, step;
00011   int a[MAX], b[MAX];
00012 
00013   N1 = rand () % 13;
00014   N2 = rand () % (MAX - 51) + 50;
00015   step = rand () % 7 + 1;
00016 
00017   printf ("N1 = %d\nN2 = %d\nstep = %d\n", N1, N2, step);
00018 
00019   for (i = N1; i <= N2; i += step)
00020     a[i] = 42+ i;
00021 
00022   /* COUNTING UP (<).  Fill in array 'b' in parallel.  */
00023   memset (b, 0, sizeof b);
00024 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
00025   {
00026 #pragma omp for
00027     for (i = N1; i < N2; i += step)
00028       b[i] = a[i];
00029   }
00030 
00031   /* COUNTING UP (<).  Check that all the cells were filled in properly.  */
00032   for (i = N1; i < N2; i += step)
00033     if (a[i] != b[i])
00034       abort ();
00035 
00036   printf ("for (i = %d; i < %d; i += %d) [OK]\n", N1, N2, step);
00037 
00038   /* COUNTING UP (<=).  Fill in array 'b' in parallel.  */
00039   memset (b, 0, sizeof b);
00040 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
00041   {
00042 #pragma omp for
00043     for (i = N1; i <= N2; i += step)
00044       b[i] = a[i];
00045   }
00046 
00047   /* COUNTING UP (<=).  Check that all the cells were filled in properly.  */
00048   for (i = N1; i <= N2; i += step)
00049     if (a[i] != b[i])
00050       abort ();
00051 
00052   printf ("for (i = %d; i <= %d; i += %d) [OK]\n", N1, N2, step);
00053 
00054   /* COUNTING DOWN (>).  Fill in array 'b' in parallel.  */
00055   memset (b, 0, sizeof b);
00056 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
00057   {
00058 #pragma omp for
00059     for (i = N2; i > N1; i -= step)
00060       b[i] = a[i];
00061   }
00062 
00063   /* COUNTING DOWN (>).  Check that all the cells were filled in properly.  */
00064   for (i = N2; i > N1; i -= step)
00065     if (a[i] != b[i])
00066       abort ();
00067 
00068   printf ("for (i = %d; i > %d; i -= %d) [OK]\n", N2, N1, step);
00069 
00070   /* COUNTING DOWN (>=).  Fill in array 'b' in parallel.  */
00071   memset (b, 0, sizeof b);
00072 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
00073   {
00074 #pragma omp for
00075     for (i = N2; i >= N1; i -= step)
00076       b[i] = a[i];
00077   }
00078 
00079   /* COUNTING DOWN (>=).  Check that all the cells were filled in properly.  */
00080   for (i = N2; i >= N1; i -= step)
00081     if (a[i] != b[i])
00082       abort ();
00083 
00084   printf ("for (i = %d; i >= %d; i -= %d) [OK]\n", N2, N1, step);
00085 }
00086 
00087 int
00088 main ()
00089 {
00090   int i;
00091 
00092   srand (0);
00093   for (i = 0; i < 10; ++i)
00094     main1();
00095   return 0;
00096 }

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