aligator  0.13.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
 
Loading...
Searching...
No Matches
eigen-map.hpp
Go to the documentation of this file.
1
2#pragma once
3
5#include <new>
6
7namespace aligator {
8
9template <typename MatrixType, int Alignment = Eigen::AlignedMax>
11 Eigen::Index rows, Eigen::Index cols) {
12 using MapType = Eigen::Map<MatrixType, Alignment>;
13 using Scalar = typename MatrixType::Scalar;
14 size_t size = size_t(rows * cols);
15 Scalar *data = alloc.allocate<Scalar>(size, Alignment);
16 return MapType{data, rows, cols};
17}
18
19template <typename MatrixType, int Alignment = Eigen::AlignedMax>
21 Eigen::Index size) {
22 using MapType = Eigen::Map<MatrixType, Alignment>;
23 using Scalar = typename MatrixType::Scalar;
24 Scalar *data = alloc.allocate<Scalar>(size_t(size), Alignment);
25 return MapType{data, size};
26}
27
30template <typename MatrixType, int Alignment>
31void emplace_map_steal(Eigen::Map<MatrixType, Alignment> &map,
32 Eigen::Map<MatrixType, Alignment> &other) {
33 EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(MatrixType);
34 using MapType = Eigen::Map<MatrixType, Alignment>;
35 typename MatrixType::Scalar *data = other.data();
36 if (data) {
37 if constexpr (MatrixType::IsVectorAtCompileTime) {
38 ::new (&map) MapType{data, other.size()};
39 } else {
40 ::new (&map) MapType{data, other.rows(), other.cols()};
41 }
42 }
43 other.~MapType();
44}
45
48template <typename MatrixType, int Alignment>
49void emplace_map_from_data(Eigen::Map<MatrixType, Alignment> &map,
50 Eigen::Index rows, Eigen::Index cols,
51 typename MatrixType::Scalar *data) {
52 EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(MatrixType);
53 using MapType = Eigen::Map<MatrixType, Alignment>;
54 ::new (&map) MapType{data, rows, cols};
55}
56
58template <typename MatrixType, int Alignment>
59void emplace_map_from_data(Eigen::Map<MatrixType, Alignment> &map,
60 Eigen::Index size,
61 typename MatrixType::Scalar *data) {
62 EIGEN_STATIC_ASSERT_VECTOR_ONLY(MatrixType);
63 EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(MatrixType);
64 using MapType = Eigen::Map<MatrixType, Alignment>;
65 ::new (&map) MapType{data, size};
66}
67
70template <typename MatrixType, int Alignment>
71void emplace_allocated_map(Eigen::Map<MatrixType, Alignment> &map,
72 Eigen::Index rows, Eigen::Index cols,
74 using Scalar = typename MatrixType::Scalar;
75 Scalar *data = alloc.template allocate<Scalar>(size_t(rows * cols));
76 emplace_map_from_data(map, rows, cols, data);
77}
78
80template <typename MatrixType, int Alignment>
81void emplace_allocated_map(Eigen::Map<MatrixType, Alignment> &map,
82 Eigen::Index size, polymorphic_allocator alloc) {
83 using Scalar = typename MatrixType::Scalar;
84 Scalar *data = alloc.template allocate<Scalar>(size_t(size), Alignment);
85 emplace_map_from_data(map, size, data);
86}
87
88template <typename MatrixType, int Alignment>
89void emplace_resize_map(Eigen::Map<MatrixType, Alignment> &map,
90 Eigen::Index rows, Eigen::Index cols,
92 EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(MatrixType);
93 using MapType = Eigen::Map<MatrixType, Alignment>;
94 using Scalar = typename MatrixType::Scalar;
95 bool need_reallocate = map.size() != rows * cols;
96 Scalar *data = map.data();
97 if (data && need_reallocate) {
98 alloc.template deallocate<Scalar>(data, map.size(), Alignment);
99 data = alloc.template allocate<Scalar>(size_t(rows * cols));
100 }
101 map.~MapType();
102 ::new (&map) MapType{data, rows, cols};
103}
104
105template <typename MatrixType, int Alignment>
106void deallocate_map(Eigen::Map<MatrixType, Alignment> &map,
107 polymorphic_allocator alloc) {
108 EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(MatrixType);
109 using Scalar = typename MatrixType::Scalar;
110 size_t dealloc_size = size_t(map.size());
111 if (map.data() != NULL)
112 alloc.template deallocate<Scalar>(map.data(), dealloc_size, Alignment);
113}
114
116template <typename MatrixType, int Alignment>
117void emplace_map_copy(Eigen::Map<MatrixType, Alignment> &map,
118 const Eigen::Map<MatrixType, Alignment> &other,
119 polymorphic_allocator alloc) {
120 if constexpr (MatrixType::IsVectorAtCompileTime) {
121 emplace_allocated_map(map, other.size(), alloc);
122 } else {
123 emplace_allocated_map(map, other.rows(), other.cols(), alloc);
124 }
125 // now copy values using Eigen's copy operator=
126 map = other;
127}
128
129} // namespace aligator
A convenience subclass of std::pmr::polymorphic_allocator for bytes.
Definition allocator.hpp:17
T * allocate(size_t n, size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition allocator.hpp:33
Main package namespace.
void emplace_map_copy(Eigen::Map< MatrixType, Alignment > &map, const Eigen::Map< MatrixType, Alignment > &other, polymorphic_allocator alloc)
Create a deep copy of a managed Eigen::Map object.
void deallocate_map(Eigen::Map< MatrixType, Alignment > &map, polymorphic_allocator alloc)
auto allocate_eigen_map(polymorphic_allocator alloc, Eigen::Index rows, Eigen::Index cols)
Definition eigen-map.hpp:10
void emplace_map_from_data(Eigen::Map< MatrixType, Alignment > &map, Eigen::Index rows, Eigen::Index cols, typename MatrixType::Scalar *data)
Use placement new to create an Eigen::Map object with given dimensions and data pointer.
Definition eigen-map.hpp:49
void emplace_map_steal(Eigen::Map< MatrixType, Alignment > &map, Eigen::Map< MatrixType, Alignment > &other)
In-place construct a map from another one by stealing the other's data.
Definition eigen-map.hpp:31
void emplace_allocated_map(Eigen::Map< MatrixType, Alignment > &map, Eigen::Index rows, Eigen::Index cols, polymorphic_allocator alloc)
Use placement new and an allocator to create an Eigen::Map object to it.
Definition eigen-map.hpp:71
void emplace_resize_map(Eigen::Map< MatrixType, Alignment > &map, Eigen::Index rows, Eigen::Index cols, polymorphic_allocator alloc)
Definition eigen-map.hpp:89