-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCGMultiOpenMPTarget.cpp
598 lines (505 loc) · 18.4 KB
/
CGMultiOpenMPTarget.cpp
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// SPDX-License-Identifier: GPL-3.0-or-later
#include <cassert>
#include <memory>
#ifdef __INTEL_COMPILER
// For check that there are exactly two devices.
#include <cstdlib>
#include <iostream>
#endif
#include <omp.h>
#include "../CG.h"
#include "../Matrix.h"
#include "../Preconditioner.h"
static inline int getNumberOfDevices() { return omp_get_num_devices(); }
/// Class implementing parallel kernels with OpenMP target directives.
class CGMultiOpenMPTarget : public CG {
std::unique_ptr<floatType[]> p;
std::unique_ptr<floatType[]> q;
std::unique_ptr<floatType[]> r;
std::unique_ptr<floatType[]> z;
std::unique_ptr<floatType[]> vectorDotResults;
floatType *getVector(Vector v) {
switch (v) {
case VectorK:
return k;
case VectorX:
return x;
case VectorP:
return p.get();
case VectorQ:
return q.get();
case VectorR:
return r.get();
case VectorZ:
return z.get();
}
assert(0 && "Invalid value of v!");
return nullptr;
}
virtual bool supportsMatrixFormat(MatrixFormat format) override {
return format == MatrixFormatCRS || format == MatrixFormatELL;
}
virtual bool supportsPreconditioner(Preconditioner preconditioner) override {
return preconditioner == PreconditionerJacobi;
}
virtual int getNumberOfChunks() override { return getNumberOfDevices(); }
virtual bool supportsOverlappedGather() override { return true; }
virtual void init(const char *matrixFile) override;
virtual bool needsTransfer() override { return true; }
virtual void doTransferTo() override;
virtual void doTransferFrom() override;
virtual void cpy(Vector _dst, Vector _src) override;
void matvecGatherXViaHost(floatType *x);
template <bool roundup = false>
void matvecKernelCRS(MatrixDataCRS *matrices, floatType *x, floatType *y);
template <bool roundup = false>
void matvecKernelELL(MatrixDataELL *matrices, floatType *x, floatType *y);
virtual void matvecKernel(Vector _x, Vector _y) override;
virtual void axpyKernel(floatType a, Vector _x, Vector _y) override;
virtual void xpayKernel(Vector _x, floatType a, Vector _y) override;
virtual floatType vectorDotKernel(Vector _a, Vector _b) override;
void applyPreconditionerKernelJacobi(floatType *x, floatType *y);
virtual void applyPreconditionerKernel(Vector _x, Vector _y) override;
public:
CGMultiOpenMPTarget()
: CG(MatrixFormatCRS, PreconditionerJacobi,
/* overlappedGather= */ true) {}
};
void CGMultiOpenMPTarget::init(const char *matrixFile) {
int devices = getNumberOfDevices();
#ifdef __INTEL_COMPILER
// Until #6000161589 is fixed, we can't have an asynchronous offloading that
// depends on previous tasks! To work that around, we manually unroll a loop
// in matvecGatherXViaHost() and assume two devices.
if (devices != 2) {
std::cerr << "Only supports exactly two devices!" << std::endl;
std::exit(1);
}
#endif
// Init the devices with a simple target region.
for (int d = 0; d < getNumberOfDevices(); d++) {
#pragma omp target device(d)
{ }
}
CG::init(matrixFile);
assert(workDistribution->numberOfChunks == devices);
p.reset(new floatType[N]);
q.reset(new floatType[N]);
r.reset(new floatType[N]);
if (preconditioner != PreconditionerNone) {
z.reset(new floatType[N]);
}
vectorDotResults.reset(new floatType[devices]);
}
static inline void enterMatrixCRS(const MatrixDataCRS &matrix, int N) {
int *ptr = matrix.ptr;
int nz = ptr[N];
int *index = matrix.index;
floatType *value = matrix.value;
#pragma omp target enter data nowait map(to: ptr[0:N+1], index[0:nz]) \
map(to: value[0:nz])
}
static inline void enterMatrixELL(const MatrixDataELL &matrix, int N) {
int elements = matrix.elements;
int *length = matrix.length;
int *index = matrix.index;
floatType *data = matrix.data;
#pragma omp target enter data nowait map(to: length[0:N], index[0:elements]) \
map(to: data[0:elements])
}
void CGMultiOpenMPTarget::doTransferTo() {
// Allocate memory on the device with plain pointers.
int N = this->N;
floatType *p = this->p.get();
floatType *q = this->q.get();
floatType *r = this->r.get();
floatType *x = this->x;
floatType *k = this->k;
floatType *vectorDotResults = this->vectorDotResults.get();
for (int d = 0; d < getNumberOfDevices(); d++) {
// Call omp_set_default_device once and don't use the device-clause.
omp_set_default_device(d);
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#pragma omp target enter data nowait map(alloc: p[0:N], q[offset:length]) \
map(alloc: r[offset:length]) \
map(to: x[0:N], k[offset:length])
switch (matrixFormat) {
case MatrixFormatCRS:
if (!overlappedGather) {
enterMatrixCRS(splitMatrixCRS->data[d], length);
} else {
enterMatrixCRS(partitionedMatrixCRS->diag[d], length);
enterMatrixCRS(partitionedMatrixCRS->minor[d], length);
}
break;
case MatrixFormatELL:
if (!overlappedGather) {
enterMatrixELL(splitMatrixELL->data[d], length);
} else {
enterMatrixELL(partitionedMatrixELL->diag[d], length);
enterMatrixELL(partitionedMatrixELL->minor[d], length);
}
break;
default:
assert(0 && "Invalid matrix format!");
}
if (preconditioner != PreconditionerNone) {
floatType *z = this->z.get();
#pragma omp target enter data nowait map(alloc: z[offset:length])
switch (preconditioner) {
case PreconditionerJacobi: {
floatType *C = jacobi->C;
#pragma omp target enter data nowait map(to: C[offset:length])
break;
}
default:
assert(0 && "Invalid preconditioner!");
}
}
#pragma omp target enter data nowait map(alloc: vectorDotResults[d:1])
}
#pragma omp taskwait
}
static inline void exitMatrixCRS(const MatrixDataCRS &matrix, int N) {
int *ptr = matrix.ptr;
int nz = ptr[N];
int *index = matrix.index;
floatType *value = matrix.value;
#pragma omp target exit data nowait map(release: ptr[0:N+1], index[0:nz]) \
map(release: value[0:nz])
}
static inline void exitMatrixELL(const MatrixDataELL &matrix, int N) {
int elements = matrix.elements;
int *length = matrix.length;
int *index = matrix.index;
floatType *data = matrix.data;
#pragma omp target exit data nowait map(release: length[0:N]) \
map(release: index[0:elements]) \
map(release: data[0:elements])
}
void CGMultiOpenMPTarget::doTransferFrom() {
// Free memory on the device with plain pointers.
int N = this->N;
floatType *p = this->p.get();
floatType *q = this->q.get();
floatType *r = this->r.get();
floatType *x = this->x;
floatType *k = this->k;
floatType *vectorDotResults = this->vectorDotResults.get();
for (int d = 0; d < getNumberOfDevices(); d++) {
// Call omp_set_default_device once and don't use the device-clause.
omp_set_default_device(d);
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
// Copy back this device's part of the solution...
#pragma omp target update nowait from(x[offset:length]) depend(out: x[offset:length])
// ... and delete the whole array afterwards.
#ifdef __INTEL_COMPILER
// Until #6000161589 is fixed, we can't have an asynchronous offloading that
// depends on previous tasks!
#pragma omp taskwait
#endif
#pragma omp target exit data nowait map(release: x[0:N]) depend(in: x[offset:length])
#pragma omp target exit data nowait map(release: p[0:N], q[offset:length]) \
map(release: r[offset:length]) \
map(release: k[offset:length])
switch (matrixFormat) {
case MatrixFormatCRS:
if (!overlappedGather) {
exitMatrixCRS(splitMatrixCRS->data[d], length);
} else {
exitMatrixCRS(partitionedMatrixCRS->diag[d], length);
exitMatrixCRS(partitionedMatrixCRS->minor[d], length);
}
break;
case MatrixFormatELL:
if (!overlappedGather) {
exitMatrixELL(splitMatrixELL->data[d], length);
} else {
exitMatrixELL(partitionedMatrixELL->diag[d], length);
exitMatrixELL(partitionedMatrixELL->minor[d], length);
}
break;
default:
assert(0 && "Invalid matrix format!");
}
if (preconditioner != PreconditionerNone) {
floatType *z = this->z.get();
#pragma omp target exit data nowait map(release: z[offset:length])
switch (preconditioner) {
case PreconditionerJacobi: {
floatType *C = jacobi->C;
#pragma omp target exit data nowait map(release: C[offset:length])
break;
}
default:
assert(0 && "Invalid preconditioner!");
}
}
#pragma omp target exit data nowait map(release: vectorDotResults[d:1])
}
#pragma omp taskwait
}
void CGMultiOpenMPTarget::cpy(Vector _dst, Vector _src) {
floatType *dst = getVector(_dst);
floatType *src = getVector(_src);
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
// Bug in Intel Compiler 17.0.2 20170213: nowait on combined construct
// does not result in asynchronous offloading!
#pragma omp target nowait device(d) \
map(dst[offset:length], src[offset:length])
#pragma omp teams distribute parallel for simd
for (int i = offset; i < offset + length; i++) {
dst[i] = src[i];
}
}
#pragma omp taskwait
}
void CGMultiOpenMPTarget::matvecGatherXViaHost(floatType *x) {
// Gather x on host.
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#pragma omp target update nowait device(d) from(x[offset:length]) \
depend(out: x[offset:length])
}
#ifdef __INTEL_COMPILER
// Until #6000161589 is fixed, we can't have an asynchronous offloading that
// depends on previous tasks! We have checked in init() that there are exactly
// two devices...
int d0 = 0;
int src0 = 1;
int offset0 = workDistribution->offsets[src0];
int length0 = workDistribution->lengths[src0];
#pragma omp target update nowait device(d0) to(x[offset0:length0]) \
depend(in: x[offset0:length0])
int d1 = 1;
int src1 = 0;
int offset1 = workDistribution->offsets[src1];
int length1 = workDistribution->lengths[src1];
#pragma omp target update nowait device(d1) to(x[offset1:length1]) \
depend(in: x[offset1:length1])
#else
// Transfer x to devices.
for (int d = 0; d < getNumberOfDevices(); d++) {
for (int src = 0; src < getNumberOfDevices(); src++) {
if (src == d) {
// Don't transfer chunk that is already on the device.
continue;
}
int offset = workDistribution->offsets[src];
int length = workDistribution->lengths[src];
#pragma omp target update nowait device(d) to(x[offset:length]) \
depend(in: x[offset:length])
}
}
#endif
// In case of overlappedGather, we also wait for the first part of the
// computation to finish!
#pragma omp taskwait
}
template <bool roundup>
void CGMultiOpenMPTarget::matvecKernelCRS(MatrixDataCRS *matrices, floatType *x,
floatType *y) {
int N = this->N;
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
int *ptr = matrices[d].ptr;
int nz = ptr[length];
int *index = matrices[d].index;
floatType *value = matrices[d].value;
#pragma omp target nowait device(d) map(x[0:N], y[offset:length]) \
map(ptr[0:length+1], index[0:nz], value[0:nz])
#pragma omp teams distribute parallel for
for (int i = 0; i < length; i++) {
// Skip load and store if nothing to be done...
if (!roundup || ptr[i] != ptr[i + 1]) {
floatType tmp = (roundup ? y[offset + i] : 0);
#pragma omp simd reduction(+:tmp)
for (int j = ptr[i]; j < ptr[i + 1]; j++) {
tmp += value[j] * x[index[j]];
}
y[offset + i] = tmp;
}
}
}
if (!overlappedGather) {
#pragma omp taskwait
}
}
template <bool roundup>
void CGMultiOpenMPTarget::matvecKernelELL(MatrixDataELL *matrices, floatType *x,
floatType *y) {
int N = this->N;
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
int elements = matrices[d].elements;
int *lengthA = matrices[d].length;
int *index = matrices[d].index;
floatType *data = matrices[d].data;
#pragma omp target nowait device(d) map(x[0:N], y[offset:length]) \
map(lengthA[0:length], index[0:elements], data[0:elements])
#pragma omp teams distribute parallel for simd
for (int i = 0; i < length; i++) {
// Skip load and store if nothing to be done...
if (!roundup || lengthA[i] > 0) {
floatType tmp = (roundup ? y[offset + i] : 0);
#pragma unroll 1
for (int j = 0; j < lengthA[i]; j++) {
int k = j * length + i;
tmp += data[k] * x[index[k]];
}
y[offset + i] = tmp;
}
}
}
if (!overlappedGather) {
#pragma omp taskwait
}
}
void CGMultiOpenMPTarget::matvecKernel(Vector _x, Vector _y) {
floatType *x = getVector(_x);
floatType *y = getVector(_y);
if (overlappedGather) {
// Start computation on the diagonal that does not require data exchange
// between the devices. It is efficient to do so before the gather because
// the computation is expected to take longer. This effectively even hides
// the overhead of starting the gather.
switch (matrixFormat) {
case MatrixFormatCRS:
matvecKernelCRS(partitionedMatrixCRS->diag.get(), x, y);
break;
case MatrixFormatELL:
matvecKernelELL(partitionedMatrixELL->diag.get(), x, y);
break;
default:
assert(0 && "Invalid matrix format!");
}
}
matvecGatherXViaHost(x);
switch (matrixFormat) {
case MatrixFormatCRS:
if (!overlappedGather) {
matvecKernelCRS(splitMatrixCRS->data.get(), x, y);
} else {
matvecKernelCRS<true>(partitionedMatrixCRS->minor.get(), x, y);
}
break;
case MatrixFormatELL:
if (!overlappedGather) {
matvecKernelELL(splitMatrixELL->data.get(), x, y);
} else {
matvecKernelELL<true>(partitionedMatrixELL->minor.get(), x, y);
}
break;
default:
assert(0 && "Invalid matrix format!");
}
if (overlappedGather) {
// Wait for all tasks - this was skipped before!
#pragma omp taskwait
}
}
void CGMultiOpenMPTarget::axpyKernel(floatType a, Vector _x, Vector _y) {
floatType *x = getVector(_x);
floatType *y = getVector(_y);
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#pragma omp target nowait device(d) map(x[offset:length], y[offset:length])
#pragma omp teams distribute parallel for simd
for (int i = offset; i < offset + length; i++) {
y[i] += a * x[i];
}
}
#pragma omp taskwait
}
void CGMultiOpenMPTarget::xpayKernel(Vector _x, floatType a, Vector _y) {
floatType *x = getVector(_x);
floatType *y = getVector(_y);
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#pragma omp target nowait device(d) map(x[offset:length], y[offset:length])
#pragma omp teams distribute parallel for simd
for (int i = offset; i < offset + length; i++) {
y[i] = x[i] + a * y[i];
}
}
#pragma omp taskwait
}
floatType CGMultiOpenMPTarget::vectorDotKernel(Vector _a, Vector _b) {
floatType res = 0;
floatType *a = getVector(_a);
floatType *b = getVector(_b);
floatType *vectorDotResults = this->vectorDotResults.get();
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#ifndef __INTEL_COMPILER
// We can't use vectorDotResults[d] directly below, so we can save this...
vectorDotResults[d] = 0;
#pragma omp target update device(d) to(vectorDotResults[d:1])
#endif
#pragma omp target nowait device(d) map(a[offset:length], b[offset:length]) \
map(vectorDotResults[d:1])
#ifndef __INTEL_COMPILER
// 17.0.2 20170213
// array section derived from "vectorDotResults" is not supported for simd pragma
#pragma omp teams distribute parallel for simd reduction(+:vectorDotResults[d:1])
// Another possibility:
// 17.0.2 20170213: internal error: 04010002_1529
// #pragma omp teams distribute parallel for reduction(+:vectorDotResults[d:1])
for (int i = offset; i < offset + length; i++) {
vectorDotResults[d] += a[i] * b[i];
}
#else
{
floatType red = 0;
#pragma omp parallel for reduction(+:red)
for (int i = offset; i < offset + length; i++) {
red += a[i] * b[i];
}
vectorDotResults[d] = red;
}
#endif
}
#pragma omp taskwait
for (int d = 0; d < getNumberOfDevices(); d++) {
#pragma omp target update device(d) from(vectorDotResults[d:1])
res += vectorDotResults[d];
}
return res;
}
void CGMultiOpenMPTarget::applyPreconditionerKernelJacobi(floatType *x,
floatType *y) {
floatType *C = jacobi->C;
for (int d = 0; d < getNumberOfDevices(); d++) {
int offset = workDistribution->offsets[d];
int length = workDistribution->lengths[d];
#pragma omp target nowait device(d) \
map(x[offset:length], y[offset:length], C[offset:length])
#pragma omp teams distribute parallel for simd
for (int i = offset; i < offset + length; i++) {
y[i] = C[i] * x[i];
}
}
#pragma omp taskwait
}
void CGMultiOpenMPTarget::applyPreconditionerKernel(Vector _x, Vector _y) {
floatType *x = getVector(_x);
floatType *y = getVector(_y);
switch (preconditioner) {
case PreconditionerJacobi:
applyPreconditionerKernelJacobi(x, y);
break;
default:
assert(0 && "Invalid preconditioner!");
}
}
CG *CG::getInstance() { return new CGMultiOpenMPTarget; }