aboutsummaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c-c++-common/reduction-1.c
blob: 89a4153b0784c65e3b64af2f0354387e48089e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* C / C++'s logical AND and OR operators take any scalar argument
   which compares (un)equal to 0 - the result 1 or 0 and of type int.

   In this testcase, the int result is again converted to a floating-poing
   or complex type.

   While having a floating-point/complex array element with || and && can make
   sense, having a non-integer/non-bool reduction variable is odd but valid.

   Test: FP reduction variable + FP array.  */

#define N 1024
_Complex float rcf[N];
_Complex double rcd[N];
float rf[N];
double rd[N];

int
reduction_or ()
{
  float orf = 0;
  double ord = 0;
  _Complex float orfc = 0;
  _Complex double ordc = 0;

  #pragma omp parallel reduction(||: orf)
  for (int i=0; i < N; ++i)
    orf = orf || rf[i];

  #pragma omp parallel for reduction(||: ord)
  for (int i=0; i < N; ++i)
    ord = ord || rcd[i];

  #pragma omp parallel for simd reduction(||: orfc)
  for (int i=0; i < N; ++i)
    orfc = orfc || rcf[i];

  #pragma omp parallel loop reduction(||: ordc)
  for (int i=0; i < N; ++i)
    ordc = ordc || rcd[i];

  return orf + ord + __real__ orfc + __real__ ordc;
}

int
reduction_or_teams ()
{
  float orf = 0;
  double ord = 0;
  _Complex float orfc = 0;
  _Complex double ordc = 0;

  #pragma omp teams distribute parallel for reduction(||: orf)
  for (int i=0; i < N; ++i)
    orf = orf || rf[i];

  #pragma omp teams distribute parallel for simd reduction(||: ord)
  for (int i=0; i < N; ++i)
    ord = ord || rcd[i];

  #pragma omp teams distribute parallel for reduction(||: orfc)
  for (int i=0; i < N; ++i)
    orfc = orfc || rcf[i];

  #pragma omp teams distribute parallel for simd reduction(||: ordc)
  for (int i=0; i < N; ++i)
    ordc = ordc || rcd[i];

  return orf + ord + __real__ orfc + __real__ ordc;
}

int
reduction_and ()
{
  float andf = 1;
  double andd = 1;
  _Complex float andfc = 1;
  _Complex double anddc = 1;

  #pragma omp parallel reduction(&&: andf)
  for (int i=0; i < N; ++i)
    andf = andf && rf[i];

  #pragma omp parallel for reduction(&&: andd)
  for (int i=0; i < N; ++i)
    andd = andd && rcd[i];

  #pragma omp parallel for simd reduction(&&: andfc)
  for (int i=0; i < N; ++i)
    andfc = andfc && rcf[i];

  #pragma omp parallel loop reduction(&&: anddc)
  for (int i=0; i < N; ++i)
    anddc = anddc && rcd[i];

  return andf + andd + __real__ andfc + __real__ anddc;
}

int
reduction_and_teams ()
{
  float andf = 1;
  double andd = 1;
  _Complex float andfc = 1;
  _Complex double anddc = 1;

  #pragma omp teams distribute parallel for reduction(&&: andf)
  for (int i=0; i < N; ++i)
    andf = andf && rf[i];

  #pragma omp teams distribute parallel for simd reduction(&&: andd)
  for (int i=0; i < N; ++i)
    andd = andd && rcd[i];

  #pragma omp teams distribute parallel for reduction(&&: andfc)
  for (int i=0; i < N; ++i)
    andfc = andfc && rcf[i];

  #pragma omp teams distribute parallel for simd reduction(&&: anddc)
  for (int i=0; i < N; ++i)
    anddc = anddc && rcd[i];

  return andf + andd + __real__ andfc + __real__ anddc;
}

int
main ()
{
  for (int i = 0; i < N; ++i)
    {
      rf[i] = 0;
      rd[i] = 0;
      rcf[i] = 0;
      rcd[i] = 0;
    }

  if (reduction_or () != 0)
    __builtin_abort ();
  if (reduction_or_teams () != 0)
    __builtin_abort ();
  if (reduction_and () != 0)
    __builtin_abort ();
  if (reduction_and_teams () != 0)
    __builtin_abort ();

  rf[10] = 1.0;
  rd[15] = 1.0;
  rcf[10] = 1.0;
  rcd[15] = 1.0i;

  if (reduction_or () != 4)
    __builtin_abort ();
  if (reduction_or_teams () != 4)
    __builtin_abort ();
  if (reduction_and () != 0)
    __builtin_abort ();
  if (reduction_and_teams () != 0)
    __builtin_abort ();

  for (int i = 0; i < N; ++i)
    {
      rf[i] = 1;
      rd[i] = 1;
      rcf[i] = 1;
      rcd[i] = 1;
    }

  if (reduction_or () != 4)
    __builtin_abort ();
  if (reduction_or_teams () != 4)
    __builtin_abort ();
  if (reduction_and () != 4)
    __builtin_abort ();
  if (reduction_and_teams () != 4)
    __builtin_abort ();

  rf[10] = 0.0;
  rd[15] = 0.0;
  rcf[10] = 0.0;
  rcd[15] = 0.0;

  if (reduction_or () != 4)
    __builtin_abort ();
  if (reduction_or_teams () != 4)
    __builtin_abort ();
  if (reduction_and () != 0)
    __builtin_abort ();
  if (reduction_and_teams () != 0)
    __builtin_abort ();

  return 0;
}