mapped_matrix.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) 2006-2010 Benoit Jacob <jacob.benoit.1@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 #ifndef EIGEN_NO_STATIC_ASSERT
11 #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
12 #endif
13 
14 #include "main.h"
15 
16 #define EIGEN_TESTMAP_MAX_SIZE 256
17 
18 template<typename VectorType> void map_class_vector(const VectorType& m)
19 {
20  typedef typename VectorType::Scalar Scalar;
21 
22  Index size = m.size();
23 
24  Scalar* array1 = internal::aligned_new<Scalar>(size);
25  Scalar* array2 = internal::aligned_new<Scalar>(size);
26  Scalar* array3 = new Scalar[size+1];
27  Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
29 
30  Map<VectorType, AlignedMax>(array1, size) = VectorType::Random(size);
32  Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
36  VectorType ma3 = Map<VectorType>(array3unaligned, size);
37  VectorType ma4 = Map<VectorType>(array4, size);
38  VERIFY_IS_EQUAL(ma1, ma2);
39  VERIFY_IS_EQUAL(ma1, ma3);
40  VERIFY_IS_EQUAL(ma1, ma4);
41  #ifdef EIGEN_VECTORIZE
42  if(internal::packet_traits<Scalar>::Vectorizable && size>=AlignedMax)
44  #endif
45 
48  delete[] array3;
49 }
50 
51 template<typename MatrixType> void map_class_matrix(const MatrixType& m)
52 {
53  typedef typename MatrixType::Scalar Scalar;
54 
55  Index rows = m.rows(), cols = m.cols(), size = rows*cols;
56  Scalar s1 = internal::random<Scalar>();
57 
58  // array1 and array2 -> aligned heap allocation
59  Scalar* array1 = internal::aligned_new<Scalar>(size);
60  for(int i = 0; i < size; i++) array1[i] = Scalar(1);
61  Scalar* array2 = internal::aligned_new<Scalar>(size);
62  for(int i = 0; i < size; i++) array2[i] = Scalar(1);
63  // array3unaligned -> unaligned pointer to heap
64  Scalar* array3 = new Scalar[size+1];
65  Index sizep1 = size + 1; // <- without this temporary MSVC 2103 generates bad code
66  for(Index i = 0; i < sizep1; i++) array3[i] = Scalar(1);
67  Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
68  Scalar array4[256];
69  if(size<=256)
70  for(int i = 0; i < size; i++) array4[i] = Scalar(1);
71 
72  Map<MatrixType> map1(array1, rows, cols);
73  Map<MatrixType, AlignedMax> map2(array2, rows, cols);
74  Map<MatrixType> map3(array3unaligned, rows, cols);
75  Map<MatrixType> map4(array4, rows, cols);
76 
77  VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols));
78  VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols));
79  VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols));
80  map1 = MatrixType::Random(rows,cols);
81  map2 = map1;
82  map3 = map1;
83  MatrixType ma1 = map1;
84  MatrixType ma2 = map2;
85  MatrixType ma3 = map3;
86  VERIFY_IS_EQUAL(map1, map2);
87  VERIFY_IS_EQUAL(map1, map3);
88  VERIFY_IS_EQUAL(ma1, ma2);
89  VERIFY_IS_EQUAL(ma1, ma3);
90  VERIFY_IS_EQUAL(ma1, map3);
91 
92  VERIFY_IS_APPROX(s1*map1, s1*map2);
93  VERIFY_IS_APPROX(s1*ma1, s1*ma2);
94  VERIFY_IS_EQUAL(s1*ma1, s1*ma3);
95  VERIFY_IS_APPROX(s1*map1, s1*map3);
96 
97  map2 *= s1;
98  map3 *= s1;
99  VERIFY_IS_APPROX(s1*map1, map2);
100  VERIFY_IS_APPROX(s1*map1, map3);
101 
102  if(size<=256)
103  {
104  VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols));
105  map4 = map1;
106  MatrixType ma4 = map4;
107  VERIFY_IS_EQUAL(map1, map4);
108  VERIFY_IS_EQUAL(ma1, map4);
109  VERIFY_IS_EQUAL(ma1, ma4);
110  VERIFY_IS_APPROX(s1*map1, s1*map4);
111 
112  map4 *= s1;
113  VERIFY_IS_APPROX(s1*map1, map4);
114  }
115 
118  delete[] array3;
119 }
120 
121 template<typename VectorType> void map_static_methods(const VectorType& m)
122 {
123  typedef typename VectorType::Scalar Scalar;
124 
125  Index size = m.size();
126 
127  Scalar* array1 = internal::aligned_new<Scalar>(size);
128  Scalar* array2 = internal::aligned_new<Scalar>(size);
129  Scalar* array3 = new Scalar[size+1];
130  Scalar* array3unaligned = internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3;
131 
132  VectorType::MapAligned(array1, size) = VectorType::Random(size);
133  VectorType::Map(array2, size) = VectorType::Map(array1, size);
134  VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
135  VectorType ma1 = VectorType::Map(array1, size);
136  VectorType ma2 = VectorType::MapAligned(array2, size);
137  VectorType ma3 = VectorType::Map(array3unaligned, size);
138  VERIFY_IS_EQUAL(ma1, ma2);
139  VERIFY_IS_EQUAL(ma1, ma3);
140 
143  delete[] array3;
144 }
145 
146 template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
147 {
148  // there's a lot that we can't test here while still having this test compile!
149  // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
150  // CMake can help with that.
151 
152  // verify that map-to-const don't have LvalueBit
153  typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
154  VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
155  VERIFY( !(internal::traits<Map<ConstPlainObjectType, AlignedMax> >::Flags & LvalueBit) );
158 }
159 
160 template<typename Scalar>
162 {
164  Index size = 11;
165  Scalar* array1 = internal::aligned_new<Scalar>((size+1)*(size+1)+1);
166  Scalar* array2 = reinterpret_cast<Scalar*>(sizeof(Scalar)/2+std::size_t(array1));
168  MatrixType m2 = MatrixType::Random(size,size);
169  map2 = m2;
170  VERIFY_IS_EQUAL(m2, map2);
171 
173  Map<VectorType> map3(array2, size);
174  MatrixType v3 = VectorType::Random(size);
175  map3 = v3;
176  VERIFY_IS_EQUAL(v3, map3);
177 
178  internal::aligned_delete(array1, (size+1)*(size+1)+1);
179 }
180 
181 EIGEN_DECLARE_TEST(mapped_matrix)
182 {
183  for(int i = 0; i < g_repeat; i++) {
186  CALL_SUBTEST_2( map_class_vector(Vector4d()) );
187  CALL_SUBTEST_2( map_class_vector(VectorXd(13)) );
189  CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
190  CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
191  CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
192  CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
193 
195  CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
197  CALL_SUBTEST_4( map_class_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
198  CALL_SUBTEST_5( map_class_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
199 
201  CALL_SUBTEST_7( map_static_methods(Vector3f()) );
202  CALL_SUBTEST_8( map_static_methods(RowVector3d()) );
203  CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) );
204  CALL_SUBTEST_10( map_static_methods(VectorXf(12)) );
205  CALL_SUBTEST_11( map_not_aligned_on_scalar<double>() );
206  }
207 }
map_static_methods
void map_static_methods(const VectorType &m)
Definition: mapped_matrix.cpp:121
map_class_vector
void map_class_vector(const VectorType &m)
Definition: mapped_matrix.cpp:18
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
Eigen::internal::UIntPtr
std::size_t UIntPtr
Definition: Meta.h:92
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
check_const_correctness
void check_const_correctness(const PlainObjectType &)
Definition: mapped_matrix.cpp:146
CALL_SUBTEST_11
#define CALL_SUBTEST_11(FUNC)
Definition: split_test_helper.h:64
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
map_class_matrix
void map_class_matrix(const MatrixType &m)
Definition: mapped_matrix.cpp:51
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
EIGEN_TESTMAP_MAX_SIZE
#define EIGEN_TESTMAP_MAX_SIZE
Definition: mapped_matrix.cpp:16
Eigen::LvalueBit
const unsigned int LvalueBit
Definition: Constants.h:144
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
EIGEN_MAX_ALIGN_BYTES
#define EIGEN_MAX_ALIGN_BYTES
Definition: ConfigureVectorization.h:175
Eigen::OuterStride
Convenience specialization of Stride to specify only an outer stride See class Map for some examples.
Definition: Stride.h:106
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(mapped_matrix)
Definition: mapped_matrix.cpp:181
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
Eigen::internal::aligned_delete
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:361
main.h
Eigen::AlignedMax
@ AlignedMax
Definition: Constants.h:252
Eigen::Matrix< Scalar, Dynamic, Dynamic >
VectorType
Definition: FFTW.cpp:65
v3
Vector v3
Definition: testSerializationBase.cpp:40
map_not_aligned_on_scalar
void map_not_aligned_on_scalar()
Definition: mapped_matrix.cpp:161
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:12:08