ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
cusolver.h
Go to the documentation of this file.
1#ifndef BASE_THIRD_PARTY_CUSOLVER_H_
2#define BASE_THIRD_PARTY_CUSOLVER_H_
3
4#include <cublas_v2.h>
5#include <cuda_runtime.h>
6#include <iostream>
7#include <memory>
8
9// #include <base/third_party/cusolver_utils.h> // traits, needed if generic API is used.
10// header provided by cusolver, including some data types and macros.
11// see https://github.com/NVIDIA/CUDALibrarySamples/blob/master/cuSOLVER/utils/cusolver_utils.h
12// The cuSolverDN library provides two different APIs; legacy and generic.
13// https://docs.nvidia.com/cuda/cusolver/index.html#naming-conventions
14// now only legacy APIs are used, while the general APIs have the potential to simplify code implementation.
15// for example, cucusolverDnXpotrf/getrf/geqrf/sytrf
16// More tests are needed to confirm that the generic APIs are operating normally, as they are not yet fully supported.
17
18#include <base/macros/cuda.h>
19
20namespace container {
22
24 void operator()(void* ptr) const noexcept {
25 if (ptr) cudaFree(ptr);
26 }
27};
28
29template <typename T>
30using unique_cuda_ptr = std::unique_ptr<T, CudaDeleter>;
31
32
33static inline
34void potri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, float * A, const int& lda)
35{
36 int lwork;
37 CHECK_CUSOLVER(cusolverDnSpotri_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, A, n, &lwork));
38 float* raw_work = nullptr;
39 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(float)));
40 unique_cuda_ptr<float> work(raw_work);
41 CHECK_CUSOLVER(cusolverDnSpotri(cusolver_handle, cublas_fill_mode(uplo), n, A, n, work.get(), lwork, nullptr));
42}
43static inline
44void potri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, double * A, const int& lda)
45{
46 int lwork;
47 CHECK_CUSOLVER(cusolverDnDpotri_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, A, n, &lwork));
48 double* raw_work = nullptr;
49 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(double)));
50 unique_cuda_ptr<double> work(raw_work);
51 CHECK_CUSOLVER(cusolverDnDpotri(cusolver_handle, cublas_fill_mode(uplo), n, A, n, work.get(), lwork, nullptr));
52}
53static inline
54void potri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, std::complex<float> * A, const int& lda)
55{
56 int lwork;
57 CHECK_CUSOLVER(cusolverDnCpotri_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuComplex *>(A), n, &lwork));
58 cuComplex* raw_work = nullptr;
59 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(cuComplex)));
60 unique_cuda_ptr<cuComplex> work(raw_work);
61 CHECK_CUSOLVER(cusolverDnCpotri(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuComplex *>(A), n, work.get(), lwork, nullptr));
62}
63static inline
64void potri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, std::complex<double> * A, const int& lda)
65{
66 int lwork;
67 CHECK_CUSOLVER(cusolverDnZpotri_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuDoubleComplex *>(A), n, &lwork));
68 cuDoubleComplex* raw_work = nullptr;
69 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(cuDoubleComplex)));
71 CHECK_CUSOLVER(cusolverDnZpotri(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuDoubleComplex *>(A), n, work.get(), lwork, nullptr));
72}
73
74
75static inline
76void potrf (cusolverDnHandle_t& cusolver_handle, const char& uplo, const int& n, float * A, const int& lda)
77{
78 int lwork;
79 int* raw_info = nullptr;
80 CHECK_CUDA(cudaMalloc((void**)&raw_info, 1 * sizeof(int)));
81 unique_cuda_ptr<int> info(raw_info);
82 CHECK_CUSOLVER(cusolverDnSpotrf_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, A, n, &lwork));
83 float* raw_work = nullptr;
84 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(float)));
85 unique_cuda_ptr<float> work(raw_work);
86 CHECK_CUSOLVER(cusolverDnSpotrf(cusolver_handle, cublas_fill_mode(uplo), n, A, n, work.get(), lwork, info.get()));
87}
88static inline
89void potrf (cusolverDnHandle_t& cusolver_handle, const char& uplo, const int& n, double * A, const int& lda)
90{
91 int lwork;
92 int* raw_info = nullptr;
93 CHECK_CUDA(cudaMalloc((void**)&raw_info, 1 * sizeof(int)));
94 unique_cuda_ptr<int> info(raw_info);
95 CHECK_CUSOLVER(cusolverDnDpotrf_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, A, n, &lwork));
96 double* raw_work = nullptr;
97 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(double)));
98 unique_cuda_ptr<double> work(raw_work);
99 CHECK_CUSOLVER(cusolverDnDpotrf(cusolver_handle, cublas_fill_mode(uplo), n, A, n, work.get(), lwork, info.get()));
100}
101static inline
102void potrf (cusolverDnHandle_t& cusolver_handle, const char& uplo, const int& n, std::complex<float> * A, const int& lda)
103{
104 int lwork;
105 int* raw_info = nullptr;
106 CHECK_CUDA(cudaMalloc((void**)&raw_info, 1 * sizeof(int)));
107 unique_cuda_ptr<int> info(raw_info);
108 CHECK_CUSOLVER(cusolverDnCpotrf_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuComplex*>(A), lda, &lwork));
109 cuComplex* raw_work = nullptr;
110 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(cuComplex)));
111 unique_cuda_ptr<cuComplex> work(raw_work);
112 CHECK_CUSOLVER(cusolverDnCpotrf(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuComplex*>(A), lda, work.get(), lwork, info.get()));
113}
114static inline
115void potrf (cusolverDnHandle_t& cusolver_handle, const char& uplo, const int& n, std::complex<double> * A, const int& lda)
116{
117 int lwork;
118 int* raw_info = nullptr;
119 CHECK_CUDA(cudaMalloc((void**)&raw_info, 1 * sizeof(int)));
120 unique_cuda_ptr<int> info(raw_info);
121 CHECK_CUSOLVER(cusolverDnZpotrf_bufferSize(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuDoubleComplex*>(A), lda, &lwork));
122 cuDoubleComplex* raw_work = nullptr;
123 CHECK_CUDA(cudaMalloc((void**)&raw_work, lwork * sizeof(cuDoubleComplex)));
125 CHECK_CUSOLVER(cusolverDnZpotrf(cusolver_handle, cublas_fill_mode(uplo), n, reinterpret_cast<cuDoubleComplex*>(A), lda, work.get(), lwork, info.get()));
126}
127
128
129static inline
130void heevd (cusolverDnHandle_t& cusolver_handle, const char& jobz, const char& uplo, const int& n, float* A, const int& lda, float * W)
131{
132 int lwork = 0;
133 int h_info = 0;
134 int* raw_d_info = nullptr;
135 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
136 unique_cuda_ptr<int> d_info(raw_d_info);
137
138 CHECK_CUSOLVER(cusolverDnSsyevd_bufferSize(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
139 n, A, lda, W, &lwork));
140 float* raw_d_work = nullptr;
141 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(float) * lwork));
142 unique_cuda_ptr<float> d_work(raw_d_work);
143 CHECK_CUSOLVER(cusolverDnSsyevd(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
144 n, A, lda, W, d_work.get(), lwork, d_info.get()));
145
146 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
147 if (h_info != 0) {
148 throw std::runtime_error("heevd: failed to invert matrix");
149 }
150}
151static inline
152void heevd (cusolverDnHandle_t& cusolver_handle, const char& jobz, const char& uplo, const int& n, double* A, const int& lda, double * W)
153{
154 int lwork = 0;
155 int h_info = 0;
156 int* raw_d_info = nullptr;
157 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
158 unique_cuda_ptr<int> d_info(raw_d_info);
159
160 CHECK_CUSOLVER(cusolverDnDsyevd_bufferSize(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
161 n, A, lda, W, &lwork));
162 double* raw_d_work = nullptr;
163 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(double) * lwork));
164 unique_cuda_ptr<double> d_work(raw_d_work);
165 CHECK_CUSOLVER(cusolverDnDsyevd(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
166 n, A, lda, W, d_work.get(), lwork, d_info.get()));
167
168 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
169 if (h_info != 0) {
170 throw std::runtime_error("heevd: failed to invert matrix");
171 }
172}
173static inline
174void heevd (cusolverDnHandle_t& cusolver_handle, const char& jobz, const char& uplo, const int& n, std::complex<float>* A, const int& lda, float * W)
175{
176 int lwork = 0;
177 int h_info = 0;
178 int* raw_d_info = nullptr;
179 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
180 unique_cuda_ptr<int> d_info(raw_d_info);
181
182 CHECK_CUSOLVER(cusolverDnCheevd_bufferSize(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
183 n, reinterpret_cast<cuComplex*>(A), lda, W, &lwork));
184 cuComplex* raw_d_work = nullptr;
185 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuComplex) * lwork));
186 unique_cuda_ptr<cuComplex> d_work(raw_d_work);
187 CHECK_CUSOLVER(cusolverDnCheevd(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
188 n, reinterpret_cast<cuComplex*>(A), lda, W, d_work.get(), lwork, d_info.get()));
189
190 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
191 if (h_info != 0) {
192 throw std::runtime_error("heevd: failed to invert matrix");
193 }
194}
195static inline
196void heevd (cusolverDnHandle_t& cusolver_handle, const char& jobz, const char& uplo, const int& n, std::complex<double>* A, const int& lda, double* W)
197{
198 int lwork = 0;
199 int h_info = 0;
200 int* raw_d_info = nullptr;
201 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
202 unique_cuda_ptr<int> d_info(raw_d_info);
203
204 CHECK_CUSOLVER(cusolverDnZheevd_bufferSize(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
205 n, reinterpret_cast<cuDoubleComplex*>(A), lda, W, &lwork));
206 cuDoubleComplex* raw_d_work = nullptr;
207 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuDoubleComplex) * lwork));
208 unique_cuda_ptr<cuDoubleComplex> d_work(raw_d_work);
209 CHECK_CUSOLVER(cusolverDnZheevd(cusolver_handle, cublas_eig_mode(jobz), cublas_fill_mode(uplo),
210 n, reinterpret_cast<cuDoubleComplex*>(A), lda, W, d_work.get(), lwork, d_info.get()));
211
212 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
213 if (h_info != 0) {
214 throw std::runtime_error("heevd: failed to invert matrix");
215 }
216}
217
218// =====================================================================================================
219// heevdx: Compute eigenvalues and eigenvectors of symmetric/Hermitian matrix
220// =====================================================================================================
221// --- float ---
222static inline
223void heevdx(cusolverDnHandle_t& cusolver_handle,
224 const int n,
225 const int lda,
226 float* d_A,
227 const char jobz,
228 const char uplo,
229 const char range,
230 const int il, const int iu,
231 const float vl, const float vu,
232 float* d_eigen_val,
233 int* h_meig)
234{
235 int lwork = 0;
236 int* raw_d_info = nullptr;
237 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
238 unique_cuda_ptr<int> d_info(raw_d_info);
239
240 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
241 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
242 cusolverEigRange_t range_t = cublas_eig_range(range);
243
244 CHECK_CUSOLVER(cusolverDnSsyevdx_bufferSize(
245 cusolver_handle,
246 jobz_t, range_t, uplo_t,
247 n, d_A, lda,
248 vl, vu, il, iu,
249 h_meig,
250 d_eigen_val,
251 &lwork
252 ));
253
254 float* raw_d_work = nullptr;
255 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(float) * lwork));
256 unique_cuda_ptr<float> d_work(raw_d_work);
257
258 CHECK_CUSOLVER(cusolverDnSsyevdx(
259 cusolver_handle,
260 jobz_t, range_t, uplo_t,
261 n,
262 d_A, lda,
263 vl, vu, il, iu,
264 h_meig,
265 d_eigen_val,
266 d_work.get(), lwork,
267 d_info.get()
268 ));
269
270 int h_info = 0;
271 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
272 if (h_info != 0) {
273 throw std::runtime_error("heevdx (float) failed with info = " + std::to_string(h_info));
274 }
275}
276
277// --- double ---
278static inline
279void heevdx(cusolverDnHandle_t& cusolver_handle,
280 const int n,
281 const int lda,
282 double* d_A,
283 const char jobz,
284 const char uplo,
285 const char range,
286 const int il, const int iu,
287 const double vl, const double vu,
288 double* d_eigen_val,
289 int* h_meig)
290{
291 int lwork = 0;
292 int* raw_d_info = nullptr;
293 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
294 unique_cuda_ptr<int> d_info(raw_d_info);
295
296 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
297 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
298 cusolverEigRange_t range_t = cublas_eig_range(range);
299
300 CHECK_CUSOLVER(cusolverDnDsyevdx_bufferSize(
301 cusolver_handle,
302 jobz_t, range_t, uplo_t,
303 n, d_A, lda,
304 vl, vu, il, iu,
305 h_meig,
306 d_eigen_val,
307 &lwork
308 ));
309
310 double* raw_d_work = nullptr;
311 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(double) * lwork));
312 unique_cuda_ptr<double> d_work(raw_d_work);
313
314 CHECK_CUSOLVER(cusolverDnDsyevdx(
315 cusolver_handle,
316 jobz_t, range_t, uplo_t,
317 n,
318 d_A, lda,
319 vl, vu, il, iu,
320 h_meig,
321 d_eigen_val,
322 d_work.get(), lwork,
323 d_info.get()
324 ));
325
326 int h_info = 0;
327 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
328 if (h_info != 0) {
329 throw std::runtime_error("heevdx (double) failed with info = " + std::to_string(h_info));
330 }
331}
332
333// --- complex<float> ---
334static inline
335void heevdx(cusolverDnHandle_t& cusolver_handle,
336 const int n,
337 const int lda,
338 std::complex<float>* d_A,
339 const char jobz,
340 const char uplo,
341 const char range,
342 const int il, const int iu,
343 const float vl, const float vu,
344 float* d_eigen_val,
345 int* h_meig)
346{
347 int lwork = 0;
348 int* raw_d_info = nullptr;
349 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
350 unique_cuda_ptr<int> d_info(raw_d_info);
351
352 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
353 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
354 cusolverEigRange_t range_t = cublas_eig_range(range);
355
356 CHECK_CUSOLVER(cusolverDnCheevdx_bufferSize(
357 cusolver_handle,
358 jobz_t, range_t, uplo_t,
359 n,
360 reinterpret_cast<cuComplex*>(d_A), lda,
361 vl, vu, il, iu,
362 h_meig,
363 d_eigen_val,
364 &lwork
365 ));
366
367 cuComplex* raw_d_work = nullptr;
368 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuComplex) * lwork));
369 unique_cuda_ptr<cuComplex> d_work(raw_d_work);
370
371 CHECK_CUSOLVER(cusolverDnCheevdx(
372 cusolver_handle,
373 jobz_t, range_t, uplo_t,
374 n,
375 reinterpret_cast<cuComplex*>(d_A), lda,
376 vl, vu, il, iu,
377 h_meig,
378 d_eigen_val,
379 d_work.get(), lwork,
380 d_info.get()
381 ));
382
383 int h_info = 0;
384 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
385 if (h_info != 0) {
386 throw std::runtime_error("heevdx (complex<float>) failed with info = " + std::to_string(h_info));
387 }
388}
389
390// --- complex<double> ---
391static inline
392void heevdx(cusolverDnHandle_t& cusolver_handle,
393 const int n,
394 const int lda,
395 std::complex<double>* d_A,
396 const char jobz,
397 const char uplo,
398 const char range,
399 const int il, const int iu,
400 const double vl, const double vu,
401 double* d_eigen_val,
402 int* h_meig)
403{
404 int lwork = 0;
405 int* raw_d_info = nullptr;
406 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
407 unique_cuda_ptr<int> d_info(raw_d_info);
408
409 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
410 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
411 cusolverEigRange_t range_t = cublas_eig_range(range);
412
413 CHECK_CUSOLVER(cusolverDnZheevdx_bufferSize(
414 cusolver_handle,
415 jobz_t, range_t, uplo_t,
416 n,
417 reinterpret_cast<cuDoubleComplex*>(d_A), lda,
418 vl, vu, il, iu,
419 h_meig,
420 d_eigen_val,
421 &lwork
422 ));
423
424 cuDoubleComplex* raw_d_work = nullptr;
425 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuDoubleComplex) * lwork));
426 unique_cuda_ptr<cuDoubleComplex> d_work(raw_d_work);
427
428 CHECK_CUSOLVER(cusolverDnZheevdx(
429 cusolver_handle,
430 jobz_t, range_t, uplo_t,
431 n,
432 reinterpret_cast<cuDoubleComplex*>(d_A), lda,
433 vl, vu, il, iu,
434 h_meig,
435 d_eigen_val,
436 d_work.get(), lwork,
437 d_info.get()
438 ));
439
440 int h_info = 0;
441 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
442 if (h_info != 0) {
443 throw std::runtime_error("heevdx (complex<double>) failed with info = " + std::to_string(h_info));
444 }
445}
446
447static inline
448void hegvd (cusolverDnHandle_t& cusolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, float* A, const int& lda, float* B, const int& ldb, float * W)
449{
450 int lwork = 0;
451 int h_info = 0;
452 int* raw_d_info = nullptr;
453 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
454 unique_cuda_ptr<int> d_info(raw_d_info);
455
456 CHECK_CUSOLVER(cusolverDnSsygvd_bufferSize(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
457 n, A, lda, B, ldb, W, &lwork));
458 float* raw_d_work = nullptr;
459 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(float) * lwork));
460 unique_cuda_ptr<float> d_work(raw_d_work);
461 CHECK_CUSOLVER(cusolverDnSsygvd(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
462 n, A, lda, B, ldb, W, d_work.get(), lwork, d_info.get()));
463
464 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
465 if (h_info != 0) {
466 throw std::runtime_error("heevd: failed to invert matrix");
467 }
468}
469static inline
470void hegvd (cusolverDnHandle_t& cusolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, double* A, const int& lda, double* B, const int& ldb, double * W)
471{
472 int lwork = 0;
473 int h_info = 0;
474 int* raw_d_info = nullptr;
475 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
476 unique_cuda_ptr<int> d_info(raw_d_info);
477
478 CHECK_CUSOLVER(cusolverDnDsygvd_bufferSize(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
479 n, A, lda, B, ldb, W, &lwork));
480 double* raw_d_work = nullptr;
481 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(double) * lwork));
482 unique_cuda_ptr<double> d_work(raw_d_work);
483 CHECK_CUSOLVER(cusolverDnDsygvd(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
484 n, A, lda, B, ldb, W, d_work.get(), lwork, d_info.get()));
485
486 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
487 if (h_info != 0) {
488 throw std::runtime_error("heevd: failed to invert matrix");
489 }
490}
491static inline
492void hegvd (cusolverDnHandle_t& cusolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, std::complex<float>* A, const int& lda, std::complex<float>* B, const int& ldb, float* W)
493{
494 int lwork = 0;
495 int h_info = 0;
496 int* raw_d_info = nullptr;
497 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
498 unique_cuda_ptr<int> d_info(raw_d_info);
499
500 CHECK_CUSOLVER(cusolverDnChegvd_bufferSize(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
501 n, reinterpret_cast<cuComplex*>(A), lda, reinterpret_cast<cuComplex*>(B), ldb, W, &lwork));
502 cuComplex* raw_d_work = nullptr;
503 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuComplex) * lwork));
504 unique_cuda_ptr<cuComplex> d_work(raw_d_work);
505 CHECK_CUSOLVER(cusolverDnChegvd(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
506 n, reinterpret_cast<cuComplex*>(A), lda, reinterpret_cast<cuComplex*>(B), ldb, W, d_work.get(), lwork, d_info.get()));
507
508 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
509 if (h_info != 0) {
510 throw std::runtime_error("heevd: failed to invert matrix");
511 }
512}
513static inline
514void hegvd (cusolverDnHandle_t& cusolver_handle, const int& itype, const char& jobz, const char& uplo, const int& n, std::complex<double>* A, const int& lda, std::complex<double>* B, const int& ldb, double* W)
515{
516 int lwork = 0;
517 int h_info = 0;
518 int* raw_d_info = nullptr;
519 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
520 unique_cuda_ptr<int> d_info(raw_d_info);
521
522 CHECK_CUSOLVER(cusolverDnZhegvd_bufferSize(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
523 n, reinterpret_cast<cuDoubleComplex*>(A), lda, reinterpret_cast<cuDoubleComplex*>(B), ldb, W, &lwork));
524 cuDoubleComplex* raw_d_work = nullptr;
525 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuDoubleComplex) * lwork));
526 unique_cuda_ptr<cuDoubleComplex> d_work(raw_d_work);
527 CHECK_CUSOLVER(cusolverDnZhegvd(cusolver_handle, cublas_eig_type(itype), cublas_eig_mode(jobz), cublas_fill_mode(uplo),
528 n, reinterpret_cast<cuDoubleComplex*>(A), lda, reinterpret_cast<cuDoubleComplex*>(B), ldb, W, d_work.get(), lwork, d_info.get()));
529
530 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
531 if (h_info != 0) {
532 throw std::runtime_error("heevd: failed to invert matrix");
533 }
534}
535
536// =====================================================================================================
537// hegvd x: Compute selected eigenvalues and eigenvectors of generalized Hermitian-definite eigenproblem
538// A * x = lambda * B * x
539// =====================================================================================================
540
541// --- float ---
542static inline
543void hegvdx(
544 cusolverDnHandle_t& cusolver_handle,
545 const int itype,
546 const char jobz,
547 const char range,
548 const char uplo,
549 const int n,
550 const int lda,
551 float* d_A,
552 float* d_B,
553 const float vl,
554 const float vu,
555 const int il,
556 const int iu,
557 int* h_meig,
558 float* d_eigen_val,
559 float* d_eigen_vec
560) {
561 int lwork = 0;
562 int *raw_d_info = nullptr;
563 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
564 unique_cuda_ptr<int> d_info(raw_d_info);
565
566 float *raw_d_A_copy = nullptr, *raw_d_B_copy = nullptr;
567 CHECK_CUDA(cudaMalloc((void**)&raw_d_A_copy, sizeof(float) * n * lda));
568 unique_cuda_ptr<float> d_A_copy(raw_d_A_copy);
569 CHECK_CUDA(cudaMalloc((void**)&raw_d_B_copy, sizeof(float) * n * lda));
570 unique_cuda_ptr<float> d_B_copy(raw_d_B_copy);
571 CHECK_CUDA(cudaMemcpy(d_A_copy.get(), d_A, sizeof(float) * n * lda, cudaMemcpyDeviceToDevice));
572 CHECK_CUDA(cudaMemcpy(d_B_copy.get(), d_B, sizeof(float) * n * lda, cudaMemcpyDeviceToDevice));
573
574 cusolverEigType_t itype_t = cublas_eig_type(itype);
575 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
576 cusolverEigRange_t range_t = cublas_eig_range(range);
577 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
578
579 CHECK_CUSOLVER(cusolverDnSsygvdx_bufferSize(
580 cusolver_handle,
581 itype_t, jobz_t, range_t, uplo_t,
582 n,
583 d_A_copy.get(), lda,
584 d_B_copy.get(), lda,
585 vl, vu, il, iu,
586 h_meig,
587 d_eigen_val,
588 &lwork
589 ));
590
591 float* raw_d_work = nullptr;
592 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(float) * lwork));
593 unique_cuda_ptr<float> d_work(raw_d_work);
594
595 CHECK_CUSOLVER(cusolverDnSsygvdx(
596 cusolver_handle,
597 itype_t, jobz_t, range_t, uplo_t,
598 n,
599 d_A_copy.get(), lda,
600 d_B_copy.get(), lda,
601 vl, vu, il, iu,
602 h_meig,
603 d_eigen_val,
604 d_work.get(), lwork,
605 d_info.get()
606 ));
607
608 int h_info = 0;
609 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
610 if (h_info < 0) {
611 throw std::runtime_error("hegvdx (float): illegal argument #" + std::to_string(-h_info));
612 } else if (h_info > 0) {
613 if (jobz_t == CUSOLVER_EIG_MODE_NOVECTOR && h_info <= n) {
614 throw std::runtime_error("hegvdx (float): failed to converge, " + std::to_string(h_info) + " off-diagonal elements didn't converge");
615 } else if (h_info > n) {
616 throw std::runtime_error("hegvdx (float): leading minor of order " + std::to_string(h_info - n) + " of B is not positive definite");
617 }
618 }
619
620 if (jobz == 'V') {
621 const int m = (*h_meig);
622 CHECK_CUDA(cudaMemcpy(d_eigen_vec, d_A_copy.get(), sizeof(float) * n * m, cudaMemcpyDeviceToDevice));
623 }
624}
625
626
627// --- double ---
628static inline
629void hegvdx(
630 cusolverDnHandle_t& cusolver_handle,
631 const int itype,
632 const char jobz,
633 const char range,
634 const char uplo,
635 const int n,
636 const int lda,
637 double* d_A,
638 double* d_B,
639 const double vl,
640 const double vu,
641 const int il,
642 const int iu,
643 int* h_meig,
644 double* d_eigen_val,
645 double* d_eigen_vec
646) {
647 int lwork = 0;
648 int *raw_d_info = nullptr;
649 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
650 unique_cuda_ptr<int> d_info(raw_d_info);
651
652 double *raw_d_A_copy = nullptr, *raw_d_B_copy = nullptr;
653 CHECK_CUDA(cudaMalloc((void**)&raw_d_A_copy, sizeof(double) * n * lda));
654 unique_cuda_ptr<double> d_A_copy(raw_d_A_copy);
655 CHECK_CUDA(cudaMalloc((void**)&raw_d_B_copy, sizeof(double) * n * lda));
656 unique_cuda_ptr<double> d_B_copy(raw_d_B_copy);
657 CHECK_CUDA(cudaMemcpy(d_A_copy.get(), d_A, sizeof(double) * n * lda, cudaMemcpyDeviceToDevice));
658 CHECK_CUDA(cudaMemcpy(d_B_copy.get(), d_B, sizeof(double) * n * lda, cudaMemcpyDeviceToDevice));
659
660 cusolverEigType_t itype_t = cublas_eig_type(itype);
661 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
662 cusolverEigRange_t range_t = cublas_eig_range(range);
663 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
664
665 CHECK_CUSOLVER(cusolverDnDsygvdx_bufferSize(
666 cusolver_handle,
667 itype_t, jobz_t, range_t, uplo_t,
668 n,
669 d_A_copy.get(), lda,
670 d_B_copy.get(), lda,
671 vl, vu, il, iu,
672 h_meig,
673 d_eigen_val,
674 &lwork
675 ));
676
677 double* raw_d_work = nullptr;
678 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(double) * lwork));
679 unique_cuda_ptr<double> d_work(raw_d_work);
680
681 CHECK_CUSOLVER(cusolverDnDsygvdx(
682 cusolver_handle,
683 itype_t, jobz_t, range_t, uplo_t,
684 n,
685 d_A_copy.get(), lda,
686 d_B_copy.get(), lda,
687 vl, vu, il, iu,
688 h_meig,
689 d_eigen_val,
690 d_work.get(), lwork,
691 d_info.get()
692 ));
693
694 int h_info = 0;
695 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
696 if (h_info < 0) {
697 throw std::runtime_error("hegvdx (double): illegal argument #" + std::to_string(-h_info));
698 } else if (h_info > 0) {
699 if (jobz_t == CUSOLVER_EIG_MODE_NOVECTOR && h_info <= n) {
700 throw std::runtime_error("hegvdx (double): failed to converge, " + std::to_string(h_info) + " off-diagonal elements didn't converge");
701 } else if (h_info > n) {
702 throw std::runtime_error("hegvdx (double): leading minor of order " + std::to_string(h_info - n) + " of B is not positive definite");
703 }
704 }
705
706 if (jobz == 'V') {
707 const int m = (*h_meig);
708 CHECK_CUDA(cudaMemcpy(d_eigen_vec, d_A_copy.get(), sizeof(double) * n * m, cudaMemcpyDeviceToDevice));
709 }
710}
711
712
713// --- complex<float> ---
714static inline
715void hegvdx(
716 cusolverDnHandle_t& cusolver_handle,
717 const int itype,
718 const char jobz,
719 const char range,
720 const char uplo,
721 const int n,
722 const int lda,
723 std::complex<float>* d_A,
724 std::complex<float>* d_B,
725 const float vl,
726 const float vu,
727 const int il,
728 const int iu,
729 int* h_meig,
730 float* d_eigen_val,
731 std::complex<float>* d_eigen_vec
732) {
733 int lwork = 0;
734 int *raw_d_info = nullptr;
735 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
736 unique_cuda_ptr<int> d_info(raw_d_info);
737
738 cuComplex *raw_d_A_copy = nullptr, *raw_d_B_copy = nullptr;
739 CHECK_CUDA(cudaMalloc((void**)&raw_d_A_copy, sizeof(cuComplex) * n * lda));
740 unique_cuda_ptr<cuComplex> d_A_copy(raw_d_A_copy);
741 CHECK_CUDA(cudaMalloc((void**)&raw_d_B_copy, sizeof(cuComplex) * n * lda));
742 unique_cuda_ptr<cuComplex> d_B_copy(raw_d_B_copy);
743 CHECK_CUDA(cudaMemcpy(d_A_copy.get(), reinterpret_cast<cuComplex*>(d_A), sizeof(cuComplex) * n * lda, cudaMemcpyDeviceToDevice));
744 CHECK_CUDA(cudaMemcpy(d_B_copy.get(), reinterpret_cast<cuComplex*>(d_B), sizeof(cuComplex) * n * lda, cudaMemcpyDeviceToDevice));
745
746 cusolverEigType_t itype_t = cublas_eig_type(itype);
747 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
748 cusolverEigRange_t range_t = cublas_eig_range(range);
749 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
750
751 CHECK_CUSOLVER(cusolverDnChegvdx_bufferSize(
752 cusolver_handle,
753 itype_t, jobz_t, range_t, uplo_t,
754 n,
755 d_A_copy.get(), lda,
756 d_B_copy.get(), lda,
757 vl, vu, il, iu,
758 h_meig,
759 d_eigen_val,
760 &lwork
761 ));
762
763 cuComplex* raw_d_work = nullptr;
764 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuComplex) * lwork));
765 unique_cuda_ptr<cuComplex> d_work(raw_d_work);
766
767 CHECK_CUSOLVER(cusolverDnChegvdx(
768 cusolver_handle,
769 itype_t, jobz_t, range_t, uplo_t,
770 n,
771 d_A_copy.get(), lda,
772 d_B_copy.get(), lda,
773 vl, vu, il, iu,
774 h_meig,
775 d_eigen_val,
776 d_work.get(), lwork,
777 d_info.get()
778 ));
779
780 int h_info = 0;
781 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
782 if (h_info < 0) {
783 throw std::runtime_error("hegvdx (complex<float>): illegal argument #" + std::to_string(-h_info));
784 } else if (h_info > 0) {
785 if (jobz_t == CUSOLVER_EIG_MODE_NOVECTOR && h_info <= n) {
786 throw std::runtime_error("hegvdx (complex<float>): failed to converge, " + std::to_string(h_info) + " off-diagonal elements didn't converge");
787 } else if (h_info > n) {
788 throw std::runtime_error("hegvdx (complex<float>): leading minor of order " + std::to_string(h_info - n) + " of B is not positive definite");
789 }
790 }
791
792 if (jobz == 'V') {
793 const int m = (*h_meig);
794 CHECK_CUDA(cudaMemcpy(reinterpret_cast<cuComplex*>(d_eigen_vec), d_A_copy.get(), sizeof(cuComplex) * n * m, cudaMemcpyDeviceToDevice));
795 }
796}
797
798
799// --- complex<double> ---
800static inline
801void hegvdx(
802 cusolverDnHandle_t& cusolver_handle,
803 const int itype,
804 const char jobz,
805 const char range,
806 const char uplo,
807 const int n,
808 const int lda,
809 std::complex<double>* d_A,
810 std::complex<double>* d_B,
811 const double vl,
812 const double vu,
813 const int il,
814 const int iu,
815 int* h_meig,
816 double* d_eigen_val,
817 std::complex<double>* d_eigen_vec
818) {
819 int lwork = 0;
820 int *raw_d_info = nullptr;
821 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
822 unique_cuda_ptr<int> d_info(raw_d_info);
823
824 cuDoubleComplex *raw_d_A_copy = nullptr, *raw_d_B_copy = nullptr;
825 CHECK_CUDA(cudaMalloc((void**)&raw_d_A_copy, sizeof(cuDoubleComplex) * n * lda));
826 unique_cuda_ptr<cuDoubleComplex> d_A_copy(raw_d_A_copy);
827 CHECK_CUDA(cudaMalloc((void**)&raw_d_B_copy, sizeof(cuDoubleComplex) * n * lda));
828 unique_cuda_ptr<cuDoubleComplex> d_B_copy(raw_d_B_copy);
829 CHECK_CUDA(cudaMemcpy(d_A_copy.get(), reinterpret_cast<cuDoubleComplex*>(d_A), sizeof(cuDoubleComplex) * n * lda, cudaMemcpyDeviceToDevice));
830 CHECK_CUDA(cudaMemcpy(d_B_copy.get(), reinterpret_cast<cuDoubleComplex*>(d_B), sizeof(cuDoubleComplex) * n * lda, cudaMemcpyDeviceToDevice));
831
832 cusolverEigType_t itype_t = cublas_eig_type(itype);
833 cusolverEigMode_t jobz_t = cublas_eig_mode(jobz);
834 cusolverEigRange_t range_t = cublas_eig_range(range);
835 cublasFillMode_t uplo_t = cublas_fill_mode(uplo);
836
837 CHECK_CUSOLVER(cusolverDnZhegvdx_bufferSize(
838 cusolver_handle,
839 itype_t, jobz_t, range_t, uplo_t,
840 n,
841 d_A_copy.get(), lda,
842 d_B_copy.get(), lda,
843 vl, vu, il, iu,
844 h_meig,
845 d_eigen_val,
846 &lwork
847 ));
848
849 cuDoubleComplex* raw_d_work = nullptr;
850 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuDoubleComplex) * lwork));
851 unique_cuda_ptr<cuDoubleComplex> d_work(raw_d_work);
852
853 CHECK_CUSOLVER(cusolverDnZhegvdx(
854 cusolver_handle,
855 itype_t, jobz_t, range_t, uplo_t,
856 n,
857 d_A_copy.get(), lda,
858 d_B_copy.get(), lda,
859 vl, vu, il, iu,
860 h_meig,
861 d_eigen_val,
862 d_work.get(), lwork,
863 d_info.get()
864 ));
865
866 int h_info = 0;
867 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
868 if (h_info < 0) {
869 throw std::runtime_error("hegvdx (complex<double>): illegal argument #" + std::to_string(-h_info));
870 } else if (h_info > 0) {
871 if (jobz_t == CUSOLVER_EIG_MODE_NOVECTOR && h_info <= n) {
872 throw std::runtime_error("hegvdx (complex<double>): failed to converge, " + std::to_string(h_info) + " off-diagonal elements didn't converge");
873 } else if (h_info > n) {
874 throw std::runtime_error("hegvdx (complex<double>): leading minor of order " + std::to_string(h_info - n) + " of B is not positive definite");
875 }
876 }
877
878 if (jobz == 'V') {
879 const int m = (*h_meig);
880 CHECK_CUDA(cudaMemcpy(reinterpret_cast<cuDoubleComplex*>(d_eigen_vec), d_A_copy.get(), sizeof(cuDoubleComplex) * n * m, cudaMemcpyDeviceToDevice));
881 }
882}
883
884
885// --- getrf
886static inline
887void getrf(cusolverDnHandle_t& cusolver_handle, const int& m, const int& n, float* A, const int& lda, int* ipiv)
888{
889 int lwork = 0;
890 int h_info = 0;
891 int* raw_d_info = nullptr;
892 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
893 unique_cuda_ptr<int> d_info(raw_d_info);
894
895 CHECK_CUSOLVER(cusolverDnSgetrf_bufferSize(cusolver_handle, m, n, A, lda, &lwork));
896
897 float* raw_d_work = nullptr;
898 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(float) * lwork));
899 unique_cuda_ptr<float> d_work(raw_d_work);
900
901 CHECK_CUSOLVER(cusolverDnSgetrf(cusolver_handle, m, n, A, lda, d_work.get(), ipiv, d_info.get()));
902
903 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
904 if (h_info != 0) {
905 throw std::runtime_error("getrf: failed to compute LU factorization");
906 }
907}
908static inline
909void getrf(cusolverDnHandle_t& cusolver_handle, const int& m, const int& n, double* A, const int& lda, int* ipiv)
910{
911 int lwork = 0;
912 int h_info = 0;
913 int* raw_d_info = nullptr;
914 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
915 unique_cuda_ptr<int> d_info(raw_d_info);
916
917 CHECK_CUSOLVER(cusolverDnDgetrf_bufferSize(cusolver_handle, m, n, A, lda, &lwork));
918
919 double* raw_d_work = nullptr;
920 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(double) * lwork));
921 unique_cuda_ptr<double> d_work(raw_d_work);
922
923 CHECK_CUSOLVER(cusolverDnDgetrf(cusolver_handle, m, n, A, lda, d_work.get(), ipiv, d_info.get()));
924
925 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
926 if (h_info != 0) {
927 throw std::runtime_error("getrf: failed to compute LU factorization");
928 }
929}
930static inline
931void getrf(cusolverDnHandle_t& cusolver_handle, const int& m, const int& n, std::complex<float>* A, const int& lda, int* ipiv)
932{
933 int lwork = 0;
934 int h_info = 0;
935 int* raw_d_info = nullptr;
936 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
937 unique_cuda_ptr<int> d_info(raw_d_info);
938
939 CHECK_CUSOLVER(cusolverDnCgetrf_bufferSize(cusolver_handle, m, n, reinterpret_cast<cuComplex*>(A), lda, &lwork));
940
941 cuComplex* raw_d_work = nullptr;
942 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuComplex) * lwork));
943 unique_cuda_ptr<cuComplex> d_work(raw_d_work);
944
945 CHECK_CUSOLVER(cusolverDnCgetrf(cusolver_handle, m, n, reinterpret_cast<cuComplex*>(A), lda, d_work.get(), ipiv, d_info.get()));
946
947 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
948 if (h_info != 0) {
949 throw std::runtime_error("getrf: failed to compute LU factorization");
950 }
951}
952static inline
953void getrf(cusolverDnHandle_t& cusolver_handle, const int& m, const int& n, std::complex<double>* A, const int& lda, int* ipiv)
954{
955 int lwork = 0;
956 int h_info = 0;
957 int* raw_d_info = nullptr;
958 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
959 unique_cuda_ptr<int> d_info(raw_d_info);
960
961 CHECK_CUSOLVER(cusolverDnZgetrf_bufferSize(cusolver_handle, m, n, reinterpret_cast<cuDoubleComplex*>(A), lda, &lwork));
962
963 cuDoubleComplex* raw_d_work = nullptr;
964 CHECK_CUDA(cudaMalloc((void**)&raw_d_work, sizeof(cuDoubleComplex) * lwork));
965 unique_cuda_ptr<cuDoubleComplex> d_work(raw_d_work);
966
967 CHECK_CUSOLVER(cusolverDnZgetrf(cusolver_handle, m, n, reinterpret_cast<cuDoubleComplex*>(A), lda, d_work.get(), ipiv, d_info.get()));
968
969 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
970 if (h_info != 0) {
971 throw std::runtime_error("getrf: failed to compute LU factorization");
972 }
973}
974
975static inline
976void getrs(cusolverDnHandle_t& cusolver_handle, const char& trans, const int& n, const int& nrhs, float* A, const int& lda, const int* ipiv, float* B, const int& ldb)
977{
978 int h_info = 0;
979 int* raw_d_info = nullptr;
980 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
981 unique_cuda_ptr<int> d_info(raw_d_info);
982
983 CHECK_CUSOLVER(cusolverDnSgetrs(cusolver_handle, GetCublasOperation(trans), n, nrhs, A, lda, ipiv, B, ldb, d_info.get()));
984
985 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
986 if (h_info != 0) {
987 throw std::runtime_error("getrs: failed to solve the linear system");
988 }
989}
990static inline
991void getrs(cusolverDnHandle_t& cusolver_handle, const char& trans, const int& n, const int& nrhs, double* A, const int& lda, const int* ipiv, double* B, const int& ldb)
992{
993 int h_info = 0;
994 int* raw_d_info = nullptr;
995 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
996 unique_cuda_ptr<int> d_info(raw_d_info);
997
998 CHECK_CUSOLVER(cusolverDnDgetrs(cusolver_handle, GetCublasOperation(trans), n, nrhs, A, lda, ipiv, B, ldb, d_info.get()));
999
1000 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1001 if (h_info != 0) {
1002 throw std::runtime_error("getrs: failed to solve the linear system");
1003 }
1004}
1005static inline
1006void getrs(cusolverDnHandle_t& cusolver_handle, const char& trans, const int& n, const int& nrhs, std::complex<float>* A, const int& lda, const int* ipiv, std::complex<float>* B, const int& ldb)
1007{
1008 int h_info = 0;
1009 int* raw_d_info = nullptr;
1010 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
1011 unique_cuda_ptr<int> d_info(raw_d_info);
1012
1013 CHECK_CUSOLVER(cusolverDnCgetrs(cusolver_handle, GetCublasOperation(trans), n, nrhs, reinterpret_cast<cuComplex*>(A), lda, ipiv, reinterpret_cast<cuComplex*>(B), ldb, d_info.get()));
1014
1015 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1016 if (h_info != 0) {
1017 throw std::runtime_error("getrs: failed to solve the linear system");
1018 }
1019}
1020static inline
1021void getrs(cusolverDnHandle_t& cusolver_handle, const char& trans, const int& n, const int& nrhs, std::complex<double>* A, const int& lda, const int* ipiv, std::complex<double>* B, const int& ldb)
1022{
1023 int h_info = 0;
1024 int* raw_d_info = nullptr;
1025 CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
1026 unique_cuda_ptr<int> d_info(raw_d_info);
1027
1028 CHECK_CUSOLVER(cusolverDnZgetrs(cusolver_handle, GetCublasOperation(trans), n, nrhs, reinterpret_cast<cuDoubleComplex*>(A), lda, ipiv, reinterpret_cast<cuDoubleComplex*>(B), ldb, d_info.get()));
1029
1030 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1031 if (h_info != 0) {
1032 throw std::runtime_error("getrs: failed to solve the linear system");
1033 }
1034}
1035
1036// QR decomposition
1037// geqrf, orgqr
1038// Note:
1039// there are two cusolver geqrf
1040// one is cusolverDn<t>geqrf
1041// one is cusolverDnXgeqrf
1042// which one is better?
1043//
1044// template<typename T>
1045// static inline void geqrf(
1046// cusolverDnHandle_t& cusolver_handle,
1047// const int64_t m,
1048// const int64_t n,
1049// T* d_A, // device matrix A (m x n, column-major)
1050// const int64_t lda,
1051// T* d_tau // output: scalar factors of elementary reflectors
1052// ) {
1053// // query workspace size
1054// int *d_info = nullptr; /* error info */
1055//
1056// size_t workspaceInBytesOnDevice = 0; /* size of workspace */
1057// void *d_work = nullptr; /* device workspace */
1058// size_t workspaceInBytesOnHost = 0; /* size of workspace */
1059// void *h_work = nullptr; /* host workspace */
1060//
1061// CHECK_CUDA(cudaMalloc(reinterpret_cast<void **>(&d_info), sizeof(int)));
1062//
1063// cusolverDnParams_t params = NULL;
1064// CHECK_CUSOLVER(cusolverDnCreateParams(&params));
1065//
1066// CHECK_CUSOLVER(cusolverDnXgeqrf_bufferSize(
1067// cusolver_handle,
1068// params,
1069// m, n,
1070// traits<T>::cuda_data_type,
1071// d_A,
1072// lda,
1073// traits<T>::cuda_data_type,
1074// d_tau,
1075// traits<T>::cuda_data_type,
1076// &workspaceInBytesOnDevice,
1077// &workspaceInBytesOnHost
1078// ));
1079//
1080// // allocate device workspace
1081// CHECK_CUDA(cudaMalloc(reinterpret_cast<void **>(&d_work), workspaceInBytesOnDevice));
1082//
1083// // allocate host workspace
1084// if (workspaceInBytesOnHost > 0) {
1085// h_work = reinterpret_cast<void *>(malloc(workspaceInBytesOnHost));
1086// if (h_work == nullptr) {
1087// throw std::runtime_error("Error: h_work not allocated.");
1088// }
1089// }
1090//
1091// // QR factorization
1092// CHECK_CUSOLVER(cusolverDnXgeqrf(
1093// cusolver_handle,
1094// params,
1095// m, n,
1096// traits<T>::cuda_data_type,
1097// d_A,
1098// lda,
1099// traits<T>::cuda_data_type,
1100// d_tau,
1101// traits<T>::cuda_data_type,
1102// d_work,
1103// workspaceInBytesOnDevice,
1104// h_work,
1105// workspaceInBytesOnHost,
1106// d_info
1107// ));
1108//
1109// // check info
1110// int h_info = 0;
1111// CHECK_CUDA(cudaMemcpy(&h_info, d_info, sizeof(int), cudaMemcpyDeviceToHost));
1112// if (h_info != 0) {
1113// // std::printf("%d-th parameter is wrong \n", -info);
1114// // print error message
1115// std::cout << -h_info << "th parameter is wrong" << std::endl;
1116// throw std::runtime_error("geqrf: failed to compute QR decomposition");
1117// }
1118//
1119// // clean workspace
1120// CHECK_CUDA(cudaFree(d_info));
1121// CHECK_CUDA(cudaFree(d_work));
1122// if (h_work) free(h_work);
1123// CHECK_CUSOLVER(cusolverDnDestroyParams(params));
1124// }
1125
1126// geqrf
1127
1128// --- float ---
1129static inline void geqrf(
1130 cusolverDnHandle_t& cusolver_handle,
1131 const int m,
1132 const int n,
1133 float* d_A,
1134 const int lda,
1135 float* d_tau
1136) {
1137 int lwork = 0;
1138 CHECK_CUSOLVER(cusolverDnSgeqrf_bufferSize(
1139 cusolver_handle, m, n, d_A, lda, &lwork));
1140
1142 if (lwork > 0) {
1143 float* raw_d_work = nullptr;
1144 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(float) * lwork));
1145 d_work.reset(raw_d_work);
1146 }
1147 int* raw_d_info = nullptr;
1148 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1149 unique_cuda_ptr<int> d_info(raw_d_info);
1150
1151 CHECK_CUSOLVER(cusolverDnSgeqrf(
1152 cusolver_handle, m, n, d_A, lda, d_tau, d_work.get(), lwork, d_info.get()));
1153
1154 int h_info = 0;
1155 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1156 if (h_info != 0) {
1157 throw std::runtime_error("geqrf (S): QR factorization failed");
1158 }
1159}
1160
1161// --- double ---
1162static inline void geqrf(
1163 cusolverDnHandle_t& cusolver_handle,
1164 const int m,
1165 const int n,
1166 double* d_A,
1167 const int lda,
1168 double* d_tau
1169) {
1170 int lwork = 0;
1171 CHECK_CUSOLVER(cusolverDnDgeqrf_bufferSize(
1172 cusolver_handle, m, n, d_A, lda, &lwork));
1173
1175 if (lwork > 0) {
1176 double* raw_d_work = nullptr;
1177 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(double) * lwork));
1178 d_work.reset(raw_d_work);
1179 }
1180 int* raw_d_info = nullptr;
1181 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1182 unique_cuda_ptr<int> d_info(raw_d_info);
1183
1184 CHECK_CUSOLVER(cusolverDnDgeqrf(
1185 cusolver_handle, m, n, d_A, lda, d_tau, d_work.get(), lwork, d_info.get()));
1186
1187 int h_info = 0;
1188 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1189 if (h_info != 0) {
1190 throw std::runtime_error("geqrf (D): QR factorization failed");
1191 }
1192}
1193
1194// --- std::complex<float> ---
1195static inline void geqrf(
1196 cusolverDnHandle_t& cusolver_handle,
1197 const int m,
1198 const int n,
1199 std::complex<float>* d_A,
1200 const int lda,
1201 std::complex<float>* d_tau
1202) {
1203 int lwork = 0;
1204 CHECK_CUSOLVER(cusolverDnCgeqrf_bufferSize(
1205 cusolver_handle, m, n,
1206 reinterpret_cast<cuComplex*>(d_A),
1207 lda,
1208 &lwork
1209 ));
1210
1212 if (lwork > 0) {
1213 cuComplex* raw_d_work = nullptr;
1214 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(cuComplex) * lwork));
1215 d_work.reset(raw_d_work);
1216 }
1217 int* raw_d_info = nullptr;
1218 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1219 unique_cuda_ptr<int> d_info(raw_d_info);
1220
1221 CHECK_CUSOLVER(cusolverDnCgeqrf(
1222 cusolver_handle, m, n,
1223 reinterpret_cast<cuComplex*>(d_A),
1224 lda,
1225 reinterpret_cast<cuComplex*>(d_tau),
1226 d_work.get(), lwork, d_info.get()));
1227
1228 int h_info = 0;
1229 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1230 if (h_info != 0) {
1231 throw std::runtime_error("geqrf (C): QR factorization failed");
1232 }
1233}
1234
1235// --- std::complex<double> ---
1236static inline void geqrf(
1237 cusolverDnHandle_t& cusolver_handle,
1238 const int m,
1239 const int n,
1240 std::complex<double>* d_A,
1241 const int lda,
1242 std::complex<double>* d_tau
1243) {
1244 int lwork = 0;
1245 CHECK_CUSOLVER(cusolverDnZgeqrf_bufferSize(
1246 cusolver_handle, m, n,
1247 reinterpret_cast<cuDoubleComplex*>(d_A),
1248 lda,
1249 &lwork
1250 ));
1251
1253 if (lwork > 0) {
1254 cuDoubleComplex* raw_d_work = nullptr;
1255 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(cuDoubleComplex) * lwork));
1256 d_work.reset(raw_d_work);
1257 }
1258 int* raw_d_info = nullptr;
1259 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1260 unique_cuda_ptr<int> d_info(raw_d_info);
1261
1262 CHECK_CUSOLVER(cusolverDnZgeqrf(
1263 cusolver_handle, m, n,
1264 reinterpret_cast<cuDoubleComplex*>(d_A),
1265 lda,
1266 reinterpret_cast<cuDoubleComplex*>(d_tau),
1267 d_work.get(), lwork, d_info.get()));
1268
1269 int h_info = 0;
1270 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1271 if (h_info != 0) {
1272 throw std::runtime_error("geqrf (Z): QR factorization failed");
1273 }
1274}
1275
1276
1277// --- float ---
1278static inline void orgqr(
1279 cusolverDnHandle_t& cusolver_handle,
1280 const int m,
1281 const int n,
1282 const int k,
1283 float* d_A,
1284 const int lda,
1285 float* d_tau
1286) {
1287 int lwork = 0;
1288 CHECK_CUSOLVER(cusolverDnSorgqr_bufferSize(
1289 cusolver_handle, m, n, k, d_A, lda, d_tau, &lwork));
1290
1292 if (lwork > 0) {
1293 float* raw_d_work = nullptr;
1294 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(float) * lwork));
1295 d_work.reset(raw_d_work);
1296 }
1297 int* raw_d_info = nullptr;
1298 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1299 unique_cuda_ptr<int> d_info(raw_d_info);
1300
1301 CHECK_CUSOLVER(cusolverDnSorgqr(
1302 cusolver_handle, m, n, k, d_A, lda, d_tau, d_work.get(), lwork, d_info.get()));
1303
1304 int h_info = 0;
1305 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1306 if (h_info != 0) {
1307 throw std::runtime_error("orgqr (S): failed to generate Q matrix");
1308 }
1309}
1310
1311// --- double ---
1312static inline void orgqr(
1313 cusolverDnHandle_t& cusolver_handle,
1314 const int m,
1315 const int n,
1316 const int k,
1317 double* d_A,
1318 const int lda,
1319 double* d_tau
1320) {
1321 int lwork = 0;
1322 CHECK_CUSOLVER(cusolverDnDorgqr_bufferSize(
1323 cusolver_handle, m, n, k, d_A, lda, d_tau, &lwork));
1324
1326 if (lwork > 0) {
1327 double* raw_d_work = nullptr;
1328 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(double) * lwork));
1329 d_work.reset(raw_d_work);
1330 }
1331 int* raw_d_info = nullptr;
1332 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1333 unique_cuda_ptr<int> d_info(raw_d_info);
1334
1335 CHECK_CUSOLVER(cusolverDnDorgqr(
1336 cusolver_handle, m, n, k, d_A, lda, d_tau, d_work.get(), lwork, d_info.get()));
1337
1338 int h_info = 0;
1339 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1340 if (h_info != 0) {
1341 throw std::runtime_error("orgqr (D): failed to generate Q matrix");
1342 }
1343}
1344
1345// --- std::complex<float> ---
1346static inline void orgqr(
1347 cusolverDnHandle_t& cusolver_handle,
1348 const int m,
1349 const int n,
1350 const int k,
1351 std::complex<float>* d_A,
1352 const int lda,
1353 std::complex<float>* d_tau
1354) {
1355 int lwork = 0;
1356 CHECK_CUSOLVER(cusolverDnCungqr_bufferSize(
1357 cusolver_handle, m, n, k,
1358 reinterpret_cast<cuComplex*>(d_A),
1359 lda,
1360 reinterpret_cast<cuComplex*>(d_tau),
1361 &lwork));
1362
1364 if (lwork > 0) {
1365 cuComplex* raw_d_work = nullptr;
1366 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(cuComplex) * lwork));
1367 d_work.reset(raw_d_work);
1368 }
1369 int* raw_d_info = nullptr;
1370 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1371 unique_cuda_ptr<int> d_info(raw_d_info);
1372
1373 CHECK_CUSOLVER(cusolverDnCungqr(
1374 cusolver_handle, m, n, k,
1375 reinterpret_cast<cuComplex*>(d_A),
1376 lda,
1377 reinterpret_cast<cuComplex*>(d_tau),
1378 d_work.get(), lwork, d_info.get()));
1379
1380 int h_info = 0;
1381 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1382 if (h_info != 0) {
1383 throw std::runtime_error("orgqr (C): failed to generate Q matrix");
1384 }
1385}
1386
1387// --- std::complex<double> ---
1388static inline void orgqr(
1389 cusolverDnHandle_t& cusolver_handle,
1390 const int m,
1391 const int n,
1392 const int k,
1393 std::complex<double>* d_A,
1394 const int lda,
1395 std::complex<double>* d_tau
1396) {
1397 int lwork = 0;
1398 CHECK_CUSOLVER(cusolverDnZungqr_bufferSize(
1399 cusolver_handle, m, n, k,
1400 reinterpret_cast<cuDoubleComplex*>(d_A),
1401 lda,
1402 reinterpret_cast<cuDoubleComplex*>(d_tau),
1403 &lwork));
1404
1406 if (lwork > 0) {
1407 cuDoubleComplex* raw_d_work = nullptr;
1408 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_work), sizeof(cuDoubleComplex) * lwork));
1409 d_work.reset(raw_d_work);
1410 }
1411 int* raw_d_info = nullptr;
1412 CHECK_CUDA(cudaMalloc(reinterpret_cast<void**>(&raw_d_info), sizeof(int)));
1413 unique_cuda_ptr<int> d_info(raw_d_info);
1414
1415 CHECK_CUSOLVER(cusolverDnZungqr(
1416 cusolver_handle, m, n, k,
1417 reinterpret_cast<cuDoubleComplex*>(d_A),
1418 lda,
1419 reinterpret_cast<cuDoubleComplex*>(d_tau),
1420 d_work.get(), lwork, d_info.get()));
1421
1422 int h_info = 0;
1423 CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
1424 if (h_info != 0) {
1425 throw std::runtime_error("orgqr (Z): failed to generate Q matrix");
1426 }
1427}
1428
1429
1430} // namespace cuSolverConnector
1431} // namespace container
1432
1433#endif // BASE_THIRD_PARTY_CUSOLVER_H_
Definition cusolver.h:21
std::unique_ptr< T, CudaDeleter > unique_cuda_ptr
Definition cusolver.h:30
Definition tensor.cpp:8
void operator()(void *ptr) const noexcept
Definition cusolver.h:24