forked from HLRA-JHPCN/HACApK-MAGMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HACApK_MAGMA.c
executable file
·412 lines (383 loc) · 14.3 KB
/
HACApK_MAGMA.c
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
#include "HACApK_MAGMA.h"
//!***c_HACApK_adot_body_lfmtx
void c_hacapk_adot_body_lfmtx_(double *zau, stc_HACApK_leafmtxp *st_leafmtxp, double *zu, double *zbu) {
register int ip,il,it;
int nlf,ndl,ndt,nstrtl,nstrtt,kt,itl,itt,ill;
int st_lf_stride = st_leafmtxp->st_lf_stride;
nlf=st_leafmtxp->nlf;
int mpi_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
//if (mpi_rank == 0) // good
for (ip = 0; ip < nlf; ip++) {
/**/
stc_HACApK_leafmtx *sttmp;
sttmp = (void *)(st_leafmtxp->st_lf) + st_lf_stride * ip;
/**/
ndl =sttmp->ndl; // m: number of rows
ndt =sttmp->ndt; // n: number of columns
nstrtl=sttmp->nstrtl; // i: index of first row (base-1)
nstrtt=sttmp->nstrtt; // j: index of first column (base-1)
if (sttmp->ltmtx == 1) { // compressed
/**/
double *a2tmp = (double *)((void*)(sttmp->a1)+sttmp->a1size);
/**/
kt = sttmp->kt; // rank
for(il=0; il<kt; il++){
zbu[il]=0.0;
for(it=0; it<ndt; it++){ // zbu := V'*zu
itt=it+nstrtt-1;
itl=it+il*ndt;
zbu[il] += sttmp->a1[itl]*zu[itt];
}
}
for(il=0; il<kt; il++){ // zau := U*zbu
for(it=0; it<ndl; it++){
ill=it+nstrtl-1;
itl=it+il*ndl;
zau[ill] += a2tmp[itl]*zbu[il];
}
}
} else if(sttmp->ltmtx==2){ // full
for(il=0; il<ndl; il++){
ill=il+nstrtl-1;
for(it=0; it<ndt; it++){
itt=it+nstrtt-1;
itl=it+il*ndt;
zau[ill] += sttmp->a1[itl]*zu[itt];
}
}
}
}
}
#if defined(HAVE_MAGMA) | defined(HAVE_MAGMA_BATCH)
// /////////////////////////////////////////////////////////////////////////
// DGEMV using MAGMA
// > using CuBLAS DGEMV (out-of-place/in-place)
// > using MAGMA batched DGEMV (out-of-place/in-pace)
// > using MAGMA batched DGEMV, sorted (in-place)
// /////////////////////////////////////////////////////////////////////////
#if defined(HAVE_MAGMA)
void magma_init_() {
magma_init();
}
void magma_finalize_() {
magma_finalize();
}
/////////////////////////////////////////////////////
// MatVec on GPU
void c_hacapk_adot_body_lfmtx_gpu_(double *zau, stc_HACApK_leafmtxp *st_leafmtxp, double *zu, double *zbu) {
// constants
int ione = 1;
double one = 1.0;
double zero = 0.0;
int ip;
int nlf, ndl, ndt, nstrtl, nstrtt, kt;
int st_lf_stride = st_leafmtxp->st_lf_stride;
//#define GPU
#define CPU
#if defined(GPU)
// allocate queue
magma_device_t cdev;
magma_queue_t queue[num_streams];
magma_getdevice( &cdev );
for (ip = 0; ip < num_streams; ip++) {
magma_queue_create( cdev, &queue[ip] );
}
// copy the input vector to GPU
magma_dsetvector( st_leafmtxp->gn, zu, 1, st_leafmtxp->zu_gpu, 1, queue[0] );
magma_dsetvector( st_leafmtxp->m, zau, 1, st_leafmtxp->zau_gpu[0], 1, queue[0] );
for (ip = 1; ip < num_streams; ip++) {
magmablas_dlaset( MagmaFull, st_leafmtxp->m, 1, zero, zero,
st_leafmtxp->zau_gpu[ip], st_leafmtxp->m, queue[ip] );
}
#endif
// parse all the blocks
#ifdef PROF_MAGMA_BATCH
double tic = MPI_Wtime();
#endif
nlf=st_leafmtxp->nlf;
for (ip = 0; ip < nlf; ip++) {
/**/
stc_HACApK_leafmtx *sttmp;
sttmp = (void *)(st_leafmtxp->st_lf) + st_lf_stride * ip;
/**/
ndl = sttmp->ndl; // m: number of rows
ndt = sttmp->ndt; // n: number of columns
nstrtl = sttmp->nstrtl; // i: index of first row (base-1)
nstrtt = sttmp->nstrtt; // j: index of first column (base-1)
#if defined(GPU)
int stream_id = ip%num_streams;
#endif
if (sttmp->ltmtx == 1) { // compressed
/**/
double *a2tmp = (double *)((void*)(sttmp->a1)+sttmp->a1size);
/**/
kt=sttmp->kt; // rank
// zbu := V'*zu
#if defined(GPU)
magmablas_dgemv(MagmaTrans, ndt, kt,
one, st_leafmtxp->mtx1_gpu[ip], ndt,
&(st_leafmtxp->zu_gpu[nstrtt-1]), ione,
zero, st_leafmtxp->zbu_gpu[stream_id], ione,
queue[stream_id] );
#elif defined(CPU)
dgemv_("T", &ndt, &kt,
&one, sttmp->a1, &ndt,
&zu[nstrtt-1], &ione,
&zero, zbu, &ione );
#else
for (il = 0; il < kt; il++) {
zbu[il]=0.0;
for ( it = 0; it < ndt; it++) {
itt=it+nstrtt-1;
itl=it+il*ndt;
zbu[il] += sttmp->a1[itl]*zu[itt];
}
}
#endif
// zau :+= U*zbu
#if defined(GPU)
magmablas_dgemv(MagmaNoTrans, ndl, kt,
one, st_leafmtxp->mtx2_gpu[ip], ndl,
st_leafmtxp->zbu_gpu[stream_id], ione,
one, &(st_leafmtxp->zau_gpu[stream_id][nstrtl-1]), ione,
queue[stream_id] );
#elif defined(CPU)
dgemv_("N", &ndl, &kt,
&one, a2tmp, &ndl,
zbu, &ione,
&one, &zau[nstrtl-1], &ione );
#else
for(il = 0; il < kt; il++) {
for(it = 0; it < ndl; it++){
ill=it+nstrtl-1;
itl=it+il*ndl;
zau[ill] += a2tmp[itl]*zbu[il];
}
}
#endif
} else if(sttmp->ltmtx == 2) { // full
#if defined(GPU)
magmablas_dgemv(MagmaTrans, ndt, ndl,
one, st_leafmtxp->mtx1_gpu[ip], ndt,
&(st_leafmtxp->zu_gpu[nstrtt-1]), ione,
one, &(st_leafmtxp->zau_gpu[stream_id][nstrtl-1]), ione,
queue[stream_id] );
#elif defined(CPU)
dgemv_("T", &ndt, &ndl,
&one, sttmp->a1, &ndt,
&zu[nstrtt-1], &ione,
&one, &zau[nstrtl-1], &ione );
#else
for (il = 0; il < ndl; il++){
ill=il+nstrtl-1;
for (it = 0; it < ndt; it++) {
itt=it+nstrtt-1;
itl=it+il*ndt;
zau[ill] += sttmp->a1[itl]*zu[itt];
}
}
#endif
}
}
#if defined(GPU)
for (ip = 1; ip < num_streams; ip++) {
magma_queue_sync( queue[ip] );
magma_queue_destroy( queue[ip] );
magma_daxpy( st_leafmtxp->m, one, st_leafmtxp->zau_gpu[ip], 1,
st_leafmtxp->zau_gpu[0], 1,
queue[0] );
}
// synch to get time
MPI_Barrier(MPI_COMM_WORLD);
magma_queue_sync( queue[0] );
#ifdef PROF_MAGMA_BATCH
if (st_leafmtxp->mpi_rank == 0) {
printf( " time_gpu: %.2e seconds\n\n",MPI_Wtime()-tic );
}
#endif
// copy back
magma_dgetvector( st_leafmtxp->m, st_leafmtxp->zau_gpu[0], 1, zau, 1, queue[0] );
magma_queue_destroy( queue[0] );
#else
MPI_Barrier(MPI_COMM_WORLD);
#ifdef PROF_MAGMA_BATCH
if (st_leafmtxp->mpi_rank == 0) {
printf( " time_cpu: %.2e seconds\n\n",MPI_Wtime()-tic );
}
#endif
#endif
}
/////////////////////////////////////////////////////
// copy blocks to GPU
void c_hacapk_adot_body_lfcpy_gpu_(int *nd, stc_HACApK_leafmtxp *st_leafmtxp) {
#define GPU
#if defined(GPU)
// local variables
register int ip;
int nlf, ndl, ndt, nstrtl, nstrtt, kt;
int st_lf_stride = st_leafmtxp->st_lf_stride;
// let me initialize here for now..
#ifdef MAGMA_INIT_PER
magma_init();
#endif
//st_leafmtxp->mpi_comm = MPI_COMM_WORLD; // comm world for now
MPI_Comm_rank(MPI_COMM_WORLD, &(st_leafmtxp->mpi_rank));
if (st_leafmtxp->mpi_rank == 0) magma_print_environment();
// allocate queue
magma_device_t cdev;
magma_queue_t queue = NULL;
magma_setdevice( get_device_id(st_leafmtxp) );
magma_getdevice( &cdev );
magma_queue_create( cdev, &queue );
int name_len;
char proc_name[300];
MPI_Get_processor_name( proc_name, &name_len );
printf( " processor %d uses %d GPU on %s\n",st_leafmtxp->mpi_rank,(st_leafmtxp->mpi_rank)%procs_per_node,proc_name);
// number of blocks
nlf = st_leafmtxp->nlf;
// initialize data structure
st_leafmtxp->gn = *nd;
st_leafmtxp->m = 0;
st_leafmtxp->n = 0;
st_leafmtxp->max_block = 0;
st_leafmtxp->mtx1_gpu = (magmaDouble_ptr*)malloc(nlf * sizeof(magmaDouble_ptr));
st_leafmtxp->mtx2_gpu = (magmaDouble_ptr*)malloc(nlf * sizeof(magmaDouble_ptr));
// parse all the blocks
size_t tot = 0;
for (ip = 0; ip < nlf; ip++) {
/**/
stc_HACApK_leafmtx *sttmp;
sttmp = (void *)(st_leafmtxp->st_lf) + st_lf_stride * ip;
ndl = sttmp->ndl; // m: number of rows
ndt = sttmp->ndt; // n: number of columns
nstrtl = sttmp->nstrtl; // i: index of first row (base-1)
nstrtt = sttmp->nstrtt; // j: index of first column (base-1)
// local matrix size
if (nstrtl == nstrtt) {
st_leafmtxp->m += ndl;
}
if (st_leafmtxp->max_block < max(ndl, ndt)) {
st_leafmtxp->max_block = max(ndl, ndt);
}
/**/
if (sttmp->ltmtx == 1) { // compressed
/**/
double *a2tmp = (double *)((void*)(sttmp->a1)+sttmp->a1size);
/**/
kt = sttmp->kt; // rank
// copy V
tot += ndt*kt;
st_leafmtxp->mtx1_gpu[ip] = NULL;
int retval = magma_dmalloc( &(st_leafmtxp->mtx1_gpu[ip]), ndt*kt );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for mtx1_gpu[0][%d]=%dx%d\n", ip,ndt,kt);
exit(0);
}
magma_dsetmatrix( ndt, kt, sttmp->a1, ndt, st_leafmtxp->mtx1_gpu[ip], ndt, queue );
// copy U
tot += ndl*kt;
st_leafmtxp->mtx2_gpu[ip] = NULL;
retval = magma_dmalloc( &(st_leafmtxp->mtx2_gpu[ip]), ndl*kt );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for mtx2_gpu[1][%d]\n", ip);
exit(0);
}
magma_dsetmatrix( ndl, kt, a2tmp, ndl, st_leafmtxp->mtx2_gpu[ip], ndl, queue );
} else if (sttmp->ltmtx == 2) { // full
st_leafmtxp->mtx1_gpu[ip] = NULL;
tot += ndt*ndl;
int retval = magma_dmalloc( &(st_leafmtxp->mtx1_gpu[ip]), ndt*ndl );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for mtx1_gpu[0][%d]\n", ip);
exit(0);
}
magma_dsetmatrix( ndt, ndl, sttmp->a1, ndt, st_leafmtxp->mtx1_gpu[ip], ndt, queue );
st_leafmtxp->mtx2_gpu[ip] = NULL;
}
//printf( " %d(%d/%d): tot=%.2eGB\n",st_leafmtxp->mpi_rank,ip,nlf,(8.0*tot)/1e9 );
}
printf( "%d: tot=%.2eGB\n",st_leafmtxp->mpi_rank,(8.0*tot)/1e9 );
// let's just use global size.
st_leafmtxp->m = st_leafmtxp->gn;
st_leafmtxp->n = st_leafmtxp->gn;
// workspace for GEMV on GPU
st_leafmtxp->zu_gpu = NULL;
st_leafmtxp->zau_gpu = NULL;
st_leafmtxp->zbu_gpu = NULL;
if (st_leafmtxp->max_block > 0) {
st_leafmtxp->zbu_gpu = (double**)malloc( num_streams * sizeof(double*) );
for (ip = 0; ip < num_streams; ip++) {
int retval = magma_dmalloc( &st_leafmtxp->zbu_gpu[ip], st_leafmtxp->max_block );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for zbu_gpu\n");
exit(0);
}
}
}
if (st_leafmtxp->m > 0) {
st_leafmtxp->zau_gpu = (double**)malloc( num_streams * sizeof(double*) );
for (ip = 0; ip < num_streams; ip++) {
int retval = magma_dmalloc( &st_leafmtxp->zau_gpu[ip], st_leafmtxp->m );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for zau_gpu\n");
exit(0);
}
}
}
if (st_leafmtxp->n > 0) {
int retval = magma_dmalloc( &st_leafmtxp->zu_gpu, st_leafmtxp->gn );
if ( MAGMA_SUCCESS != retval ) {
fprintf( stderr, "!!!! magma_dmalloc failed for zu_gpu\n");
exit(0);
}
}
if (st_leafmtxp->mpi_rank == 0) {
printf( " %d-by-%d matrix (# blocks=%d)\n",st_leafmtxp->m,st_leafmtxp->n,nlf );
}
magma_queue_destroy( queue );
#endif
}
/////////////////////////////////////////////////////
// delete GPU memory
void c_hacapk_adot_body_lfdel_gpu_(stc_HACApK_leafmtxp *st_leafmtxp) {
#if defined(GPU)
int ip;
for(ip = 0; ip < st_leafmtxp->nlf; ip++) {
if(st_leafmtxp->mtx1_gpu[ip] != NULL) {
magma_free(st_leafmtxp->mtx1_gpu[ip]);
st_leafmtxp->mtx1_gpu[ip] = NULL;
}
if(st_leafmtxp->mtx2_gpu[ip] != NULL) {
magma_free(st_leafmtxp->mtx2_gpu[ip]);
st_leafmtxp->mtx2_gpu[ip] = NULL;
}
}
free(st_leafmtxp->mtx1_gpu);
free(st_leafmtxp->mtx2_gpu);
if (st_leafmtxp->zu_gpu != NULL) {
magma_free(st_leafmtxp->zu_gpu);
st_leafmtxp->zu_gpu = NULL;
}
if (st_leafmtxp->zbu_gpu != NULL) {
for (ip = 0; ip < num_streams; ip++) {
magma_free(st_leafmtxp->zbu_gpu[ip]);
}
free(st_leafmtxp->zbu_gpu);
st_leafmtxp->zbu_gpu = NULL;
}
if (st_leafmtxp->zau_gpu != NULL) {
for (ip = 0; ip < num_streams; ip++) {
magma_free(st_leafmtxp->zau_gpu[ip]);
}
free(st_leafmtxp->zau_gpu);
st_leafmtxp->zau_gpu = NULL;
}
// let me finalize it here for now
#ifdef MAGMA_INIT_PER
magma_finalize();
#endif
#endif
}
#endif
#endif