5#include <KokkosBlas3_gemm.hpp>
6#include <KokkosBlas3_trsm.hpp>
7#include <Kokkos_Core.hpp>
21namespace gpr::linalg::detail {
23using exec_space = Kokkos::DefaultExecutionSpace;
24using mem_space =
typename exec_space::memory_space;
26using device_matrix_t = Kokkos::View<double**, Kokkos::LayoutLeft, mem_space>;
27using device_vector_t = Kokkos::View<double*, mem_space>;
29struct KokkosDeviceFactor {
34 Kokkos::View<
double**, Kokkos::LayoutLeft, Kokkos::HostSpace,
35 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
36using host_const_matrix_t =
37 Kokkos::View<
const double**, Kokkos::LayoutLeft, Kokkos::HostSpace,
38 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
39using host_vector_t = Kokkos::View<
double*, Kokkos::HostSpace,
40 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
41using host_const_vector_t =
42 Kokkos::View<
const double*, Kokkos::HostSpace,
43 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
45inline void ensure_kokkos()
47 if (Kokkos::is_finalized())
48 throw std::logic_error(
"cannot use a finalized Kokkos runtime");
49 static std::once_flag initialized;
50 std::call_once(initialized, [] {
51 if (!Kokkos::is_initialized()) {
54 if (Kokkos::is_initialized() && !Kokkos::is_finalized())
62inline host_const_matrix_t wrap(
const EigenMatrix& M)
64 return host_const_matrix_t(M.data(), M.rows(), M.cols());
68 return host_matrix_t(M.data(), M.rows(), M.cols());
70inline host_const_vector_t wrap(
const Eigen::VectorXd& v)
72 return host_const_vector_t(v.data(), v.size());
74inline host_vector_t wrap(Eigen::VectorXd& v)
76 return host_vector_t(v.data(), v.size());
80inline device_matrix_t to_device(host_const_matrix_t h,
81 const std::string& label)
83 device_matrix_t d(std::string(label), h.extent(0), h.extent(1));
84 audited_deep_copy(d, h, h.size() *
sizeof(
double), CopyRole::ToExecution);
87inline device_vector_t to_device(host_const_vector_t h,
88 const std::string& label)
90 device_vector_t d(std::string(label), h.extent(0));
91 audited_deep_copy(d, h, h.size() *
sizeof(
double), CopyRole::ToExecution);
96inline void to_eigen(
const device_matrix_t& d,
EigenMatrix& M)
98 M.resize(d.extent(0), d.extent(1));
100 audited_deep_copy(h, d, d.size() *
sizeof(
double), CopyRole::ToHost);
102inline void to_eigen(
const device_vector_t& d, Eigen::VectorXd& v)
104 v.resize(d.extent(0));
106 audited_deep_copy(h, d, d.size() *
sizeof(
double), CopyRole::ToHost);
109template <
class ExecutionSpace,
class MatrixView>
110void solve_lower(
const ExecutionSpace& space,
const MatrixView& factor,
111 MatrixView& rhs,
bool transpose)
113#if defined(KOKKOS_ENABLE_HIP)
114 if constexpr (std::is_same_v<ExecutionSpace, Kokkos::HIP>) {
115 KokkosHipDeviceScope device(space.hip_device());
116 rocblas_handle raw =
nullptr;
117 kokkosRocblasCheck(rocblas_create_handle(&raw),
118 "rocblas_create_handle");
119 const std::unique_ptr<std::remove_pointer_t<rocblas_handle>,
120 decltype(&rocblas_destroy_handle)>
121 handle(raw, &rocblas_destroy_handle);
122 kokkosRocblasCheck(rocblas_set_stream(handle.get(), space.hip_stream()),
123 "rocblas_set_stream");
124 const double alpha = 1.0;
126 rocblas_dtrsm(handle.get(), rocblas_side_left, rocblas_fill_lower,
127 transpose ? rocblas_operation_transpose
128 : rocblas_operation_none,
129 rocblas_diagonal_non_unit,
130 kokkosSolverExtent(rhs.extent(0)),
131 kokkosSolverExtent(rhs.extent(1)), &alpha,
132 factor.data(), kokkosSolverExtent(factor.stride(1)),
133 rhs.data(), kokkosSolverExtent(rhs.stride(1))),
135 space.fence(
"gpr HIP triangular solve");
139 const KokkosCopyScope kernels(KokkosCopyContext::ThirdParty);
140 KokkosBlas::trsm(space,
"L",
"L", transpose ?
"T" :
"N",
"N", 1.0,
142 space.fence(
"gpr triangular solve");
146inline void fill_identity(
const device_matrix_t& identity)
148 const std::size_t n = identity.extent(0);
149 Kokkos::parallel_for(
151 Kokkos::RangePolicy<exec_space, Kokkos::IndexType<std::size_t>>(0,
153 KOKKOS_LAMBDA(std::size_t index) {
154 const std::size_t i = index % n;
155 const std::size_t j = index / n;
156 identity(i, j) = (i == j) ? 1.0 : 0.0;
161inline void cholesky_inverse_on_device(
const device_matrix_t& factor,
162 device_matrix_t& work,
163 const device_matrix_t& inverse)
166 solve_lower(exec_space{}, factor, work,
false);
167 const KokkosCopyScope kernels(KokkosCopyContext::ThirdParty);
168 KokkosBlas::gemm(
"T",
"N", 1.0, work, work, 0.0, inverse);
172inline void column_squared_norms(
const device_matrix_t& matrix,
173 const device_vector_t& norms)
175 const int rows = kokkosSolverExtent(matrix.extent(0));
176 const int cols = kokkosSolverExtent(matrix.extent(1));
177 Kokkos::parallel_for(
178 "gpr column squared norms", Kokkos::RangePolicy<exec_space>(0, cols),
179 KOKKOS_LAMBDA(
const int j) {
182 for (
int i = 0; i < rows; ++i) {
183 const double value = matrix(i, j);
184 const double sq = value * value;
185 const double next = sum + sq;
186 corr += Kokkos::abs(sum) >= Kokkos::abs(sq) ? (sum - next) + sq
190 norms(j) = sum + corr;
192 exec_space{}.fence(
"gpr column squared norms");
196inline void reserve_view(device_matrix_t& view, std::size_t rows,
197 std::size_t cols,
const char* label)
199 if (view.data() !=
nullptr && view.extent(0) == rows &&
200 view.extent(1) == cols)
202 view = device_matrix_t();
203 view = device_matrix_t(
204 Kokkos::view_alloc(Kokkos::WithoutInitializing, std::string(label)),
207inline void reserve_view(device_vector_t& view, std::size_t size,
210 if (view.data() !=
nullptr && view.extent(0) == size)
return;
211 view = device_vector_t();
212 view = device_vector_t(
213 Kokkos::view_alloc(Kokkos::WithoutInitializing, std::string(label)),
217inline std::size_t byte_count(Eigen::Index entries)
219 return static_cast<std::size_t
>(entries) *
sizeof(
double);
224 return a.rows() == b.rows() && a.cols() == b.cols() &&
226 std::memcmp(a.data(), b.data(), byte_count(a.size())) == 0);
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix