dense_storage.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2013 Hauke Heibel <hauke.heibel@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 #include "AnnoyingScalar.h"
12 #include "SafeScalar.h"
13 
14 #include <Eigen/Core>
15 
16 #if EIGEN_HAS_TYPE_TRAITS && EIGEN_HAS_CXX11
17 using DenseStorageD3x3 = Eigen::DenseStorage<double, 3, 3, 3, 3>;
18 static_assert(std::is_trivially_move_constructible<DenseStorageD3x3>::value, "DenseStorage not trivially_move_constructible");
19 static_assert(std::is_trivially_move_assignable<DenseStorageD3x3>::value, "DenseStorage not trivially_move_assignable");
20 #if !defined(EIGEN_DENSE_STORAGE_CTOR_PLUGIN)
21 static_assert(std::is_trivially_copy_constructible<DenseStorageD3x3>::value, "DenseStorage not trivially_copy_constructible");
22 static_assert(std::is_trivially_copy_assignable<DenseStorageD3x3>::value, "DenseStorage not trivially_copy_assignable");
23 static_assert(std::is_trivially_copyable<DenseStorageD3x3>::value, "DenseStorage not trivially_copyable");
24 #endif
25 #endif
26 
27 template <typename T, int Size, int Rows, int Cols>
29 {
30  typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
31 
32  const int size = rows*cols;
33  DenseStorageType reference(size, rows, cols);
34  T* raw_reference = reference.data();
35  for (int i=0; i<size; ++i)
36  raw_reference[i] = static_cast<T>(i);
37 
38  DenseStorageType copied_reference(reference);
39  const T* raw_copied_reference = copied_reference.data();
40  for (int i=0; i<size; ++i)
41  VERIFY_IS_EQUAL(raw_reference[i], raw_copied_reference[i]);
42 }
43 
44 template <typename T, int Size, int Rows, int Cols>
46 {
47  typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
48 
49  const int size = rows*cols;
50  DenseStorageType reference(size, rows, cols);
51  T* raw_reference = reference.data();
52  for (int i=0; i<size; ++i)
53  raw_reference[i] = static_cast<T>(i);
54 
55  DenseStorageType copied_reference;
56  copied_reference = reference;
57  const T* raw_copied_reference = copied_reference.data();
58  for (int i=0; i<size; ++i)
59  VERIFY_IS_EQUAL(raw_reference[i], raw_copied_reference[i]);
60 }
61 
62 template <typename T, int Size, int Rows, int Cols>
63 void dense_storage_swap(int rows0, int cols0, int rows1, int cols1)
64 {
65  typedef DenseStorage<T, Size, Rows, Cols, 0> DenseStorageType;
66 
67  const int size0 = rows0*cols0;
68  DenseStorageType a(size0, rows0, cols0);
69  for (int i=0; i<size0; ++i) {
70  a.data()[i] = static_cast<T>(i);
71  }
72 
73  const int size1 = rows1*cols1;
74  DenseStorageType b(size1, rows1, cols1);
75  for (int i=0; i<size1; ++i) {
76  b.data()[i] = static_cast<T>(-i);
77  }
78 
79  a.swap(b);
80 
81  for (int i=0; i<size0; ++i) {
82  VERIFY_IS_EQUAL(b.data()[i], static_cast<T>(i));
83  }
84 
85  for (int i=0; i<size1; ++i) {
86  VERIFY_IS_EQUAL(a.data()[i], static_cast<T>(-i));
87  }
88 }
89 
90 template<typename T, int Size, std::size_t Alignment>
92 {
93  #if EIGEN_HAS_ALIGNAS
94 
95  struct alignas(Alignment) Empty1 {};
97 
98  struct EIGEN_ALIGN_TO_BOUNDARY(Alignment) Empty2 {};
100 
101  struct Nested1 { EIGEN_ALIGN_TO_BOUNDARY(Alignment) T data[Size]; };
103 
104  VERIFY_IS_EQUAL( (std::alignment_of<internal::plain_array<T,Size,AutoAlign,Alignment> >::value), Alignment);
105 
107 
108  VERIFY_IS_EQUAL( (std::alignment_of<DenseStorage<T,Size,1,1,AutoAlign> >::value), default_alignment);
109  VERIFY_IS_EQUAL( (std::alignment_of<Matrix<T,Size,1,AutoAlign> >::value), default_alignment);
110  struct Nested2 { Matrix<T,Size,1,AutoAlign> mat; };
112 
113  #endif
114 }
115 
116 template<typename T>
118  // Dynamic Storage.
119  dense_storage_copy<T,Dynamic,Dynamic,Dynamic>(4, 3);
120  dense_storage_copy<T,Dynamic,Dynamic,3>(4, 3);
121  dense_storage_copy<T,Dynamic,4,Dynamic>(4, 3);
122  // Fixed Storage.
123  dense_storage_copy<T,12,4,3>(4, 3);
124  dense_storage_copy<T,12,Dynamic,Dynamic>(4, 3);
125  dense_storage_copy<T,12,4,Dynamic>(4, 3);
126  dense_storage_copy<T,12,Dynamic,3>(4, 3);
127  // Fixed Storage with Uninitialized Elements.
128  dense_storage_copy<T,18,Dynamic,Dynamic>(4, 3);
129  dense_storage_copy<T,18,4,Dynamic>(4, 3);
130  dense_storage_copy<T,18,Dynamic,3>(4, 3);
131 
132  // Dynamic Storage.
133  dense_storage_assignment<T,Dynamic,Dynamic,Dynamic>(4, 3);
134  dense_storage_assignment<T,Dynamic,Dynamic,3>(4, 3);
135  dense_storage_assignment<T,Dynamic,4,Dynamic>(4, 3);
136  // Fixed Storage.
137  dense_storage_assignment<T,12,4,3>(4, 3);
138  dense_storage_assignment<T,12,Dynamic,Dynamic>(4, 3);
139  dense_storage_assignment<T,12,4,Dynamic>(4, 3);
140  dense_storage_assignment<T,12,Dynamic,3>(4, 3);
141  // Fixed Storage with Uninitialized Elements.
142  dense_storage_assignment<T,18,Dynamic,Dynamic>(4, 3);
143  dense_storage_assignment<T,18,4,Dynamic>(4, 3);
144  dense_storage_assignment<T,18,Dynamic,3>(4, 3);
145 
146  // Dynamic Storage.
147  dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(4, 3, 4, 3);
148  dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(4, 3, 2, 1);
149  dense_storage_swap<T,Dynamic,Dynamic,Dynamic>(2, 1, 4, 3);
150  dense_storage_swap<T,Dynamic,Dynamic,3>(4, 3, 4, 3);
151  dense_storage_swap<T,Dynamic,Dynamic,3>(4, 3, 2, 3);
152  dense_storage_swap<T,Dynamic,Dynamic,3>(2, 3, 4, 3);
153  dense_storage_swap<T,Dynamic,4,Dynamic>(4, 3, 4, 3);
154  dense_storage_swap<T,Dynamic,4,Dynamic>(4, 3, 4, 1);
155  dense_storage_swap<T,Dynamic,4,Dynamic>(4, 1, 4, 3);
156  // Fixed Storage.
157  dense_storage_swap<T,12,4,3>(4, 3, 4, 3);
158  dense_storage_swap<T,12,Dynamic,Dynamic>(4, 3, 4, 3);
159  dense_storage_swap<T,12,Dynamic,Dynamic>(4, 3, 2, 1);
160  dense_storage_swap<T,12,Dynamic,Dynamic>(2, 1, 4, 3);
161  dense_storage_swap<T,12,4,Dynamic>(4, 3, 4, 3);
162  dense_storage_swap<T,12,4,Dynamic>(4, 3, 4, 1);
163  dense_storage_swap<T,12,4,Dynamic>(4, 1, 4, 3);
164  dense_storage_swap<T,12,Dynamic,3>(4, 3, 4, 3);
165  dense_storage_swap<T,12,Dynamic,3>(4, 3, 2, 3);
166  dense_storage_swap<T,12,Dynamic,3>(2, 3, 4, 3);
167  // Fixed Storage with Uninitialized Elements.
168  dense_storage_swap<T,18,Dynamic,Dynamic>(4, 3, 4, 3);
169  dense_storage_swap<T,18,Dynamic,Dynamic>(4, 3, 2, 1);
170  dense_storage_swap<T,18,Dynamic,Dynamic>(2, 1, 4, 3);
171  dense_storage_swap<T,18,4,Dynamic>(4, 3, 4, 3);
172  dense_storage_swap<T,18,4,Dynamic>(4, 3, 4, 1);
173  dense_storage_swap<T,18,4,Dynamic>(4, 1, 4, 3);
174  dense_storage_swap<T,18,Dynamic,3>(4, 3, 4, 3);
175  dense_storage_swap<T,18,Dynamic,3>(4, 3, 2, 3);
176  dense_storage_swap<T,18,Dynamic,3>(2, 3, 4, 3);
177 
178  dense_storage_alignment<T,16,8>();
179  dense_storage_alignment<T,16,16>();
180  dense_storage_alignment<T,16,32>();
181  dense_storage_alignment<T,16,64>();
182 }
183 
184 EIGEN_DECLARE_TEST(dense_storage)
185 {
186  dense_storage_tests<int>();
187  dense_storage_tests<float>();
188  dense_storage_tests<SafeScalar<float> >();
189  dense_storage_tests<AnnoyingScalar>();
190 }
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
b
Scalar * b
Definition: benchVecAdd.cpp:17
AnnoyingScalar.h
Eigen::DenseStorage
Definition: DenseStorage.h:204
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
dense_storage_copy
void dense_storage_copy(int rows, int cols)
Definition: dense_storage.cpp:28
data
int data[]
Definition: Map_placement_new.cpp:1
return_value_policy::reference
@ reference
SafeScalar.h
Eigen::Triplet< double >
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
dense_storage_alignment
void dense_storage_alignment()
Definition: dense_storage.cpp:91
dense_storage_assignment
void dense_storage_assignment(int rows, int cols)
Definition: dense_storage.cpp:45
dense_storage_tests
void dense_storage_tests()
Definition: dense_storage.cpp:117
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
main.h
dense_storage_swap
void dense_storage_swap(int rows0, int cols0, int rows1, int cols1)
Definition: dense_storage.cpp:63
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(dense_storage)
Definition: dense_storage.cpp:184
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:11