-
Notifications
You must be signed in to change notification settings - Fork 2
/
cusolver_evd.hpp
154 lines (131 loc) · 3.39 KB
/
cusolver_evd.hpp
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
#if defined(__NVCC__)
#include <cusolverDn.h>
#if 0
void
cusolver_evd_test(int n, half *A, int lda, half *W, int batchSize)
{
}
#endif
void
cusolver_evd_test(int n, double *A, int lda, double *W, int batchSize)
{
cusolverDnHandle_t cusolverH = NULL;
cusolverDnCreate(&cusolverH);
cudaStream_t stream = NULL;
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
cusolverDnSetStream(cusolverH, stream);
cusolverStatus_t err;
cudaError_t status;
int lwork;
err = cusolverDnDsyevd_bufferSize(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A,
lda,
W,
&lwork
);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
int *d_Info;
double *d_work;
status = cudaMalloc((void**)&d_Info, sizeof(int)*batchSize);
if ( status != cudaSuccess || d_Info == NULL ) { return; }
status = cudaMalloc((void**)&d_work, sizeof(double)*lwork);
if ( status != cudaSuccess || d_work == NULL ) { cudaFree(d_Info); return; }
for(int i=0; i<batchSize; i++) {
err = cusolverDnDsyevd(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A+(size_t)i*(n*lda),
lda,
W+(size_t)i*(n),
d_work,
lwork,
d_Info+i
);
}
cudaFree(d_work);
int * info = (int *)malloc(sizeof(int)*batchSize);
status = cudaMemcpy(info,d_Info,sizeof(int)*batchSize,cudaMemcpyDeviceToHost);
cudaFree(d_Info);
if ( err == CUSOLVER_STATUS_INVALID_VALUE ) {
if ( info[0] < 0 ) {
printf("[%06d]'s parameter was wrong.\n",-info[0]);
}
} else {
for(int i=0;i<batchSize;i++){
if(info[i]==n+1) {
printf("[%06d] Gave up the iteration.\n",i); break;
}
}
}
free(info);
cusolverDnDestroy(cusolverH);
cudaStreamDestroy(stream);
}
void
cusolver_evd_test(int n, float *A, int lda, float *W, int batchSize)
{
cusolverDnHandle_t cusolverH = NULL;
cusolverDnCreate(&cusolverH);
cudaStream_t stream = NULL;
cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
cusolverDnSetStream(cusolverH, stream);
cusolverStatus_t err;
cudaError_t status;
int lwork;
err = cusolverDnSsyevd_bufferSize(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A,
lda,
W,
&lwork
);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
int *s_Info;
float *s_work;
status = cudaMalloc((void**)&s_Info, sizeof(int)*batchSize);
if ( status != cudaSuccess || s_Info == NULL ) { return; }
status = cudaMalloc((void**)&s_work, sizeof(float)*lwork);
if ( status != cudaSuccess || s_work == NULL ) { cudaFree(s_Info); return; }
for(int i=0; i<batchSize; i++) {
err = cusolverDnSsyevd(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A+(size_t)i*(n*lda),
lda,
W+(size_t)i*(n),
s_work,
lwork,
s_Info+i
);
}
cudaFree(s_work);
int * info = (int *)malloc(sizeof(int)*batchSize);
status = cudaMemcpy(info,s_Info,sizeof(int)*batchSize,cudaMemcpyDeviceToHost);
cudaFree(s_Info);
if ( err == CUSOLVER_STATUS_INVALID_VALUE ) {
if ( info[0] < 0 ) {
printf("[%06d]'s parameter was wrong.\n",-info[0]);
}
} else {
for(int i=0;i<batchSize;i++){
if(info[i]==n+1) {
printf("[%06d] Gave up the iteration.\n",i); break;
}
}
}
free(info);
cusolverDnDestroy(cusolverH);
cudaStreamDestroy(stream);
}
#endif