-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhip_blas.cpp
More file actions
630 lines (563 loc) · 23 KB
/
hip_blas.cpp
File metadata and controls
630 lines (563 loc) · 23 KB
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include <rocsparse.h>
#include <rocblas.h>
#include <hip/hip_runtime_api.h>
#include "hip_blas.h"
static rocblas_handle handle_rocblas;
static rocsparse_handle handle_rocsparse;
static void *mv_buffer = NULL;
static void *L_buffer;
static void *U_buffer;
static void *ichol_buffer;
static rocsparse_mat_descr matA = NULL;
static rocsparse_mat_descr descrL, descrU, descrA;
static rocsparse_mat_descr descrLt; //for ICHOL
static rocsparse_mat_info infoL, infoU, infoLic, infoLtic;
static rocsparse_mat_info infoA;
static rocsparse_mat_descr descrLic, descrLtic, descrM; //ICHOL
static rocsparse_mat_info infoM; //ICHOL
void initialize_handles(){
rocblas_create_handle(&handle_rocblas);
rocsparse_create_handle(&handle_rocsparse);
rocsparse_create_mat_descr(&(descrL));
rocsparse_set_mat_fill_mode(descrL, rocsparse_fill_mode_lower);
rocsparse_set_mat_index_base(descrL, rocsparse_index_base_zero);
rocsparse_create_mat_descr(&(descrU));
rocsparse_set_mat_index_base(descrU, rocsparse_index_base_zero);
rocsparse_set_mat_fill_mode(descrU, rocsparse_fill_mode_upper);
rocsparse_create_mat_descr(&(descrA));
rocsparse_set_mat_index_base(descrA, rocsparse_index_base_zero);
rocsparse_set_mat_type(descrA, rocsparse_matrix_type_general);
rocsparse_create_mat_info(&infoA);
rocsparse_create_mat_info(&infoL);
rocsparse_create_mat_info(&infoU);
hipDeviceSynchronize();
}
void analyze_spmv(const int n,
const int nnz,
int *ia,
int *ja,
real_type *a,
const real_type *x,
real_type *result,
char * option
){
/* no buffer in matvec */
rocsparse_status status_rocsparse;
if (strcmp(option, "A") == 0) {
status_rocsparse = rocsparse_dcsrmv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
descrA,
a,
ia,
ja,
infoA);
}
if (strcmp(option, "L") == 0) {
status_rocsparse = rocsparse_dcsrmv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
descrL,
a,
ia,
ja,
infoL);
}
if (strcmp(option, "U") == 0) {
status_rocsparse = rocsparse_dcsrmv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
descrU,
a,
ia,
ja,
infoU);
}
if (status_rocsparse != 0) {
printf("mv analysis status for %s is %d \n", option, status_rocsparse);
}
hipDeviceSynchronize();
}
void initialize_and_analyze_L_and_U_solve(const int n,
const int nnzL,
int *lia,
int *lja,
real_type *la,
const int nnzU,
int *uia,
int *uja,
real_type *ua){
size_t L_buffer_size;
size_t U_buffer_size;
rocsparse_status status_rocsparse;
status_rocsparse = rocsparse_dcsrsv_buffer_size(handle_rocsparse,
rocsparse_operation_none,
n,
nnzL,
descrL,
la,
lia,
lja,
infoL,
&L_buffer_size);
hipMalloc((void**)&(L_buffer), L_buffer_size);
status_rocsparse = rocsparse_dcsrsv_buffer_size(handle_rocsparse,
rocsparse_operation_none,
n,
nnzU,
descrU,
ua,
uia,
uja,
infoU,
&U_buffer_size);
hipMalloc((void**)&(U_buffer), U_buffer_size);
status_rocsparse = rocsparse_dcsrsv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
nnzL,
descrL,
la,
lia,
lja,
infoL,
rocsparse_analysis_policy_reuse,
rocsparse_solve_policy_auto,
L_buffer);
if (status_rocsparse != 0) {
printf("status after analysis 1 %d \n", status_rocsparse);
}
status_rocsparse = rocsparse_dcsrsv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
nnzU,
descrU,
ua,
uia,
uja,
infoU,
rocsparse_analysis_policy_reuse,
rocsparse_solve_policy_auto,
U_buffer);
if (status_rocsparse != 0) {
printf("status after analysis 2 %d \n", status_rocsparse);
}
hipDeviceSynchronize();
}
void initialize_ichol(const int n,
const int nnzA,
int *ia,
int *ja,
real_type *a)
{
// printf("initializing ICHOLi, n = %d, nnzA = %d \n",n,nnzA);
/* Create matrix descriptor for M */
rocsparse_status status_rocsparse;
rocsparse_create_mat_descr(&descrM);
rocsparse_set_mat_type(descrM, rocsparse_matrix_type_general);
/* Create matrix descriptor for L */
rocsparse_create_mat_descr(&descrLic);
rocsparse_set_mat_fill_mode(descrLic, rocsparse_fill_mode_lower);
rocsparse_set_mat_diag_type(descrLic, rocsparse_diag_type_non_unit);
rocsparse_set_mat_index_base(descrLic, rocsparse_index_base_zero);
/* Create matrix descriptor for L' */
rocsparse_create_mat_descr(&descrLtic);
rocsparse_set_mat_fill_mode(descrLtic, rocsparse_fill_mode_upper);
rocsparse_set_mat_diag_type(descrLtic, rocsparse_diag_type_non_unit);
rocsparse_set_mat_index_base(descrLtic, rocsparse_index_base_zero);
/* Create matrix info structure */
rocsparse_create_mat_info(&infoM);
rocsparse_create_mat_info(&infoLic);
rocsparse_create_mat_info(&infoLtic);
/* Obtain required buffer size */
size_t buffer_size_M;
size_t buffer_size_L;
size_t buffer_size_Lt;
rocsparse_dcsric0_buffer_size(handle_rocsparse,
n,
nnzA,
descrM,
a,
ia,
ja,
infoM,
&buffer_size_M);
rocsparse_dcsrsv_buffer_size(handle_rocsparse,
rocsparse_operation_none,
n,
nnzA,
descrLic,
a,
ia,
ja,
infoM,
&buffer_size_L);
rocsparse_dcsrsv_buffer_size(handle_rocsparse,
rocsparse_operation_transpose,
n,
nnzA,
descrLic,
a,
ia,
ja,
infoM,
&buffer_size_Lt);
// printf("Buffer sizes: %d %d %d \n", buffer_size_M, buffer_size_L, buffer_size_Lt);
size_t buffer_size = max(buffer_size_M, max(buffer_size_L, buffer_size_Lt));
// printf("finalsize %d \n",buffer_size);
// Allocate temporary buffer
hipMalloc(&ichol_buffer, buffer_size);
/* Perform analysis steps, using rocsparse_analysis_policy_reuse to improve
* computation performance */
status_rocsparse = rocsparse_dcsric0_analysis(handle_rocsparse,
n,
nnzA,
descrM,
a,
ia,
ja,
infoM,
rocsparse_analysis_policy_reuse,
rocsparse_solve_policy_auto,
ichol_buffer);
// printf("status 0: %d \n", status_rocsparse);
status_rocsparse = rocsparse_dcsrsv_analysis(handle_rocsparse,
rocsparse_operation_none,
n,
nnzA,
descrLic,
a,
ia,
ja,
infoM,
rocsparse_analysis_policy_reuse,
rocsparse_solve_policy_auto,
ichol_buffer);
// printf("status 1: %d \n", status_rocsparse);
status_rocsparse = rocsparse_dcsrsv_analysis(handle_rocsparse,
rocsparse_operation_transpose,
n,
nnzA,
descrLic,
a,
ia,
ja,
infoM,
rocsparse_analysis_policy_reuse,
rocsparse_solve_policy_auto,
ichol_buffer);
// printf("status 2: %d \n", status_rocsparse);
/* Check for zero pivot */
rocsparse_int position;
if (rocsparse_status_zero_pivot == rocsparse_csric0_zero_pivot(handle_rocsparse,
infoM,
&position)) {
printf("A has structural zero at A(%d,%d)\n", position, position);
}
/* Compute incomplete Cholesky factorization M = LL' */
status_rocsparse = rocsparse_dcsric0(handle_rocsparse,
n,
nnzA,
descrM,
a,
ia,
ja,
infoM,
rocsparse_solve_policy_auto,
ichol_buffer);
// printf("status 3: %d \n", status_rocsparse);
/* Check for zero pivot */
if (rocsparse_status_zero_pivot == rocsparse_csric0_zero_pivot(handle_rocsparse,
infoM,
&position)) {
printf("L has structural and/or numerical zero at L(%d,%d)\n",
position,
position);
}
hipDeviceSynchronize();
}
void hip_ichol(const int *ia,
const int *ja,
real_type *a,
const int nnzA,
pdata *prec_data,
real_type *x,
real_type *y)
{
real_type one = 1.0;
rocsparse_status st;
st = rocsparse_dcsrsv_solve(handle_rocsparse,
rocsparse_operation_none,
prec_data->n,
nnzA,
&one,
descrLic,
prec_data->ichol_vals,
ia,
ja,
infoM,
x,//input
prec_data->aux_vec1, //output
rocsparse_solve_policy_auto,
ichol_buffer);
if (st != 0) {
printf("before L^T solve: norm of input %16.16e, norm of output %16.16e\n", hip_dot (prec_data->n, prec_data->aux_vec1, prec_data->aux_vec1), hip_dot (prec_data->n, y, y) );
}
/* Solve L'y = z */
st = rocsparse_dcsrsv_solve(handle_rocsparse,
rocsparse_operation_transpose,
prec_data->n,
nnzA,
&one,
descrLic,
prec_data->ichol_vals,
ia,
ja,
infoM,
prec_data->aux_vec1,
y,
rocsparse_solve_policy_auto,
ichol_buffer);
if (st != 0) {
printf("status L^T solve: %d \n", st);
}
hipDeviceSynchronize();
}
__global__ void hip_vec_vec_kernel(const int n,
const real_type *x,
const real_type *y,
real_type *z){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
while (idx < n) {
z[idx] = x[idx] * y[idx];
idx += blockDim.x * gridDim.x;
}
}
__global__ void hip_vec_reciprocal_kernel(const int n,
const real_type *x,
real_type *z){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
while (idx < n) {
if (x[idx] != 0.0){
z[idx] = 1.0 / x[idx];
} else {
z[idx] = 0.0;
}
idx += blockDim.x * gridDim.x;
}
}
__global__ void hip_vec_sqrt_kernel(const int n,
const real_type *x,
real_type *z){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
while (idx < n) {
if (x[idx] > 0) {
z[idx] = sqrt(x[idx]);
} else {
z[idx] = 0.0;
}
idx += blockDim.x * gridDim.x;
}
}
__global__ void hip_vec_set_kernel(const int n,
real_type value,
real_type *x){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
while (idx < n){
x[idx] = value;
idx += blockDim.x * gridDim.x;
}
}
__global__ void hip_vec_zero_kernel(const int n,
real_type *x){
int idx = blockIdx.x * blockDim.x + threadIdx.x;
while (idx < n){
x[idx] = 0.0;
idx += blockDim.x * gridDim.x;
}
}
real_type hip_dot (const int n, const real_type *v, const real_type *w){
real_type sum;
hipDeviceSynchronize();
rocblas_ddot (handle_rocblas,
n,
v,
1,
w,
1,
&sum);
hipDeviceSynchronize();
return sum;
}
void hip_scal (const int n, const real_type alpha, real_type *v){
rocblas_dscal(handle_rocblas,
n,
&alpha,
v,
1);
hipDeviceSynchronize();
}
void hip_axpy (const int n, const real_type alpha, const real_type *x, real_type *y){
rocblas_daxpy(handle_rocblas,
n,
&alpha,
x,
1,
y,
1);
hipDeviceSynchronize();
}
void hip_csr_matvec(const int n,
const int nnz,
const int *ia,
const int *ja,
const real_type *a,
const real_type *x,
real_type *result,
const real_type *al,
const real_type *bet,
const char *kind){
/* y = alpha *A* x + beta *y */
rocsparse_status st;
hipDeviceSynchronize();
if (strcmp(kind, "A") == 0) {
st = rocsparse_dcsrmv(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
al,
descrA,
a,
ia,
ja,
infoA,
x,
bet,
result);
}
if (strcmp(kind, "L") == 0) {
st = rocsparse_dcsrmv(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
al,
descrL,
a,
ia,
ja,
infoL,
x,
bet,
result);
}
if (strcmp(kind, "U") == 0) {
st = rocsparse_dcsrmv(handle_rocsparse,
rocsparse_operation_none,
n,
n,
nnz,
al,
descrU,
a,
ia,
ja,
infoU,
x,
bet,
result);
}
hipDeviceSynchronize();
// printf("status after mv: %d\n", st);
}
void hip_lower_triangular_solve(const int n,
const int nnzL,
const int *lia,
const int *lja,
const real_type *la,
const real_type *diagonal,
const real_type *x,
real_type *result){
/* compute result = L^{-1}x */
/* we DO NOT assume anything about L diagonal */
/* d_x3 = L^(-1)dx2 */
real_type one = 1.0;
hipDeviceSynchronize();
rocsparse_dcsrsv_solve(handle_rocsparse,
rocsparse_operation_none,
n,
nnzL,
&one,
descrL,
la,
lia,
lja,
infoL,
x,
result,
rocsparse_solve_policy_auto,
L_buffer);
hipDeviceSynchronize();
}
void hip_upper_triangular_solve(const int n,
const int nnzU,
const int *uia,
const int *uja,
const real_type *ua,
const real_type *diagonal,
const real_type *x,
real_type *result){
/* compute result = U^{-1}x */
real_type one = 1.0;
hipDeviceSynchronize();
rocsparse_dcsrsv_solve(handle_rocsparse,
rocsparse_operation_none,
n,
nnzU,
&one,
descrU,
ua,
uia,
uja,
infoU,
x,
result,
rocsparse_solve_policy_auto,
U_buffer);
hipDeviceSynchronize();
}
/* not std blas but needed and embarassingly parallel */
/* hip vec-vec computes an element-wise product (needed for scaling) */
void hip_vec_vec(const int n, const real_type *x, const real_type *y, real_type *res){
hipLaunchKernelGGL(hip_vec_vec_kernel, dim3(n / 1024 + 1), dim3(1024), 0, 0, n, x, y, res);
hipDeviceSynchronize();
}
/*vector reciprocal computes 1./d */
void hip_vector_reciprocal(const int n, const real_type *v, real_type *res){
hipLaunchKernelGGL( hip_vec_reciprocal_kernel,dim3(n / 1024 + 1), dim3(1024), 0, 0, n, v, res);
hipDeviceSynchronize();
}
//vector sqrt takes an sqrt from each vector entry
void hip_vector_sqrt(const int n, const real_type *v, real_type *res){
hipLaunchKernelGGL(hip_vec_sqrt_kernel, dim3(n / 1024 +1), dim3(1024), 0,0,n, v, res);
hipDeviceSynchronize();
}
void hip_vec_copy(const int n, const real_type *src, real_type *dest){
hipMemcpy(dest, src, sizeof(real_type) * n, hipMemcpyDeviceToDevice);
hipDeviceSynchronize();
}
void hip_vec_set(const int n, real_type value, real_type *vec){
hipLaunchKernelGGL(hip_vec_set_kernel,dim3(n/ 1024 + 1), dim3(1024), 0, 0, n, value, vec);
hipDeviceSynchronize();
}
void hip_vec_zero(const int n, real_type *vec){
hipLaunchKernelGGL(hip_vec_zero_kernel,dim3(n / 1024 + 1), dim3(1024), 0, 0, n, vec);
hipDeviceSynchronize();
}