5#include <Eigen/Cholesky>
6#include <Kokkos_Core.hpp>
16#if defined(KOKKOS_ENABLE_CUDA)
17#include <cuda_runtime_api.h>
18#include <cusolverDn.h>
20#if defined(KOKKOS_ENABLE_HIP)
21#include <hip/hip_runtime_api.h>
22#include <rocsolver/rocsolver.h>
25namespace gpr::linalg::detail {
27#if defined(KOKKOS_ENABLE_CUDA)
28inline void kokkosCudaCheck(cudaError_t
status,
const char* operation)
31 throw std::runtime_error(std::string(operation) +
": " +
32 cudaGetErrorString(
status));
37 throw std::runtime_error(std::string(operation) +
38 " failed with status " +
39 std::to_string(
static_cast<int>(
status)));
41class KokkosCudaDeviceScope {
43 explicit KokkosCudaDeviceScope(
int selected)
45 kokkosCudaCheck(cudaGetDevice(&previous_),
"cudaGetDevice");
46 changed_ = selected != previous_;
47 if (changed_) kokkosCudaCheck(cudaSetDevice(selected),
"cudaSetDevice");
49 ~KokkosCudaDeviceScope()
51 if (changed_) (void)cudaSetDevice(previous_);
53 KokkosCudaDeviceScope(
const KokkosCudaDeviceScope&) =
delete;
54 KokkosCudaDeviceScope& operator=(
const KokkosCudaDeviceScope&) =
delete;
58 bool changed_ =
false;
62#if defined(KOKKOS_ENABLE_HIP)
63inline void kokkosHipCheck(hipError_t
status,
const char* operation)
66 throw std::runtime_error(std::string(operation) +
": " +
67 hipGetErrorString(
status));
69inline void kokkosRocblasCheck(rocblas_status
status,
const char* operation)
71 if (
status != rocblas_status_success)
72 throw std::runtime_error(std::string(operation) +
73 " failed with status " +
74 std::to_string(
static_cast<int>(
status)));
76class KokkosHipDeviceScope {
78 explicit KokkosHipDeviceScope(
int selected)
80 kokkosHipCheck(hipGetDevice(&previous_),
"hipGetDevice");
81 changed_ = selected != previous_;
82 if (changed_) kokkosHipCheck(hipSetDevice(selected),
"hipSetDevice");
84 ~KokkosHipDeviceScope()
86 if (changed_) (void)hipSetDevice(previous_);
88 KokkosHipDeviceScope(
const KokkosHipDeviceScope&) =
delete;
89 KokkosHipDeviceScope& operator=(
const KokkosHipDeviceScope&) =
delete;
93 bool changed_ =
false;
101template <
class ExecutionSpace,
class MatrixView>
102bool kokkosFactorLower(
const ExecutionSpace& space, MatrixView& matrix)
104 const int n = kokkosSolverExtent(matrix.extent(0));
105 const int lda = kokkosSolverExtent(matrix.stride(1));
106#if defined(KOKKOS_ENABLE_CUDA)
107 if constexpr (std::is_same_v<ExecutionSpace, Kokkos::Cuda>) {
108 KokkosCudaDeviceScope device(space.cuda_device());
110 kokkosCusolverCheck(cusolverDnCreate(&raw),
"cusolverDnCreate");
111 const std::unique_ptr<std::remove_pointer_t<cusolverDnHandle_t>,
112 decltype(&cusolverDnDestroy)>
113 handle(raw, &cusolverDnDestroy);
115 cusolverDnSetStream(handle.get(), space.cuda_stream()),
116 "cusolverDnSetStream");
120 matrix.data(), lda, &size),
121 "cusolverDnDpotrf_bufferSize");
122 using MemorySpace =
typename MatrixView::memory_space;
123 Kokkos::View<double*, MemorySpace> work(
"gpr potrf work",
124 size > 0 ? size : 1);
125 Kokkos::View<int, MemorySpace> info(
"gpr potrf info");
128 matrix.data(), lda, work.data(), size,
131 space.fence(
"gpr CUDA Cholesky");
132 int status_value = 0;
133 const Kokkos::View<int, Kokkos::HostSpace,
134 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
136 ::gpr::linalg::detail::audited_deep_copy(
137 status, info,
sizeof(
int), ::gpr::linalg::detail::CopyRole::ToHost);
139 throw std::runtime_error(
"cuSOLVER rejected a Cholesky argument");
143#if defined(KOKKOS_ENABLE_HIP)
144 if constexpr (std::is_same_v<ExecutionSpace, Kokkos::HIP>) {
145 KokkosHipDeviceScope device(space.hip_device());
146 rocblas_handle raw =
nullptr;
147 kokkosRocblasCheck(rocblas_create_handle(&raw),
148 "rocblas_create_handle");
149 const std::unique_ptr<std::remove_pointer_t<rocblas_handle>,
150 decltype(&rocblas_destroy_handle)>
151 handle(raw, &rocblas_destroy_handle);
152 kokkosRocblasCheck(rocblas_set_stream(handle.get(), space.hip_stream()),
153 "rocblas_set_stream");
154 Kokkos::View<rocblas_int, typename MatrixView::memory_space> info(
156 kokkosRocblasCheck(rocsolver_dpotrf(handle.get(), rocblas_fill_lower, n,
157 matrix.data(), lda, info.data()),
159 space.fence(
"gpr HIP Cholesky");
160 rocblas_int status_value = 0;
161 const Kokkos::View<rocblas_int, Kokkos::HostSpace,
162 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
164 ::gpr::linalg::detail::audited_deep_copy(
165 status, info,
sizeof(rocblas_int),
166 ::gpr::linalg::detail::CopyRole::ToHost);
168 throw std::runtime_error(
"rocSOLVER rejected a Cholesky argument");
174 Kokkos::SpaceAccessibility<
176 typename MatrixView::memory_space>::accessible,
177 "Kokkos GP Cholesky supports CPU, CUDA and HIP execution");
179 using Stride = Eigen::OuterStride<>;
180 Eigen::Map<EigenMatrix, Eigen::Unaligned, Stride> mapped(
181 matrix.data(), n, n, Stride(lda));
182 Eigen::LLT<EigenMatrix> factor(
183 mapped.template selfadjointView<Eigen::Lower>());
184 if (factor.info() != Eigen::Success)
return false;
185 mapped.template triangularView<Eigen::Lower>() = factor.matrixL();
static constexpr int CUSOLVER_STATUS_SUCCESS
void * cusolverDnHandle_t
static constexpr int CUBLAS_FILL_MODE_LOWER
kd::mem_space MemorySpace