-
Notifications
You must be signed in to change notification settings - Fork 2
/
cusolver.hpp
184 lines (161 loc) · 4.29 KB
/
cusolver.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
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
#if defined(__NVCC__)
#include <cusolverDn.h>
#if 0
void
cusolver_test(int n, half *A, int lda, half *W, int batchSize)
{
}
#endif
void
cusolver_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);
syevjInfo_t syevj_params = NULL;
cusolverDnCreateSyevjInfo(&syevj_params);
double constexpr EPS = (double)std::numeric_limits<double>::epsilon();
cusolverDnXsyevjSetTolerance(syevj_params,EPS*512);
cusolverDnXsyevjSetMaxSweeps(syevj_params,10);
cusolverDnXsyevjSetSortEig(syevj_params,0);
cusolverStatus_t err;
cudaError_t status;
int lwork;
err = cusolverDnDsyevjBatched_bufferSize(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A,
lda,
W,
&lwork,
syevj_params,
batchSize
);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
double *d_work;
status = cudaMalloc((void**)&d_work, sizeof(double)*lwork);
if ( status != cudaSuccess || d_work == NULL ) {
printf("work allocation fault\n"); return;
}
int *d_info;
status = cudaMalloc((void**)&d_info, sizeof(int)*batchSize);
if ( status != cudaSuccess || d_info == NULL ) {
cudaFree(d_work);
printf("devinfo allocation fault\n"); return;
}
err = cusolverDnDsyevjBatched(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_UPPER,
n,
A,
lda,
W,
d_work,
lwork,
d_info,
syevj_params,
batchSize
);
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);
cusolverDnDestroySyevjInfo(syevj_params);
cusolverDnDestroy(cusolverH);
cudaStreamDestroy(stream);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
}
void
cusolver_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);
syevjInfo_t syevj_params = NULL;
cusolverDnCreateSyevjInfo(&syevj_params);
double constexpr EPS = (double)std::numeric_limits<float>::epsilon();
cusolverDnXsyevjSetTolerance(syevj_params,EPS*16);
cusolverDnXsyevjSetMaxSweeps(syevj_params,10);
cusolverDnXsyevjSetSortEig(syevj_params,0);
cusolverStatus_t err;
cudaError_t status;
int lwork;
err = cusolverDnSsyevjBatched_bufferSize(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_LOWER,
n,
A,
lda,
W,
&lwork,
syevj_params,
batchSize
);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
float *s_work;
status = cudaMalloc((void**)&s_work, sizeof(float)*lwork);
if ( status != cudaSuccess || s_work == NULL ) {
printf("work allocation fault\n"); return;
}
int *s_info;
status = cudaMalloc((void**)&s_info, sizeof(int)*batchSize);
if ( status != cudaSuccess || s_info == NULL ) {
cudaFree(s_work);
printf("devinfo allocation fault\n"); return;
}
err = cusolverDnSsyevjBatched(
cusolverH,
CUSOLVER_EIG_MODE_VECTOR,
CUBLAS_FILL_MODE_LOWER,
n,
A,
lda,
W,
s_work,
lwork,
s_info,
syevj_params,
batchSize
);
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]!=0) {
printf("[%06d] Gave up the iteration.\n",i); break;
}
}
}
free(info);
cusolverDnDestroySyevjInfo(syevj_params);
cusolverDnDestroy(cusolverH);
cudaStreamDestroy(stream);
if ( err != CUSOLVER_STATUS_SUCCESS ) { return; }
}
#endif