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 
47  internal::aligned_delete(a_array, arraysize+1);
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 
129  Map<MatrixType, Alignment, Stride<Dynamic,Dynamic> > map(array, rows, cols, Stride<Dynamic,Dynamic>(2*m.innerSize()+1, 2));
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  {
167  Matrix<Scalar,Dynamic,1>::Map(a_array1, arraysize+1).setRandom();
168  Index outerstride = m.innerSize()+4;
169  Scalar* array = array1;
170 
171  {
172  Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride));
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  {
179  Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride));
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  {
185  Map<MatrixType, Alignment, OuterStride<> > map1(array, rows, cols, OuterStride<>( outerstride));
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 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define CALL_SUBTEST_6(FUNC)
#define CALL_SUBTEST_4(FUNC)
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
#define CALL_SUBTEST_3(FUNC)
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:361
Holds strides information for Map.
Definition: Stride.h:48
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
std::size_t UIntPtr
Definition: Meta.h:92
void map_class_matrix(const MatrixType &_m)
Definition: mapstride.cpp:50
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
std::ptrdiff_t IntPtr
Definition: Meta.h:91
#define VERIFY_IS_APPROX(a, b)
void bug1453()
Definition: mapstride.cpp:196
#define CALL_SUBTEST_1(FUNC)
static int g_repeat
Definition: main.h:169
Convenience specialization of Stride to specify only an inner stride See class Map for some examples...
Definition: Stride.h:95
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Array< int, Dynamic, 1 > v
int data[]
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
#define EIGEN_MAX_ALIGN_BYTES
#define CALL_SUBTEST_2(FUNC)
const int Dynamic
Definition: Constants.h:22
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index outerStride() const
Definition: Map.h:114
The matrix class, also used for vectors and row-vectors.
Convenience specialization of Stride to specify only an outer stride See class Map for some examples...
Definition: Stride.h:106
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index innerStride() const
Definition: Map.h:108
Derived & setRandom(Index size)
Definition: Random.h:151
std::ptrdiff_t j
void map_class_vector(const VectorType &m)
Definition: mapstride.cpp:12
#define arraysize(array)
Definition: ceres/macros.h:91
EIGEN_DECLARE_TEST(mapstride)
Definition: mapstride.cpp:226


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:45