Merge from mainline
[official-gcc.git] / libgomp / testsuite / libgomp.c / omp-loop01.c
blob0e83c95832f1d40aafbcfa775fac5490280046e2
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <omp.h>
6 #define MAX 1000
8 void main1()
10 int i, N1, N2, step;
11 int a[MAX], b[MAX];
13 N1 = rand () % 13;
14 N2 = rand () % (MAX - 51) + 50;
15 step = rand () % 7 + 1;
17 printf ("N1 = %d\nN2 = %d\nstep = %d\n", N1, N2, step);
19 for (i = N1; i <= N2; i += step)
20 a[i] = 42+ i;
22 /* COUNTING UP (<). Fill in array 'b' in parallel. */
23 memset (b, 0, sizeof b);
24 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
26 #pragma omp for
27 for (i = N1; i < N2; i += step)
28 b[i] = a[i];
31 /* COUNTING UP (<). Check that all the cells were filled in properly. */
32 for (i = N1; i < N2; i += step)
33 if (a[i] != b[i])
34 abort ();
36 printf ("for (i = %d; i < %d; i += %d) [OK]\n", N1, N2, step);
38 /* COUNTING UP (<=). Fill in array 'b' in parallel. */
39 memset (b, 0, sizeof b);
40 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
42 #pragma omp for
43 for (i = N1; i <= N2; i += step)
44 b[i] = a[i];
47 /* COUNTING UP (<=). Check that all the cells were filled in properly. */
48 for (i = N1; i <= N2; i += step)
49 if (a[i] != b[i])
50 abort ();
52 printf ("for (i = %d; i <= %d; i += %d) [OK]\n", N1, N2, step);
54 /* COUNTING DOWN (>). Fill in array 'b' in parallel. */
55 memset (b, 0, sizeof b);
56 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
58 #pragma omp for
59 for (i = N2; i > N1; i -= step)
60 b[i] = a[i];
63 /* COUNTING DOWN (>). Check that all the cells were filled in properly. */
64 for (i = N2; i > N1; i -= step)
65 if (a[i] != b[i])
66 abort ();
68 printf ("for (i = %d; i > %d; i -= %d) [OK]\n", N2, N1, step);
70 /* COUNTING DOWN (>=). Fill in array 'b' in parallel. */
71 memset (b, 0, sizeof b);
72 #pragma omp parallel shared(a,b,N1,N2,step) private(i)
74 #pragma omp for
75 for (i = N2; i >= N1; i -= step)
76 b[i] = a[i];
79 /* COUNTING DOWN (>=). Check that all the cells were filled in properly. */
80 for (i = N2; i >= N1; i -= step)
81 if (a[i] != b[i])
82 abort ();
84 printf ("for (i = %d; i >= %d; i -= %d) [OK]\n", N2, N1, step);
87 int
88 main ()
90 int i;
92 srand (0);
93 for (i = 0; i < 10; ++i)
94 main1();
95 return 0;