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>;
62
63template <int EigenLayout, FloatingPointType float_type> struct EigenKokkosBase
64{
65 using EigenMatrix = MatrixType<EigenLayout, float_type>;
66 using HostView = KokkosScalarMatrix<HostSpace, EigenLayout, float_type>;
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> get_span() const
86 {
87 return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
88 }
89
90 std::span<double> get_span()
91 {
92 return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
93 }
94
96 {
97 Kokkos::deep_copy(compute, host);
98 }
99
101 {
102 Kokkos::deep_copy(host, compute);
103 }
104};
105
106// ColMajor version (default)
107template <FloatingPointType floatype>
109
110// RowMajor version
111template <FloatingPointType floatype>
112using RowMajorEigenKokkos = EigenKokkosBase<EigenLayoutRight, floatype>;
113
114#endif
Definition traits.hpp:20
Definition eigen_kokkos.hpp:64
void update_host_to_compute() const
Definition eigen_kokkos.hpp:95
KokkosScalarMatrix< HostSpace, EigenLayout, float_type > HostView
Definition eigen_kokkos.hpp:66
std::span< const double > get_span() const
Definition eigen_kokkos.hpp:85
ComputeView compute
Definition eigen_kokkos.hpp:74
std::span< double > get_span()
Definition eigen_kokkos.hpp:90
EigenKokkosBase(std::size_t n_row, std::size_t n_col)
Definition eigen_kokkos.hpp:77
HostView host
Definition eigen_kokkos.hpp:73
KokkosScalarMatrix< ComputeSpace, EigenLayout, float_type, Kokkos::MemoryTraits< Kokkos::RandomAccess > > ComputeView
Definition eigen_kokkos.hpp:67
EigenMatrix eigen_data
Definition eigen_kokkos.hpp:75
void update_compute_to_host() const
Definition eigen_kokkos.hpp:100
MatrixType< EigenLayout, float_type > EigenMatrix
Definition eigen_kokkos.hpp:65
Kokkos::LayoutLeft type
Definition eigen_kokkos.hpp:38
Kokkos::LayoutRight type
Definition eigen_kokkos.hpp:43
Definition eigen_kokkos.hpp:34