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
10// constexpr auto EigenLayoutRight = Eigen::RowMajor;
11
12// constexpr auto DataLayoutEigen = Eigen::ColMajor;
13// constexpr auto CompileMatrixSizeEigen = -1;
14// using MatrixType =
15// Eigen::Matrix<double, CompileMatrixSizeEigen, CompileMatrixSizeEigen, DataLayoutEigen>;
16
17// using SparseMatrixType = Eigen::SparseMatrix<double, DataLayoutEigen>;
18// using DiagonalType = Eigen::DiagonalMatrix<double, CompileMatrixSizeEigen>;
19
20// template <typename ExecSpace,typename ...Memorytrait>
21// using KokkosScalarMatrix = Kokkos::View<double**, Kokkos::LayoutLeft, ExecSpace,Memorytrait...>;
22
23// struct RowMajorEigenKokkos
24// {
25
26// };
27
28// struct EigenKokkos
29// {
30// KokkosScalarMatrix<HostSpace> host;
31// KokkosScalarMatrix<ComputeSpace,Kokkos::MemoryTraits<Kokkos::RandomAccess>> compute;
32// MatrixType eigen_data;
33
34// EigenKokkos(std::size_t n_row, std::size_t n_col);
35// [[nodiscard]] std::span<const double> get_span() const
36// {
37// return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
38// }
39// std::span<double> get_span()
40// {
41// return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
42// }
43
44// void update_host_to_compute() const;
45// void update_compute_to_host() const;
46// };
47
48#include <span>
49
50// Layout definitions
51constexpr auto EigenLayoutRight = Eigen::RowMajor;
52constexpr auto EigenLayoutLeft = Eigen::ColMajor;
53constexpr auto CompileMatrixSizeEigen = -1;
54
55// Templated Eigen Matrix Type
56template <int Layout>
57using MatrixType = Eigen::Matrix<double, CompileMatrixSizeEigen, CompileMatrixSizeEigen, Layout>;
58
59using ColMajorMatrixtype = MatrixType<EigenLayoutLeft>;
60
61template <int Layout> using SparseMatrixType = Eigen::SparseMatrix<double, Layout>;
62
63using DiagonalType = Eigen::DiagonalMatrix<double, CompileMatrixSizeEigen>;
64
65// template <typename ExecSpace, typename... MemoryTraits>
66// using KokkosScalarMatrix = Kokkos::View<double**, Kokkos::LayoutLeft, ExecSpace,
67// MemoryTraits...>;
68
69template <int EigenLayout> struct KokkosLayoutMapper;
70
71template <> struct KokkosLayoutMapper<Eigen::ColMajor>
72{
73 using type = Kokkos::LayoutLeft;
74};
75
76template <> struct KokkosLayoutMapper<Eigen::RowMajor>
77{
78 using type = Kokkos::LayoutRight;
79};
80
81template <typename ExecSpace, int EigenLayout, typename... MemoryTrait>
82using KokkosScalarMatrix = Kokkos::
83 View<double**, typename KokkosLayoutMapper<EigenLayout>::type, ExecSpace, MemoryTrait...>;
84
85
86using RowMajorKokkosScalarMatrix = KokkosScalarMatrix<ComputeSpace, Eigen::RowMajor>;
87using ColMajorKokkosScalarMatrix = KokkosScalarMatrix<ComputeSpace, Eigen::ColMajor>;
88
89template <int EigenLayout> struct EigenKokkosBase
90{
91 using EigenMatrix = MatrixType<EigenLayout>;
92 using HostView = KokkosScalarMatrix<HostSpace, EigenLayout>;
93 using ComputeView = KokkosScalarMatrix<ComputeSpace,
94 EigenLayout,
95 Kokkos::MemoryTraits<Kokkos::RandomAccess>>;
96
100
101 EigenKokkosBase(std::size_t n_row, std::size_t n_col)
102 : eigen_data(MatrixType<EigenLayout>(n_row, n_col))
103 {
104 eigen_data.setZero();
105 host = HostView(eigen_data.data(), n_row, n_col);
106 compute = Kokkos::create_mirror_view_and_copy(ComputeSpace(), host);
107 }
108
109 [[nodiscard]] std::span<const double> get_span() const
110 {
111 return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
112 }
113
114 std::span<double> get_span()
115 {
116 return {eigen_data.data(), static_cast<size_t>(eigen_data.size())};
117 }
118
120 {
121 Kokkos::deep_copy(compute, host);
122 }
123
125 {
126 Kokkos::deep_copy(host, compute);
127 }
128};
129
130// ColMajor version (default)
132
133// RowMajor version
135
136#endif
Definition eigen_kokkos.hpp:90
ComputeView compute
Definition eigen_kokkos.hpp:98
EigenMatrix eigen_data
Definition eigen_kokkos.hpp:99
EigenKokkosBase(std::size_t n_row, std::size_t n_col)
Definition eigen_kokkos.hpp:101
void update_host_to_compute() const
Definition eigen_kokkos.hpp:119
KokkosScalarMatrix< ComputeSpace, EigenLayout, Kokkos::MemoryTraits< Kokkos::RandomAccess > > ComputeView
Definition eigen_kokkos.hpp:93
KokkosScalarMatrix< HostSpace, EigenLayout > HostView
Definition eigen_kokkos.hpp:92
std::span< double > get_span()
Definition eigen_kokkos.hpp:114
void update_compute_to_host() const
Definition eigen_kokkos.hpp:124
std::span< const double > get_span() const
Definition eigen_kokkos.hpp:109
MatrixType< EigenLayout > EigenMatrix
Definition eigen_kokkos.hpp:91
HostView host
Definition eigen_kokkos.hpp:97
Kokkos::LayoutLeft type
Definition eigen_kokkos.hpp:73
Kokkos::LayoutRight type
Definition eigen_kokkos.hpp:78
Definition eigen_kokkos.hpp:69