Loading...
Searching...
No Matches
KokkosFactor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <Eigen/Cholesky>
6#include <Kokkos_Core.hpp>
7#include <limits>
8#include <memory>
9#include <stdexcept>
10#include <string>
11#include <type_traits>
12
13#include "KokkosAuditedCopy.h"
14#include "KokkosBackend.h"
15
16#if defined(KOKKOS_ENABLE_CUDA)
17#include <cuda_runtime_api.h>
18#include <cusolverDn.h>
19#endif
20#if defined(KOKKOS_ENABLE_HIP)
21#include <hip/hip_runtime_api.h>
22#include <rocsolver/rocsolver.h>
23#endif
24
25namespace gpr::linalg::detail {
26
27#if defined(KOKKOS_ENABLE_CUDA)
28inline void kokkosCudaCheck(cudaError_t status, const char* operation)
29{
30 if (status != cudaSuccess)
31 throw std::runtime_error(std::string(operation) + ": " +
32 cudaGetErrorString(status));
33}
34inline void kokkosCusolverCheck(cusolverStatus_t status, const char* operation)
35{
37 throw std::runtime_error(std::string(operation) +
38 " failed with status " +
39 std::to_string(static_cast<int>(status)));
40}
41class KokkosCudaDeviceScope {
42public:
43 explicit KokkosCudaDeviceScope(int selected)
44 {
45 kokkosCudaCheck(cudaGetDevice(&previous_), "cudaGetDevice");
46 changed_ = selected != previous_;
47 if (changed_) kokkosCudaCheck(cudaSetDevice(selected), "cudaSetDevice");
48 }
49 ~KokkosCudaDeviceScope()
50 {
51 if (changed_) (void)cudaSetDevice(previous_);
52 }
53 KokkosCudaDeviceScope(const KokkosCudaDeviceScope&) = delete;
54 KokkosCudaDeviceScope& operator=(const KokkosCudaDeviceScope&) = delete;
55
56private:
57 int previous_ = 0;
58 bool changed_ = false;
59};
60#endif
61
62#if defined(KOKKOS_ENABLE_HIP)
63inline void kokkosHipCheck(hipError_t status, const char* operation)
64{
65 if (status != hipSuccess)
66 throw std::runtime_error(std::string(operation) + ": " +
67 hipGetErrorString(status));
68}
69inline void kokkosRocblasCheck(rocblas_status status, const char* operation)
70{
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)));
75}
76class KokkosHipDeviceScope {
77public:
78 explicit KokkosHipDeviceScope(int selected)
79 {
80 kokkosHipCheck(hipGetDevice(&previous_), "hipGetDevice");
81 changed_ = selected != previous_;
82 if (changed_) kokkosHipCheck(hipSetDevice(selected), "hipSetDevice");
83 }
84 ~KokkosHipDeviceScope()
85 {
86 if (changed_) (void)hipSetDevice(previous_);
87 }
88 KokkosHipDeviceScope(const KokkosHipDeviceScope&) = delete;
89 KokkosHipDeviceScope& operator=(const KokkosHipDeviceScope&) = delete;
90
91private:
92 int previous_ = 0;
93 bool changed_ = false;
94};
95#endif
96
101template <class ExecutionSpace, class MatrixView>
102bool kokkosFactorLower(const ExecutionSpace& space, MatrixView& matrix)
103{
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());
109 cusolverDnHandle_t raw = nullptr;
110 kokkosCusolverCheck(cusolverDnCreate(&raw), "cusolverDnCreate");
111 const std::unique_ptr<std::remove_pointer_t<cusolverDnHandle_t>,
112 decltype(&cusolverDnDestroy)>
113 handle(raw, &cusolverDnDestroy);
114 kokkosCusolverCheck(
115 cusolverDnSetStream(handle.get(), space.cuda_stream()),
116 "cusolverDnSetStream");
117 int size = 0;
118 kokkosCusolverCheck(
119 cusolverDnDpotrf_bufferSize(handle.get(), CUBLAS_FILL_MODE_LOWER, n,
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");
126 kokkosCusolverCheck(
127 cusolverDnDpotrf(handle.get(), CUBLAS_FILL_MODE_LOWER, n,
128 matrix.data(), lda, work.data(), size,
129 info.data()),
130 "cusolverDnDpotrf");
131 space.fence("gpr CUDA Cholesky");
132 int status_value = 0;
133 const Kokkos::View<int, Kokkos::HostSpace,
134 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
135 status(&status_value);
136 ::gpr::linalg::detail::audited_deep_copy(
137 status, info, sizeof(int), ::gpr::linalg::detail::CopyRole::ToHost);
138 if (status() < 0)
139 throw std::runtime_error("cuSOLVER rejected a Cholesky argument");
140 return status() == 0;
141 } else
142#endif
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(
155 "gpr potrf info");
156 kokkosRocblasCheck(rocsolver_dpotrf(handle.get(), rocblas_fill_lower, n,
157 matrix.data(), lda, info.data()),
158 "rocsolver_dpotrf");
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>>
163 status(&status_value);
164 ::gpr::linalg::detail::audited_deep_copy(
165 status, info, sizeof(rocblas_int),
166 ::gpr::linalg::detail::CopyRole::ToHost);
167 if (status() < 0)
168 throw std::runtime_error("rocSOLVER rejected a Cholesky argument");
169 return status() == 0;
170 } else
171#endif
172 {
173 static_assert(
174 Kokkos::SpaceAccessibility<
175 Kokkos::HostSpace,
176 typename MatrixView::memory_space>::accessible,
177 "Kokkos GP Cholesky supports CPU, CUDA and HIP execution");
178 (void)space;
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();
186 return true;
187 }
188}
189
190} // namespace gpr::linalg::detail
static constexpr int CUSOLVER_STATUS_SUCCESS
void * cusolverDnHandle_t
int cusolverStatus_t
static constexpr int CUBLAS_FILL_MODE_LOWER
int status