20using cudaError_t = int;
25constexpr int kCudaSuccess = 0;
26constexpr int kCudaMemcpyHostToDevice = 1;
27constexpr int kCudaMemcpyDeviceToHost = 2;
28constexpr int kCusolverStatusSuccess = 0;
29constexpr int kCusolverEigModeVector = 1;
30constexpr int kCublasFillModeLower = 0;
33using cudaMalloc_t = cudaError_t (*)(
void**, std::size_t);
34using cudaFree_t = cudaError_t (*)(
void*);
35using cudaMemcpy_t = cudaError_t (*)(
void*,
const void*, std::size_t, int);
36using cudaDeviceSynchronize_t = cudaError_t (*)();
37using cudaGetDeviceCount_t = cudaError_t (*)(
int*);
38using cudaSetDevice_t = cudaError_t (*)(int);
45 const double* ,
int ,
const double* ,
int* );
70 const double* ,
double* ,
72constexpr int kCublasStatusSuccess = 0;
73constexpr int kCublasOpN = 0;
77bool g_cusolver_eigh_verbose =
false;
83 g_cusolver_eigh_verbose = verbose;
140 if (bytes == 0)
return true;
141 if (s.
ptr !=
nullptr && s.
bytes >= bytes)
return true;
142 if (s.
ptr !=
nullptr) {
148 if (
cudaMalloc(&raw, bytes) != kCudaSuccess || raw ==
nullptr) {
151 s.
ptr =
static_cast<double*
>(raw);
159 template <
typename Fn>
160 static Fn
sym(
void* handle,
const char* name)
162 return reinterpret_cast<Fn
>(dlsym(handle, name));
167 const bool verbose = g_cusolver_eigh_verbose;
176 const int flags = RTLD_LAZY | RTLD_GLOBAL;
177 const char* deps[] = {
"libnvJitLink.so.12",
184 for (
int i = 0; deps[i] !=
nullptr; ++i)
185 dlopen(deps[i], flags);
189 const char* cublas_libs[] = {
"libcublas.so.12",
"libcublas.so",
191 for (
int i = 0; cublas_libs[i] !=
nullptr; ++i) {
196 const char* cudart_libs[] = {
"libcudart.so",
"libcudart.so.12",
197 "libcudart.so.11.0",
"libcudart.so.11",
199 for (
int i = 0; cudart_libs[i] !=
nullptr; ++i) {
203 const char* cusolver_libs[] = {
"libcusolver.so",
"libcusolver.so.11",
204 "libcusolver.so.12",
nullptr};
205 for (
int i = 0; cusolver_libs[i] !=
nullptr; ++i) {
212 "[CusolverEigh] dlopen failed (cudart=%p "
213 "cusolver=%p): %s -> CPU fallback\n",
256 "[CusolverEigh] symbols_loaded=0 "
257 "(cudart=%p cusolver=%p) -> CPU fallback\n",
267 "[CusolverEigh] symbols_loaded=1 "
268 "cudaGetDeviceCount rc=%d count=%d -> %s\n",
274CusolverEighLib::CusolverEighLib() : impl_(new Impl())
279CusolverEighLib::~CusolverEighLib()
286 static CusolverEighLib lib;
292 return impl_ !=
nullptr && impl_->symbols_loaded && impl_->device_present;
296 Eigen::VectorXd& evals,
297 Eigen::MatrixXd& evecs)
const
300 if (sym.rows() != sym.cols())
return false;
301 if (sym.rows() >
static_cast<Eigen::Index
>(std::numeric_limits<int>::max()))
304 const int n =
static_cast<int>(sym.rows());
318 constexpr int kMinGpuEighN = 128;
319 if (n < kMinGpuEighN)
return false;
328 Eigen::MatrixXd a = sym;
329 const std::size_t bytes_a =
static_cast<std::size_t
>(n) *
330 static_cast<std::size_t
>(n) *
sizeof(double);
331 const std::size_t bytes_w =
static_cast<std::size_t
>(n) *
sizeof(
double);
334 double* d_a =
nullptr;
335 double* d_w =
nullptr;
336 double* d_work =
nullptr;
337 int* d_info =
nullptr;
340 auto cleanup = [&]() {
348 const char* fail_step =
nullptr;
350 auto chk = [&](
const char* step,
int rc,
int ok_code) ->
bool {
351 if (rc == ok_code)
return true;
365 kCusolverStatusSuccess))
367 if (!chk(
"reserve(A)", d.
reserve(d.
eig_a, bytes_a) ? 0 : 1, 0))
break;
368 if (!chk(
"reserve(W)", d.
reserve(d.
eig_w, bytes_w) ? 0 : 1, 0))
break;
375 if (!chk(
"cudaMemcpy(A,HtoD)",
376 d.
cudaMemcpy(d_a, a.data(), bytes_a, kCudaMemcpyHostToDevice),
381 if (!chk(
"syevd_bufferSize",
383 kCublasFillModeLower, n, d_a, n,
385 kCusolverStatusSuccess))
388 fail_step =
"lwork<0";
392 if (!chk(
"reserve(work)",
394 static_cast<std::size_t
>(lwork) *
sizeof(
double))
401 if (!chk(
"cusolverDnDsyevd",
403 kCublasFillModeLower, n, d_a, n, d_w,
404 d_work, lwork, d_info),
405 kCusolverStatusSuccess))
412 if (!chk(
"cudaMemcpy(info,DtoH)",
414 kCudaMemcpyDeviceToHost),
418 fail_step =
"syevd_info";
425 if (!chk(
"cudaMemcpy(W,DtoH)",
427 kCudaMemcpyDeviceToHost),
430 if (!chk(
"cudaMemcpy(V,DtoH)",
432 kCudaMemcpyDeviceToHost),
444 if (!ok && g_cusolver_eigh_verbose) {
446 "[CusolverEigh] GPU eigensolve FAILED at %s rc=%d (n=%d) "
448 fail_step ? fail_step :
"?", fail_rc, n);
457 static bool announced =
false;
461 "[CusolverEigh] cuSOLVER syevd active on GPU "
462 "(first solve n=%d)\n",
472 Eigen::MatrixXd& C)
const
474 if (!
available() || !impl_->cublas_symbols_loaded)
return false;
475 if (A.cols() != B.rows())
return false;
476 const Eigen::Index m64 = A.rows();
477 const Eigen::Index k64 = A.cols();
478 const Eigen::Index n64 = B.cols();
479 constexpr Eigen::Index kIntMax =
480 static_cast<Eigen::Index
>(std::numeric_limits<int>::max());
481 if (m64 > kIntMax || k64 > kIntMax || n64 > kIntMax)
return false;
484 constexpr int kMinGpuGemmDim = 128;
485 if (m64 < kMinGpuGemmDim || k64 < kMinGpuGemmDim || n64 < kMinGpuGemmDim)
487 const int m =
static_cast<int>(m64);
488 const int k =
static_cast<int>(k64);
489 const int n =
static_cast<int>(n64);
492 const std::size_t bytes_a =
static_cast<std::size_t
>(m) *
493 static_cast<std::size_t
>(k) *
sizeof(double);
494 const std::size_t bytes_b =
static_cast<std::size_t
>(k) *
495 static_cast<std::size_t
>(n) *
sizeof(double);
496 const std::size_t bytes_c =
static_cast<std::size_t
>(m) *
497 static_cast<std::size_t
>(n) *
sizeof(double);
499 double* d_a =
nullptr;
500 double* d_b =
nullptr;
501 double* d_c =
nullptr;
505 auto cleanup = [] {};
507 const char* fail_step =
nullptr;
509 auto chk = [&](
const char* step,
int rc,
int ok_code) ->
bool {
510 if (rc == ok_code)
return true;
523 kCublasStatusSuccess)) {
529 if (!chk(
"reserve(A)", d.
reserve(d.
gemm_a, bytes_a) ? 0 : 1, 0))
break;
530 if (!chk(
"reserve(B)", d.
reserve(d.
gemm_b, bytes_b) ? 0 : 1, 0))
break;
531 if (!chk(
"reserve(C)", d.
reserve(d.
gemm_c, bytes_c) ? 0 : 1, 0))
break;
535 if (!chk(
"cudaMemcpy(A,HtoD)",
536 d.
cudaMemcpy(d_a, A.data(), bytes_a, kCudaMemcpyHostToDevice),
539 if (!chk(
"cudaMemcpy(B,HtoD)",
540 d.
cudaMemcpy(d_b, B.data(), bytes_b, kCudaMemcpyHostToDevice),
544 const double alpha = 1.0;
545 const double beta = 0.0;
546 if (!chk(
"cublasDgemm",
548 &alpha, d_a, m, d_b, k, &beta, d_c, m),
549 kCublasStatusSuccess))
556 if (!chk(
"cudaMemcpy(C,DtoH)",
557 d.
cudaMemcpy(C.data(), d_c, bytes_c, kCudaMemcpyDeviceToHost),
563 if (!ok && fail_step !=
nullptr) {
565 "[CusolverEigh] GPU dgemm FAILED at %s rc=%d "
566 "(m=%d n=%d k=%d) -> CPU fallback\n",
567 fail_step, fail_rc, m, n, k);
575 const Eigen::MatrixXd& L,
576 Eigen::MatrixXd& B)
const
578 if (!
available() || !impl_->cublas_symbols_loaded ||
579 impl_->cublasDtrsm ==
nullptr)
581 if (L.rows() != L.cols() || B.rows() != L.rows())
return false;
582 constexpr Eigen::Index kIntMax =
583 static_cast<Eigen::Index
>(std::numeric_limits<int>::max());
584 if (L.rows() > kIntMax || B.cols() > kIntMax)
return false;
586 constexpr Eigen::Index kMinGpuTrsmOrder = 512;
587 if (L.rows() < kMinGpuTrsmOrder || B.cols() == 0)
return false;
588 const int n =
static_cast<int>(L.rows());
589 const int m =
static_cast<int>(B.cols());
591 const std::size_t bytes_l =
static_cast<std::size_t
>(n) *
592 static_cast<std::size_t
>(n) *
sizeof(double);
593 const std::size_t bytes_b =
static_cast<std::size_t
>(n) *
594 static_cast<std::size_t
>(m) *
sizeof(double);
596 const char* fail_step =
nullptr;
598 auto chk = [&](
const char* step,
int rc,
int ok_code) ->
bool {
599 if (rc == ok_code)
return true;
611 kCublasStatusSuccess)) {
619 if (!chk(
"reserve(L)", d.
reserve(d.
trsm_l, bytes_l) ? 0 : 1, 0))
621 if (!chk(
"cudaMemcpy(L,HtoD)",
623 kCudaMemcpyHostToDevice),
630 if (!chk(
"reserve(B)", d.
reserve(d.
trsm_b, bytes_b) ? 0 : 1, 0))
break;
631 if (!chk(
"cudaMemcpy(B,HtoD)",
633 kCudaMemcpyHostToDevice),
636 const double alpha = 1.0;
640 if (!chk(
"cublasDtrsm",
643 kCublasStatusSuccess))
648 if (!chk(
"cudaMemcpy(B,DtoH)",
650 kCudaMemcpyDeviceToHost),
657 if (fail_step !=
nullptr)
659 "[CusolverEigh] GPU dtrsm FAILED at %s rc=%d "
660 "(n=%d m=%d) -> CPU fallback\n",
661 fail_step, fail_rc, n, m);
void * cusolverDnHandle_t
Runtime-dlopen'd cuSOLVER dense symmetric eigensolver (syevd).
bool gemmNN(const Eigen::MatrixXd &A, const Eigen::MatrixXd &B, Eigen::MatrixXd &C) const
static CusolverEighLib & instance()
bool allEigenpairs(const Eigen::MatrixXd &sym, Eigen::VectorXd &evals, Eigen::MatrixXd &evecs) const
bool available() const noexcept
bool trsmLowerResident(std::uint64_t epoch, const Eigen::MatrixXd &L, Eigen::MatrixXd &B) const
static void setVerbose(bool verbose) noexcept
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
cublasDgemm_t cublasDgemm
cublasDtrsm_t cublasDtrsm
cudaSetDevice_t cudaSetDevice
cudaGetDeviceCount_t cudaGetDeviceCount
cusolverDnCreate_t cusolverDnCreate
cusolverDnDestroy_t cusolverDnDestroy
bool cublas_symbols_loaded
cusolverDnDsyevd_t cusolverDnDsyevd
cublasCreate_t cublasCreate
cublasDestroy_t cublasDestroy
cublasHandle_t cublas_ctx
bool reserve(Scratch &s, std::size_t bytes)
cusolverDnDsyevd_bufferSize_t cusolverDnDsyevd_bufferSize
cudaDeviceSynchronize_t cudaDeviceSynchronize
static Fn sym(void *handle, const char *name)