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