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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/* "Function scope" (top-level block scope) 'static' variables
... inside OpenACC compute construct regions as well as OpenACC 'routine'.
This is to document/verify aspects of GCC's observed behavior, not
necessarily as it's (intended to be?) restricted by the OpenACC
specification. See also PR84991, PR84992, PR90779 etc., and
<https://github.com/OpenACC/openacc-spec/issues/372> "C/C++ 'static'
variables" (only visible to members of the GitHub OpenACC organization).
*/
#undef NDEBUG
#include <assert.h>
#include <string.h>
#include <openacc.h>
#include <gomp-constants.h>
#define IF_DEBUG if (0)
/* Without explicit 'num_gangs'. */
static void t0_c(void)
{
IF_DEBUG
__builtin_printf ("%s\n", __FUNCTION__);
const int i_limit = 11;
const int var_init = 16;
for (int i = 0; i < i_limit; ++i)
{
int result = 0;
int num_gangs_actual = -1;
#pragma acc parallel \
reduction(max:num_gangs_actual) \
reduction(max:result)
{
num_gangs_actual = 1 + __builtin_goacc_parlevel_id(GOMP_DIM_GANG);
static int var = var_init;
#pragma acc atomic capture
result = ++var;
/* Irrespective of the order in which the gang-redundant threads
execute, 'var' has now been incremented 'num_gangs_actual' times, and
the final value captured as 'result'. */
}
/* Without an explicit 'num_gangs' clause GCC assigns 'num_gangs(1)'
because it doesn't see any use of gang-level parallelism inside the
region. */
assert(num_gangs_actual == 1);
assert(result == var_init + num_gangs_actual * (1 + i));
}
}
/* Call a gang-level routine. */
static const int t0_r_var_init = 61;
#pragma acc routine gang
__attribute__((noinline))
static int t0_r_r(void)
{
static int var = t0_r_var_init;
int tmp;
#pragma acc atomic capture
tmp = ++var;
return tmp;
}
static void t0_r(void)
{
IF_DEBUG
__builtin_printf ("%s\n", __FUNCTION__);
const int i_limit = 11;
for (int i = 0; i < i_limit; ++i)
{
int result = 0;
int num_gangs_actual = -1;
#pragma acc parallel \
reduction(max:num_gangs_actual) \
reduction(max:result)
{
num_gangs_actual = 1 + __builtin_goacc_parlevel_id(GOMP_DIM_GANG);
result = t0_r_r();
/* Irrespective of the order in which the gang-redundant threads
execute, 'var' has now been incremented 'num_gangs_actual' times, and
the final value captured as 'result'. */
}
/* The number of gangs selected by the implemention ought to but must not
be bigger than one. */
IF_DEBUG
__builtin_printf ("%d: num_gangs_actual: %d\n", i, num_gangs_actual);
assert(num_gangs_actual >= 1);
assert(result == t0_r_var_init + num_gangs_actual * (1 + i));
}
}
/* Explicit 'num_gangs'. */
static void t1_c(void)
{
IF_DEBUG
__builtin_printf ("%s\n", __FUNCTION__);
const int i_limit = 22;
const int num_gangs_request = 444;
const int var_init = 5;
for (int i = 0; i < i_limit; ++i)
{
int result = 0;
int num_gangs_actual = -1;
#pragma acc parallel \
num_gangs(num_gangs_request) \
reduction(max:num_gangs_actual) \
reduction(max:result)
{
num_gangs_actual = 1 + __builtin_goacc_parlevel_id(GOMP_DIM_GANG);
static int var = var_init;
#pragma acc atomic capture
result = ++var;
/* Irrespective of the order in which the gang-redundant threads
execute, 'var' has now been incremented 'num_gangs_actual' times, and
the final value captured as 'result'. */
}
if (acc_get_device_type() == acc_device_host)
assert(num_gangs_actual == 1);
else
assert(num_gangs_actual == num_gangs_request);
assert(result == var_init + num_gangs_actual * (1 + i));
}
}
/* Check the same routine called from two compute constructs. */
static const int t1_r2_var_init = 166;
#pragma acc routine gang
__attribute__((noinline))
static int t1_r2_r(void)
{
static int var = t1_r2_var_init;
int tmp;
#pragma acc atomic capture
tmp = ++var;
return tmp;
}
static void t1_r2(void)
{
IF_DEBUG
__builtin_printf ("%s\n", __FUNCTION__);
const int i_limit = 71;
/* The checking assumes the same 'num_gangs' for all compute constructs. */
const int num_gangs_request = 333;
int num_gangs_actual = -1;
if (acc_get_device_type() == acc_device_host)
num_gangs_actual = 1;
else
{
/* We're assuming that the implementation is able to accomodate the
'num_gangs' requested (which really ought to be true for
'num_gangs'). */
num_gangs_actual = num_gangs_request;
}
for (int i = 0; i < i_limit; ++i)
{
int result_1 = 0;
#pragma acc parallel \
num_gangs(num_gangs_request) \
reduction(max:result_1)
{
result_1 = t1_r2_r();
/* Irrespective of the order in which the gang-redundant threads
execute, 'var' has now been incremented 'num_gangs_actual' times, and
the final value captured as 'result_1'. */
}
IF_DEBUG
__builtin_printf ("%d: result_1: %d\n", i, result_1);
assert(result_1 == t1_r2_var_init + num_gangs_actual * (1 + (i * 3 + 0)));
int result_2 = 0;
#pragma acc parallel \
num_gangs(num_gangs_request) \
reduction(max:result_2)
{
result_2 = t1_r2_r() + t1_r2_r();
/* Irrespective of the order in which the gang-redundant threads
execute, 'var' has now been incremented '2 * num_gangs_actual' times.
However, the order of the two 't1_r2_r' function calls is not
synchronized (between different gang-redundant threads). We thus
cannot verify the actual 'result_2' values in this case. */
}
IF_DEBUG
__builtin_printf ("%d: result_2: %d\n", i, result_2);
if (num_gangs_actual == 1)
/* Per the rationale above, only in this case we can check the actual
result. */
assert(result_2 == (t1_r2_var_init + num_gangs_actual * (1 + (i * 3 + 1))
+ t1_r2_var_init + num_gangs_actual * (1 + (i * 3 + 2))));
/* But we can generally check low and high limits. */
{
/* Must be bigger than '2 * result_1'. */
int c = 2 * result_1;
IF_DEBUG
__builtin_printf (" > %d\n", c);
assert(result_2 > c);
}
{
/* ..., but limited by the base value for next 'i'. */
int c = 2 * (t1_r2_var_init + num_gangs_actual * (0 + ((i + 1) * 3 + 0)));
IF_DEBUG
__builtin_printf (" < %d\n", c);
assert(result_2 < c);
}
}
}
/* Asynchronous execution. */
static const int t2_var_init_2 = -55;
#pragma acc routine gang
__attribute__((noinline))
static int t2_r(void)
{
static int var = t2_var_init_2;
int tmp;
#pragma acc atomic capture
tmp = ++var;
return tmp;
}
static void t2(void)
{
IF_DEBUG
__builtin_printf ("%s\n", __FUNCTION__);
const int i_limit = 12;
const int num_gangs_request_1 = 14;
const int var_init_1 = 5;
int results_1[i_limit][num_gangs_request_1];
memset (results_1, 0, sizeof results_1);
const int num_gangs_request_2 = 5;
int results_2[i_limit][num_gangs_request_2];
memset (results_2, 0, sizeof results_2);
const int num_gangs_request_3 = 34;
const int var_init_3 = 1250;
int results_3[i_limit][num_gangs_request_3];
memset (results_3, 0, sizeof results_3);
#pragma acc data \
copy(results_1, results_2, results_3)
{
for (int i = 0; i < i_limit; ++i)
{
/* The following 'async' clauses effect asynchronous execution, but
using the same async-argument for each compute construct implies that
the respective compute constructs' execution is synchronized with
itself, meaning that all 'i = 0' execution has finished (on the
device) before 'i = 1' is started (on the device), etc. */
#pragma acc parallel \
present(results_1) \
num_gangs(num_gangs_request_1) \
async(1)
{
static int var = var_init_1;
int tmp;
#pragma acc atomic capture
tmp = ++var;
results_1[i][__builtin_goacc_parlevel_id(GOMP_DIM_GANG)] += tmp;
}
#pragma acc parallel \
present(results_2) \
num_gangs(num_gangs_request_2) \
async(2)
{
results_2[i][__builtin_goacc_parlevel_id(GOMP_DIM_GANG)] += t2_r();
}
#pragma acc parallel \
present(results_3) \
num_gangs(num_gangs_request_3) \
async(3)
{
static int var = var_init_3;
int tmp;
#pragma acc atomic capture
tmp = ++var;
results_3[i][__builtin_goacc_parlevel_id(GOMP_DIM_GANG)] += tmp;
}
}
#pragma acc wait
}
int num_gangs_actual_1;
int num_gangs_actual_2;
int num_gangs_actual_3;
if (acc_get_device_type() == acc_device_host)
{
num_gangs_actual_1 = 1;
num_gangs_actual_2 = 1;
num_gangs_actual_3 = 1;
}
else
{
/* We're assuming that the implementation is able to accomodate the
'num_gangs' requested (which really ought to be true for
'num_gangs'). */
num_gangs_actual_1 = num_gangs_request_1;
num_gangs_actual_2 = num_gangs_request_2;
num_gangs_actual_3 = num_gangs_request_3;
}
/* For 'i = 0', 'results_*[i][0..num_gangs_actual_*]' are expected to each
contain one value of '(1 + var_init_*)..(var_init_* + num_gangs_actual_*)',
and so on for increasing 'i'. Their order however is unspecified due to
the gang-redundant execution. (Thus checking that their sums match.) */
int result_1 = 0;
int result_2 = 0;
int result_3 = 0;
for (int i = 0; i < i_limit; ++i)
{
int result_1_ = 0;
for (int g = 0; g < num_gangs_actual_1; ++g)
{
IF_DEBUG
__builtin_printf ("results_1[%d][%d]: %d\n", i, g, results_1[i][g]);
result_1_ += results_1[i][g];
}
IF_DEBUG
__builtin_printf ("%d result_1_: %d\n", i, result_1_);
assert (result_1_ == (((var_init_1 + num_gangs_actual_1 * (1 + i)) * (1 + var_init_1 + num_gangs_actual_1 * (1 + i)) / 2)
- ((var_init_1 + num_gangs_actual_1 * (0 + i)) * (1 + var_init_1 + num_gangs_actual_1 * (0 + i)) / 2)));
result_1 += result_1_;
int result_2_ = 0;
for (int g = 0; g < num_gangs_actual_2; ++g)
{
IF_DEBUG
__builtin_printf ("results_2[%d][%d]: %d\n", i, g, results_2[i][g]);
result_2_ += results_2[i][g];
}
IF_DEBUG
__builtin_printf ("%d result_2_: %d\n", i, result_2_);
assert (result_2_ == (((t2_var_init_2 + num_gangs_actual_2 * (1 + i)) * (1 + t2_var_init_2 + num_gangs_actual_2 * (1 + i)) / 2)
- ((t2_var_init_2 + num_gangs_actual_2 * (0 + i)) * (1 + t2_var_init_2 + num_gangs_actual_2 * (0 + i)) / 2)));
result_2 += result_2_;
int result_3_ = 0;
for (int g = 0; g < num_gangs_actual_3; ++g)
{
IF_DEBUG
__builtin_printf ("results_3[%d][%d]: %d\n", i, g, results_3[i][g]);
result_3_ += results_3[i][g];
}
IF_DEBUG
__builtin_printf ("%d result_3_: %d\n", i, result_3_);
assert (result_3_ == (((var_init_3 + num_gangs_actual_3 * (1 + i)) * (1 + var_init_3 + num_gangs_actual_3 * (1 + i)) / 2)
- ((var_init_3 + num_gangs_actual_3 * (0 + i)) * (1 + var_init_3 + num_gangs_actual_3 * (0 + i)) / 2)));
result_3 += result_3_;
}
IF_DEBUG
__builtin_printf ("result_1: %d\n", result_1);
assert (result_1 == (((var_init_1 + num_gangs_actual_1 * i_limit) * (1 + var_init_1 + num_gangs_actual_1 * i_limit) / 2)
- (var_init_1 * (var_init_1 + 1) / 2)));
IF_DEBUG
__builtin_printf ("result_2: %d\n", result_2);
assert (result_2 == (((t2_var_init_2 + num_gangs_actual_2 * i_limit) * (1 + t2_var_init_2 + num_gangs_actual_2 * i_limit) / 2)
- (t2_var_init_2 * (t2_var_init_2 + 1) / 2)));
IF_DEBUG
__builtin_printf ("result_3: %d\n", result_3);
assert (result_3 == (((var_init_3 + num_gangs_actual_3 * i_limit) * (1 + var_init_3 + num_gangs_actual_3 * i_limit) / 2)
- (var_init_3 * (var_init_3 + 1) / 2)));
}
#pragma acc routine seq
__attribute__((noinline))
static int pr84991_1_r_s(int n)
{
static const int test[] = {1,2,3,4};
return test[n];
}
static void pr84991_1(void)
{
int n[1];
n[0] = 3;
#pragma acc parallel copy(n)
{
n[0] = pr84991_1_r_s(n[0]);
}
assert(n[0] == 4);
}
static void pr84992_1(void)
{
int n[1];
n[0] = 3;
#pragma acc parallel copy(n)
{
static const int test[] = {1,2,3,4};
n[0] = test[n[0]];
}
assert(n[0] == 4);
}
int main(void)
{
t0_c();
t0_r();
t1_c();
t1_r2();
t2();
pr84991_1();
pr84992_1();
return 0;
}
|