1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <omp.h>
5
6#define MAX	1000
7
8void main1()
9{
10  int i, N1, N2, step;
11  int a[MAX], b[MAX];
12
13  N1 = rand () % 13;
14  N2 = rand () % (MAX - 51) + 50;
15  step = rand () % 7 + 1;
16
17  printf ("N1 = %d\nN2 = %d\nstep = %d\n", N1, N2, step);
18
19  for (i = N1; i <= N2; i += step)
20    a[i] = 42+ i;
21
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)
25  {
26#pragma omp for
27    for (i = N1; i < N2; i += step)
28      b[i] = a[i];
29  }
30
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 ();
35
36  printf ("for (i = %d; i < %d; i += %d) [OK]\n", N1, N2, step);
37
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)
41  {
42#pragma omp for
43    for (i = N1; i <= N2; i += step)
44      b[i] = a[i];
45  }
46
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 ();
51
52  printf ("for (i = %d; i <= %d; i += %d) [OK]\n", N1, N2, step);
53
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)
57  {
58#pragma omp for
59    for (i = N2; i > N1; i -= step)
60      b[i] = a[i];
61  }
62
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 ();
67
68  printf ("for (i = %d; i > %d; i -= %d) [OK]\n", N2, N1, step);
69
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)
73  {
74#pragma omp for
75    for (i = N2; i >= N1; i -= step)
76      b[i] = a[i];
77  }
78
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 ();
83
84  printf ("for (i = %d; i >= %d; i -= %d) [OK]\n", N2, N1, step);
85}
86
87int
88main ()
89{
90  int i;
91
92  srand (0);
93  for (i = 0; i < 10; ++i)
94    main1();
95  return 0;
96}
97