riccaticpp
Loading...
Searching...
No Matches
arena_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
2#define STAN_MATH_REV_CORE_ARENA_MATRIX_HPP
3
4#include <riccati/macros.hpp>
5#include <riccati/memory.hpp>
6#include <riccati/utils.hpp>
7#include <Eigen/Dense>
8#include <type_traits>
9
10namespace riccati {
11
19template <typename MatrixType>
20class arena_matrix : public Eigen::Map<MatrixType> {
21 public:
22 using Scalar = typename std::decay_t<MatrixType>::Scalar;
23 using Base = Eigen::Map<MatrixType>;
24 using PlainObject = std::decay_t<MatrixType>;
25 typedef typename Eigen::internal::remove_all<Base>::type NestedExpression;
26 static constexpr int RowsAtCompileTime = MatrixType::RowsAtCompileTime;
27 static constexpr int ColsAtCompileTime = MatrixType::ColsAtCompileTime;
34 template <typename T>
36 : Base::Map(nullptr,
38 ColsAtCompileTime == Eigen::Dynamic ? 0 : ColsAtCompileTime),
39 allocator_(allocator) {
40 allocator_.owns_alloc_ = false;
41 }
42
49 template <typename T>
50 arena_matrix(arena_allocator<T, arena_alloc>& allocator, Eigen::Index rows,
51 Eigen::Index cols)
52 : Base::Map(allocator.template allocate<Scalar>(rows * cols), rows, cols),
53 allocator_(allocator) {
54 allocator_.owns_alloc_ = false;
55 }
56
63 template <typename T>
64 arena_matrix(arena_allocator<T, arena_alloc>& allocator, Eigen::Index size)
65 : Base::Map(allocator.template allocate<Scalar>(size), size),
66 allocator_(allocator) {}
67
73 template <typename T, typename Expr>
75 const Expr& other) // NOLINT
76 : Base::Map(
77 allocator.template allocate<Scalar>(other.size()),
78 (RowsAtCompileTime == 1 && Expr::ColsAtCompileTime == 1)
79 || (ColsAtCompileTime == 1 && Expr::RowsAtCompileTime == 1)
80 ? other.cols()
81 : other.rows(),
82 (RowsAtCompileTime == 1 && Expr::ColsAtCompileTime == 1)
83 || (ColsAtCompileTime == 1 && Expr::RowsAtCompileTime == 1)
84 ? other.rows()
85 : other.cols()),
86 allocator_(allocator) {
87 allocator_.owns_alloc_ = false;
88 (*this).noalias() = other;
89 }
90
96 arena_matrix(const Base& other) // NOLINT
97 : Base::Map(other) {}
98
104 : Base::Map(const_cast<Scalar*>(other.data()), other.rows(),
105 other.cols()),
106 allocator_(other.allocator_) {
107 allocator_.owns_alloc_ = false;
108 }
109
110 // without this using, compiler prefers combination of implicit construction
111 // and copy assignment to the inherited operator when assigned an expression
112 using Base::operator=;
113
120 // placement new changes what data map points to - there is no allocation
121 new (this)
122 Base(const_cast<Scalar*>(other.data()), other.rows(), other.cols());
123 this->allocator_ = other.allocator_;
124 allocator_.owns_alloc_ = false;
125
126 return *this;
127 }
128
134 template <typename T>
135 arena_matrix& operator=(const T& a) {
136 // do we need to transpose?
137 if ((RowsAtCompileTime == 1 && T::ColsAtCompileTime == 1)
138 || (ColsAtCompileTime == 1 && T::RowsAtCompileTime == 1)) {
139 // placement new changes what data map points to - there is no allocation
140 new (this) Base(allocator_.template allocate<Scalar>(a.size()), a.cols(),
141 a.rows());
142
143 } else {
144 new (this) Base(allocator_.template allocate<Scalar>(a.size()), a.rows(),
145 a.cols());
146 }
147 Base::operator=(a);
148 allocator_.owns_alloc_ = false;
149
150 return *this;
151 }
152};
153
154template <typename T, typename Expr>
156 const Expr& expr) noexcept {
158}
159
160template <typename T, typename Expr>
162 const Expr& expr) noexcept {
164}
165
166template <typename Expr>
167inline auto to_arena(dummy_allocator& arena, const Expr& expr) noexcept {
168 return eval(expr);
169}
170
171template <typename T>
172inline void print(const char* name, const arena_matrix<T>& x) {
173#ifdef RICCATI_DEBUG
174 std::cout << name << "(" << x.rows() << ", " << x.cols() << ")" << std::endl;
175 std::cout << x << std::endl;
176#endif
177}
178
179} // namespace riccati
180
181namespace Eigen {
182namespace internal {
183
184template <typename T>
185struct traits<riccati::arena_matrix<T>> : traits<Eigen::Map<T>> {
186 using base = traits<Eigen::Map<T>>;
187 using XprKind = typename Eigen::internal::traits<std::decay_t<T>>::XprKind;
188 using Scalar = typename std::decay_t<T>::Scalar;
189 enum {
190 PlainObjectTypeInnerSize = base::PlainObjectTypeInnerSize,
191 InnerStrideAtCompileTime = base::InnerStrideAtCompileTime,
192 OuterStrideAtCompileTime = base::OuterStrideAtCompileTime,
193 Alignment = base::Alignment,
194 Flags = base::Flags
195 };
196};
197
198} // namespace internal
199} // namespace Eigen
200
201#endif
static constexpr int ColsAtCompileTime
static constexpr int RowsAtCompileTime
typename std::decay_t< MatrixType >::Scalar Scalar
arena_matrix & operator=(const arena_matrix< MatrixType > &other)
arena_matrix(const Base &other)
Eigen::Map< MatrixType > Base
arena_matrix & operator=(const T &a)
std::decay_t< MatrixType > PlainObject
arena_matrix(arena_allocator< T, arena_alloc > &allocator, Eigen::Index rows, Eigen::Index cols)
arena_matrix(arena_allocator< T, arena_alloc > &allocator, const Expr &other)
arena_matrix(const arena_matrix< MatrixType > &other)
arena_matrix(arena_allocator< T, arena_alloc > &allocator)
arena_matrix(arena_allocator< T, arena_alloc > &allocator, Eigen::Index size)
Eigen::internal::remove_all< Base >::type NestedExpression
#define RICCATI_ALWAYS_INLINE
Definition macros.hpp:57
auto eval(arena_allocator< T, arena_alloc > &arena, const Expr &expr) noexcept
auto to_arena(arena_allocator< T, arena_alloc > &arena, const Expr &expr) noexcept
void print(const char *name, const arena_matrix< T > &x)
typename Eigen::internal::traits< std::decay_t< T > >::XprKind XprKind