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
| // RUN: %libomp-cxx-compile-and-run
// RUN: %libomp-cxx-compile -DFLG=1 && %libomp-run
// GCC-5 is needed for OpenMP 4.0 support (taskgroup)
// XFAIL: gcc-4
#include <cstdio>
#include <cmath>
#include <cassert>
#include <omp.h>
// Total number of loop iterations, should be multiple of T for this test
#define N 10000
// Flag to request lazy (1) or eager (0) allocation of reduction objects
#ifndef FLG
#define FLG 0
#endif
/*
// initial user's code that corresponds to pseudo code of the test
#pragma omp taskgroup task_reduction(+:i,j) task_reduction(*:x)
{
for( int l = 0; l < N; ++l ) {
#pragma omp task firstprivate(l) in_reduction(+:i) in_reduction(*:x)
{
i += l;
if( l%2 )
x *= 1.0 / (l + 1);
else
x *= (l + 1);
}
}
#pragma omp taskgroup task_reduction(-:i,k) task_reduction(+:y)
{
for( int l = 0; l < N; ++l ) {
#pragma omp task firstprivate(l) in_reduction(+:j,y) \
in_reduction(*:x) in_reduction(-:k)
{
j += l;
k -= l;
y += (double)l;
if( l%2 )
x *= 1.0 / (l + 1);
else
x *= (l + 1);
}
#pragma omp task firstprivate(l) in_reduction(+:y) in_reduction(-:i,k)
{
i -= l;
k -= l;
y += (double)l;
}
#pragma omp task firstprivate(l) in_reduction(+:j) in_reduction(*:x)
{
j += l;
if( l%2 )
x *= 1.0 / (l + 1);
else
x *= (l + 1);
}
}
} // inner reduction
for( int l = 0; l < N; ++l ) {
#pragma omp task firstprivate(l) in_reduction(+:j)
j += l;
}
} // outer reduction
*/
//------------------------------------------------
// OpenMP runtime library routines
#ifdef __cplusplus
extern "C" {
#endif
extern void* __kmpc_task_reduction_get_th_data(int gtid, void* tg, void* item);
extern void* __kmpc_task_reduction_init(int gtid, int num, void* data);
extern int __kmpc_global_thread_num(void*);
#ifdef __cplusplus
}
#endif
//------------------------------------------------
// Compiler-generated code
typedef struct _task_red_item {
void *shar; // shared reduction item
size_t size; // size of data item
void *f_init; // data initialization routine
void *f_fini; // data finalization routine
void *f_comb; // data combiner routine
unsigned flags;
} _task_red_item_t;
// int:+ no need in init/fini callbacks, valid for subtraction
void __red_int_add_comb(void *lhs, void *rhs) // combiner
{ *(int*)lhs += *(int*)rhs; }
// long long:+ no need in init/fini callbacks, valid for subtraction
void __red_llong_add_comb(void *lhs, void *rhs) // combiner
{ *(long long*)lhs += *(long long*)rhs; }
// double:* no need in fini callback
void __red_dbl_mul_init(void *data) // initializer
{ *(double*)data = 1.0; }
void __red_dbl_mul_comb(void *lhs, void *rhs) // combiner
{ *(double*)lhs *= *(double*)rhs; }
// double:+ no need in init/fini callbacks
void __red_dbl_add_comb(void *lhs, void *rhs) // combiner
{ *(double*)lhs += *(double*)rhs; }
// ==============================
void calc_serial(int *pi, long long *pj, double *px, long long *pk, double *py)
{
for( int l = 0; l < N; ++l ) {
*pi += l;
if( l%2 )
*px *= 1.0 / (l + 1);
else
*px *= (l + 1);
}
for( int l = 0; l < N; ++l ) {
*pj += l;
*pk -= l;
*py += (double)l;
if( l%2 )
*px *= 1.0 / (l + 1);
else
*px *= (l + 1);
*pi -= l;
*pk -= l;
*py += (double)l;
*pj += l;
if( l%2 )
*px *= 1.0 / (l + 1);
else
*px *= (l + 1);
}
for( int l = 0; l < N; ++l ) {
*pj += l;
}
}
//------------------------------------------------
// Test case
int main()
{
int nthreads = omp_get_max_threads();
int err = 0;
void** ptrs = (void**)malloc(nthreads*sizeof(void*));
// user's code ======================================
// variables for serial calculations:
int is = 3;
long long js = -9999999;
double xs = 99999.0;
long long ks = 99999999;
double ys = -99999999.0;
// variables for parallel calculations:
int ip = 3;
long long jp = -9999999;
double xp = 99999.0;
long long kp = 99999999;
double yp = -99999999.0;
calc_serial(&is, &js, &xs, &ks, &ys);
// ==================================================
for (int i = 0; i < nthreads; ++i)
ptrs[i] = NULL;
#pragma omp parallel
{
#pragma omp single nowait
{
// outer taskgroup reduces (i,j,x)
#pragma omp taskgroup // task_reduction(+:i,j) task_reduction(*:x)
{
_task_red_item_t red_data[3];
red_data[0].shar = &ip;
red_data[0].size = sizeof(ip);
red_data[0].f_init = NULL; // RTL will zero thread-specific objects
red_data[0].f_fini = NULL; // no destructors needed
red_data[0].f_comb = (void*)&__red_int_add_comb;
red_data[0].flags = FLG;
red_data[1].shar = &jp;
red_data[1].size = sizeof(jp);
red_data[1].f_init = NULL; // RTL will zero thread-specific objects
red_data[1].f_fini = NULL; // no destructors needed
red_data[1].f_comb = (void*)&__red_llong_add_comb;
red_data[1].flags = FLG;
red_data[2].shar = &xp;
red_data[2].size = sizeof(xp);
red_data[2].f_init = (void*)&__red_dbl_mul_init;
red_data[2].f_fini = NULL; // no destructors needed
red_data[2].f_comb = (void*)&__red_dbl_mul_comb;
red_data[2].flags = FLG;
int gtid = __kmpc_global_thread_num(NULL);
void* tg1 = __kmpc_task_reduction_init(gtid, 3, red_data);
for( int l = 0; l < N; l += 2 ) {
// 2 iterations per task to get correct x value; actually any even
// number of iters per task will work, otherwise x looses precision
#pragma omp task firstprivate(l) //in_reduction(+:i) in_reduction(*:x)
{
int gtid = __kmpc_global_thread_num(NULL);
int *p_ip = (int*)__kmpc_task_reduction_get_th_data(gtid, tg1, &ip);
double *p_xp = (double*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &xp);
if (!ptrs[gtid]) ptrs[gtid] = p_xp;
// user's pseudo-code ==============================
*p_ip += l;
*p_xp *= (l + 1);
*p_ip += l + 1;
*p_xp *= 1.0 / (l + 2);
// ==================================================
}
}
// inner taskgroup reduces (i,k,y), i is same object as in outer one
#pragma omp taskgroup // task_reduction(-:i,k) task_reduction(+:y)
{
_task_red_item_t red_data[3];
red_data[0].shar = &ip;
red_data[0].size = sizeof(ip);
red_data[0].f_init = NULL; // RTL will zero thread-specific objects
red_data[0].f_fini = NULL; // no destructors needed
red_data[0].f_comb = (void*)&__red_int_add_comb;
red_data[0].flags = FLG;
red_data[1].shar = &kp;
red_data[1].size = sizeof(kp);
red_data[1].f_init = NULL; // RTL will zero thread-specific objects
red_data[1].f_fini = NULL; // no destructors needed
red_data[1].f_comb = (void*)&__red_llong_add_comb; // same for + and -
red_data[1].flags = FLG;
red_data[2].shar = &yp;
red_data[2].size = sizeof(yp);
red_data[2].f_init = NULL; // RTL will zero thread-specific objects
red_data[2].f_fini = NULL; // no destructors needed
red_data[2].f_comb = (void*)&__red_dbl_add_comb;
red_data[2].flags = FLG;
int gtid = __kmpc_global_thread_num(NULL);
void* tg2 = __kmpc_task_reduction_init(gtid, 3, red_data);
for( int l = 0; l < N; l += 2 ) {
#pragma omp task firstprivate(l)
// in_reduction(+:j,y) in_reduction(*:x) in_reduction(-:k)
{
int gtid = __kmpc_global_thread_num(NULL);
long long *p_jp = (long long*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &jp);
long long *p_kp = (long long*)__kmpc_task_reduction_get_th_data(
gtid, tg2, &kp);
double *p_xp = (double*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &xp);
double *p_yp = (double*)__kmpc_task_reduction_get_th_data(
gtid, tg2, &yp);
// user's pseudo-code ==============================
*p_jp += l;
*p_kp -= l;
*p_yp += (double)l;
*p_xp *= (l + 1);
*p_jp += l + 1;
*p_kp -= l + 1;
*p_yp += (double)(l + 1);
*p_xp *= 1.0 / (l + 2);
// =================================================
{
// the following code is here just to check __kmpc_task_reduction_get_th_data:
int tid = omp_get_thread_num();
void *addr1;
void *addr2;
addr1 = __kmpc_task_reduction_get_th_data(gtid, tg1, &xp); // from shared
addr2 = __kmpc_task_reduction_get_th_data(gtid, tg1, addr1); // from private
if (addr1 != addr2) {
#pragma omp atomic
++err;
printf("Wrong thread-specific addresses %d s:%p p:%p\n", tid, addr1, addr2);
}
// from neighbour w/o taskgroup (should start lookup from current tg2)
if (tid > 0) {
if (ptrs[tid-1]) {
addr2 = __kmpc_task_reduction_get_th_data(gtid, NULL, ptrs[tid-1]);
if (addr1 != addr2) {
#pragma omp atomic
++err;
printf("Wrong thread-specific addresses %d s:%p n:%p\n",
tid, addr1, addr2);
}
}
} else {
if (ptrs[nthreads-1]) {
addr2 = __kmpc_task_reduction_get_th_data(gtid, NULL, ptrs[nthreads-1]);
if (addr1 != addr2) {
#pragma omp atomic
++err;
printf("Wrong thread-specific addresses %d s:%p n:%p\n",
tid, addr1, addr2);
}
}
}
// ----------------------------------------------
}
}
#pragma omp task firstprivate(l)
// in_reduction(+:y) in_reduction(-:i,k)
{
int gtid = __kmpc_global_thread_num(NULL);
int *p_ip = (int*)__kmpc_task_reduction_get_th_data(
gtid, tg2, &ip);
long long *p_kp = (long long*)__kmpc_task_reduction_get_th_data(
gtid, tg2, &kp);
double *p_yp = (double*)__kmpc_task_reduction_get_th_data(
gtid, tg2, &yp);
// user's pseudo-code ==============================
*p_ip -= l;
*p_kp -= l;
*p_yp += (double)l;
*p_ip -= l + 1;
*p_kp -= l + 1;
*p_yp += (double)(l + 1);
// =================================================
}
#pragma omp task firstprivate(l)
// in_reduction(+:j) in_reduction(*:x)
{
int gtid = __kmpc_global_thread_num(NULL);
long long *p_jp = (long long*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &jp);
double *p_xp = (double*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &xp);
// user's pseudo-code ==============================
*p_jp += l;
*p_xp *= (l + 1);
*p_jp += l + 1;
*p_xp *= 1.0 / (l + 2);
// =================================================
}
}
} // inner reduction
for( int l = 0; l < N; l += 2 ) {
#pragma omp task firstprivate(l) // in_reduction(+:j)
{
int gtid = __kmpc_global_thread_num(NULL);
long long *p_jp = (long long*)__kmpc_task_reduction_get_th_data(
gtid, tg1, &jp);
// user's pseudo-code ==============================
*p_jp += l;
*p_jp += l + 1;
// =================================================
}
}
} // outer reduction
} // end single
} // end parallel
// check results
#if _DEBUG
printf("reduction flags = %u\n", FLG);
#endif
if (ip == is && jp == js && ks == kp &&
fabs(xp - xs) < 0.01 && fabs(yp - ys) < 0.01)
printf("passed\n");
else
printf("failed,\n ser:(%d %lld %f %lld %f)\n par:(%d %lld %f %lld %f)\n",
is, js, xs, ks, ys,
ip, jp, xp, kp, yp);
return 0;
}
|