BioCMAMC-ST
eigen_kokkos.hpp
1#ifndef __SIMULATION_EIGEN_KOKKOS_HPP__
2#define __SIMULATION_EIGEN_KOKKOS_HPP__
3
4#include <Eigen/Core>
5#include <Eigen/Dense>
6#include <Eigen/Sparse>
7#include <Kokkos_Core.hpp>
8#include <common/common.hpp>
9#include <common/traits.hpp>
10#include <span>
11
12// Layout definitions
13constexpr auto EigenLayoutRight = Eigen::RowMajor;
14constexpr auto EigenLayoutLeft = Eigen::ColMajor;
15constexpr auto CompileMatrixSizeEigen = -1;
16
17// Templated Eigen Matrix Type
18template <int Layout, FloatingPointType ftype>
19using MatrixType = Eigen::
20 Matrix<ftype, CompileMatrixSizeEigen, CompileMatrixSizeEigen, Layout>;
21
22template <FloatingPointType ftype>
23using ColMajorMatrixtype = MatrixType<EigenLayoutLeft, ftype>;
24
25template <int Layout, FloatingPointType ftype>
26using SparseMatrixType = Eigen::SparseMatrix<ftype, Layout>;
27
28using DiagonalType = Eigen::DiagonalMatrix<double, CompileMatrixSizeEigen>;
29
30// template <typename ExecSpace, typename... MemoryTraits>
31// using KokkosScalarMatrix = Kokkos::View<double**, Kokkos::LayoutLeft,
32// ExecSpace, MemoryTraits...>;
33
34template <int EigenLayout> struct KokkosLayoutMapper;
35
36template <> struct KokkosLayoutMapper<Eigen::ColMajor>
37{
38 using type = Kokkos::LayoutLeft;
39};
40
41template <> struct KokkosLayoutMapper<Eigen::RowMajor>
42{
43 using type = Kokkos::LayoutRight;
44};
45
46template <typename ExecSpace,
47 int EigenLayout,
49 typename... MemoryTrait>
50using KokkosScalarMatrix
51 = Kokkos::View<ftype**,
53 ExecSpace,
54 MemoryTrait...>;
55
56template <FloatingPointType ftype>
57using RowMajorKokkosScalarMatrix
58 = KokkosScalarMatrix<ComputeSpace, Eigen::RowMajor, ftype>;
59template <FloatingPointType ftype>
60using ColMajorKokkosScalarMatrix
61 = KokkosScalarMatrix<ComputeSpace, Eigen::ColMajor, ftype>;
63template <int EigenLayout, FloatingPointType float_type> struct EigenKokkosBase
65 using EigenMatrix = MatrixType<EigenLayout, float_type>;
66 using HostView = KokkosScalarMatrix<HostSpace, EigenLayout, float_type>;
67 using ComputeView
68 = KokkosScalarMatrix<ComputeSpace,
69 EigenLayout,
70 float_type,
71 Kokkos::MemoryTraits<Kokkos::RandomAccess> >;
72
76
77 EigenKokkosBase(std::size_t n_row, std::size_t n_col)
78 : eigen_data(MatrixType<EigenLayout, float_type>(n_row, n_col))
79 {
80 eigen_data.setZero();
81 host = HostView(eigen_data.data(), n_row, n_col);
82 compute = Kokkos::create_mirror_view_and_copy(ComputeSpace(), host);
83 }
84
85 [[nodiscard]] std::span<const double>
86 get_span() const
87 {
88 return { eigen_data.data(), static_cast<size_t>(eigen_data.size()) };
89 }
90
91 std::span<double>
92 get_span()
93 {
94 return { eigen_data.data(), static_cast<size_t>(eigen_data.size()) };
95 }
96
97 void
99 {
100 Kokkos::deep_copy(compute, host);
101 }
102
103 void
105 {
106 Kokkos::deep_copy(host, compute);
107 }
108};
109
110// ColMajor version (default)
111template <FloatingPointType floatype>
113
114// RowMajor version
115template <FloatingPointType floatype>
116using RowMajorEigenKokkos = EigenKokkosBase<EigenLayoutRight, floatype>;
117
118#endif
Definition traits.hpp:20
Definition eigen_kokkos.hpp:61
void update_host_to_compute() const
Definition eigen_kokkos.hpp:94
KokkosScalarMatrix< HostSpace, EigenLayout, float_type > HostView
Definition eigen_kokkos.hpp:63
std::span< const double > get_span() const
Definition eigen_kokkos.hpp:82
ComputeView compute
Definition eigen_kokkos.hpp:70
EigenKokkosBase(std::size_t n_row, std::size_t n_col)
Definition eigen_kokkos.hpp:73
HostView host
Definition eigen_kokkos.hpp:69
KokkosScalarMatrix< ComputeSpace, EigenLayout, float_type, Kokkos::MemoryTraits< Kokkos::RandomAccess > > ComputeView
Definition eigen_kokkos.hpp:64
EigenMatrix eigen_data
Definition eigen_kokkos.hpp:71
void update_compute_to_host() const
Definition eigen_kokkos.hpp:100
MatrixType< EigenLayout, float_type > EigenMatrix
Definition eigen_kokkos.hpp:62
Kokkos::LayoutLeft type
Definition eigen_kokkos.hpp:38
Kokkos::LayoutRight type
Definition eigen_kokkos.hpp:43
Definition eigen_kokkos.hpp:34