mapstride.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) 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 #include "main.h"
11 
12 template<int Alignment,typename VectorType> void map_class_vector(const VectorType& m)
13 {
14  typedef typename VectorType::Scalar Scalar;
15 
16  Index size = m.size();
17 
18  VectorType v = VectorType::Random(size);
19 
20  Index arraysize = 3*size;
21 
22  Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1);
23  Scalar* array = a_array;
24  if(Alignment!=Aligned)
25  array = (Scalar*)(internal::IntPtr(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
26 
27  {
29  map = v;
30  for(int i = 0; i < size; ++i)
31  {
32  VERIFY(array[3*i] == v[i]);
33  VERIFY(map[i] == v[i]);
34  }
35  }
36 
37  {
39  map = v;
40  for(int i = 0; i < size; ++i)
41  {
42  VERIFY(array[2*i] == v[i]);
43  VERIFY(map[i] == v[i]);
44  }
45  }
46 
48 }
49 
50 template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixType& _m)
51 {
52  typedef typename MatrixType::Scalar Scalar;
53 
54  Index rows = _m.rows(), cols = _m.cols();
55 
56  MatrixType m = MatrixType::Random(rows,cols);
57  Scalar s1 = internal::random<Scalar>();
58 
59  Index arraysize = 4*(rows+4)*(cols+4);
60 
61  Scalar* a_array1 = internal::aligned_new<Scalar>(arraysize+1);
62  Scalar* array1 = a_array1;
63  if(Alignment!=Aligned)
64  array1 = (Scalar*)(internal::IntPtr(a_array1) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
65 
66  Scalar a_array2[256];
67  Scalar* array2 = a_array2;
68  if(Alignment!=Aligned)
69  array2 = (Scalar*)(internal::IntPtr(a_array2) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
70  else
72  Index maxsize2 = a_array2 - array2 + 256;
73 
74  // test no inner stride and some dynamic outer stride
75  for(int k=0; k<2; ++k)
76  {
77  if(k==1 && (m.innerSize()+1)*m.outerSize() > maxsize2)
78  break;
79  Scalar* array = (k==0 ? array1 : array2);
80 
82  map = m;
83  VERIFY(map.outerStride() == map.innerSize()+1);
84  for(int i = 0; i < m.outerSize(); ++i)
85  for(int j = 0; j < m.innerSize(); ++j)
86  {
87  VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
88  VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
89  }
90  VERIFY_IS_APPROX(s1*map,s1*m);
91  map *= s1;
92  VERIFY_IS_APPROX(map,s1*m);
93  }
94 
95  // test no inner stride and an outer stride of +4. This is quite important as for fixed-size matrices,
96  // this allows to hit the special case where it's vectorizable.
97  for(int k=0; k<2; ++k)
98  {
99  if(k==1 && (m.innerSize()+4)*m.outerSize() > maxsize2)
100  break;
101  Scalar* array = (k==0 ? array1 : array2);
102 
103  enum {
104  InnerSize = MatrixType::InnerSizeAtCompileTime,
105  OuterStrideAtCompileTime = InnerSize==Dynamic ? Dynamic : InnerSize+4
106  };
108  map(array, rows, cols, OuterStride<OuterStrideAtCompileTime>(m.innerSize()+4));
109  map = m;
110  VERIFY(map.outerStride() == map.innerSize()+4);
111  for(int i = 0; i < m.outerSize(); ++i)
112  for(int j = 0; j < m.innerSize(); ++j)
113  {
114  VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
115  VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
116  }
117  VERIFY_IS_APPROX(s1*map,s1*m);
118  map *= s1;
119  VERIFY_IS_APPROX(map,s1*m);
120  }
121 
122  // test both inner stride and outer stride
123  for(int k=0; k<2; ++k)
124  {
125  if(k==1 && (2*m.innerSize()+1)*(m.outerSize()*2) > maxsize2)
126  break;
127  Scalar* array = (k==0 ? array1 : array2);
128 
130  map = m;
131  VERIFY(map.outerStride() == 2*map.innerSize()+1);
132  VERIFY(map.innerStride() == 2);
133  for(int i = 0; i < m.outerSize(); ++i)
134  for(int j = 0; j < m.innerSize(); ++j)
135  {
136  VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j));
137  VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
138  }
139  VERIFY_IS_APPROX(s1*map,s1*m);
140  map *= s1;
141  VERIFY_IS_APPROX(map,s1*m);
142  }
143 
144  // test inner stride and no outer stride
145  for(int k=0; k<2; ++k)
146  {
147  if(k==1 && (m.innerSize()*2)*m.outerSize() > maxsize2)
148  break;
149  Scalar* array = (k==0 ? array1 : array2);
150 
152  map = m;
153  VERIFY(map.outerStride() == map.innerSize()*2);
154  for(int i = 0; i < m.outerSize(); ++i)
155  for(int j = 0; j < m.innerSize(); ++j)
156  {
157  VERIFY(array[map.innerSize()*i*2+j*2] == m.coeffByOuterInner(i,j));
158  VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
159  }
160  VERIFY_IS_APPROX(s1*map,s1*m);
161  map *= s1;
162  VERIFY_IS_APPROX(map,s1*m);
163  }
164 
165  // test negative strides
166  {
168  Index outerstride = m.innerSize()+4;
169  Scalar* array = array1;
170 
171  {
173  Map<MatrixType, Unaligned, OuterStride<> > map2(array+(m.outerSize()-1)*outerstride, rows, cols, OuterStride<>(-outerstride));
174  if(MatrixType::IsRowMajor) VERIFY_IS_APPROX(map1.colwise().reverse(), map2);
175  else VERIFY_IS_APPROX(map1.rowwise().reverse(), map2);
176  }
177 
178  {
180  Map<MatrixType, Unaligned, Stride<Dynamic,Dynamic> > map2(array+(m.outerSize()-1)*outerstride+m.innerSize()-1, rows, cols, Stride<Dynamic,Dynamic>(-outerstride,-1));
181  VERIFY_IS_APPROX(map1.reverse(), map2);
182  }
183 
184  {
186  Map<MatrixType, Unaligned, Stride<Dynamic,-1> > map2(array+(m.outerSize()-1)*outerstride+m.innerSize()-1, rows, cols, Stride<Dynamic,-1>(-outerstride,-1));
187  VERIFY_IS_APPROX(map1.reverse(), map2);
188  }
189  }
190 
191  internal::aligned_delete(a_array1, arraysize+1);
192 }
193 
194 // Additional tests for inner-stride but no outer-stride
195 template<int>
196 void bug1453()
197 {
198  const int data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
199  typedef Matrix<int,Dynamic,Dynamic,RowMajor> RowMatrixXi;
200  typedef Matrix<int,2,3,ColMajor> ColMatrix23i;
201  typedef Matrix<int,3,2,ColMajor> ColMatrix32i;
202  typedef Matrix<int,2,3,RowMajor> RowMatrix23i;
203  typedef Matrix<int,3,2,RowMajor> RowMatrix32i;
204 
205  VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
206  VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
207  VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
208  VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
209 
210  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
211  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
212  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
213  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
214 
215  VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
216  VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
217  VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
218  VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
219 
220  VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
221  VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
222  VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
223  VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
224 }
225 
227 {
228  for(int i = 0; i < g_repeat; i++) {
229  int maxn = 3;
230  CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) );
231  CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) );
232  CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) );
233  CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) );
234  CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) );
235  CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) );
236  CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) );
237  CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) );
238  CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) );
239  CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) );
240 
241  CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) );
242  CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) );
243  CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) );
244  CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) );
245  CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) );
246  CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) );
247  CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) );
248  CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) );
249  CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
250  CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
251  CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
252  CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
253  CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
254  CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
255 
256  CALL_SUBTEST_5( bug1453<0>() );
257 
259  }
260 }
Eigen::PlainObjectBase::setRandom
Derived & setRandom(Index size)
Definition: Random.h:151
Eigen::Stride
Holds strides information for Map.
Definition: Stride.h:48
Eigen::internal::UIntPtr
std::size_t UIntPtr
Definition: Meta.h:92
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Eigen::array
Definition: EmulateArray.h:21
Eigen::Unaligned
@ Unaligned
Definition: Constants.h:233
Eigen::InnerStride
Convenience specialization of Stride to specify only an inner stride See class Map for some examples.
Definition: Stride.h:95
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
bug1453
void bug1453()
Definition: mapstride.cpp:196
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
map_class_vector
void map_class_vector(const VectorType &m)
Definition: mapstride.cpp:12
Eigen::Map::innerStride
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const
Definition: Map.h:108
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
data
int data[]
Definition: Map_placement_new.cpp:1
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
Eigen::Map::outerStride
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: Map.h:114
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::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
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
arraysize
#define arraysize(array)
Definition: ceres/macros.h:91
map_class_matrix
void map_class_matrix(const MatrixType &_m)
Definition: mapstride.cpp:50
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
VectorType
Definition: FFTW.cpp:65
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(mapstride)
Definition: mapstride.cpp:226
Eigen::internal::IntPtr
std::ptrdiff_t IntPtr
Definition: Meta.h:91
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
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
Eigen::Aligned
@ Aligned
Definition: Constants.h:240


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