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  internal::aligned_delete(a_array1, arraysize+1);
166 }
167 
168 // Additional tests for inner-stride but no outer-stride
169 template<int>
170 void bug1453()
171 {
172  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};
173  typedef Matrix<int,Dynamic,Dynamic,RowMajor> RowMatrixXi;
174  typedef Matrix<int,2,3,ColMajor> ColMatrix23i;
175  typedef Matrix<int,3,2,ColMajor> ColMatrix32i;
176  typedef Matrix<int,2,3,RowMajor> RowMatrix23i;
177  typedef Matrix<int,3,2,RowMajor> RowMatrix32i;
178 
179  VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
180  VERIFY_IS_APPROX(MatrixXi::Map(data, 2, 3, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
181  VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
182  VERIFY_IS_APPROX(MatrixXi::Map(data, 3, 2, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
183 
184  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
185  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 2, 3, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
186  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
187  VERIFY_IS_APPROX(RowMatrixXi::Map(data, 3, 2, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
188 
189  VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
190  VERIFY_IS_APPROX(ColMatrix23i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 2, 3, Stride<4,2>()));
191  VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<2>()), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
192  VERIFY_IS_APPROX(ColMatrix32i::Map(data, InnerStride<>(2)), MatrixXi::Map(data, 3, 2, Stride<6,2>()));
193 
194  VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
195  VERIFY_IS_APPROX(RowMatrix23i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 2, 3, Stride<6,2>()));
196  VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<2>()), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
197  VERIFY_IS_APPROX(RowMatrix32i::Map(data, InnerStride<>(2)), RowMatrixXi::Map(data, 3, 2, Stride<4,2>()));
198 }
199 
201 {
202  for(int i = 0; i < g_repeat; i++) {
203  int maxn = 30;
204  CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) );
205  CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) );
206  CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) );
207  CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) );
208  CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) );
209  CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) );
210  CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) );
211  CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) );
212  CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) );
213  CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) );
214 
215  CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) );
216  CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) );
217  CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) );
218  CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) );
219  CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) );
220  CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) );
221  CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) );
222  CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) );
223  CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
224  CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
225  CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
226  CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
227  CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
228  CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
229 
230  CALL_SUBTEST_5( bug1453<0>() );
231 
233  }
234 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define EIGEN_MAX_ALIGN_BYTES
Definition: Macros.h:775
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
ArrayXcf v
Definition: Cwise_arg.cpp:1
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:331
Holds strides information for Map.
Definition: Stride.h:44
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
std::size_t UIntPtr
Definition: Meta.h:51
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:50
#define VERIFY_IS_APPROX(a, b)
EIGEN_DEVICE_FUNC Index innerStride() const
Definition: Map.h:108
void bug1453()
Definition: mapstride.cpp:170
static int g_repeat
Definition: main.h:144
Convenience specialization of Stride to specify only an inner stride See class Map for some examples...
Definition: Stride.h:90
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
int data[]
EIGEN_DEVICE_FUNC Index outerStride() const
Definition: Map.h:114
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:91
#define VERIFY(a)
Definition: main.h:325
void test_mapstride()
Definition: mapstride.cpp:200
const int Dynamic
Definition: Constants.h:21
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:101
std::ptrdiff_t j
void map_class_vector(const VectorType &m)
Definition: mapstride.cpp:12
#define arraysize(array)
Definition: ceres/macros.h:91


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:45