• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
map_class_vector(const VectorType & m)12 template<int Alignment,typename VectorType> void map_class_vector(const VectorType& m)
13 {
14   typedef typename VectorType::Index Index;
15   typedef typename VectorType::Scalar Scalar;
16 
17   Index size = m.size();
18 
19   VectorType v = VectorType::Random(size);
20 
21   Index arraysize = 3*size;
22 
23   Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1);
24   Scalar* array = a_array;
25   if(Alignment!=Aligned)
26     array = (Scalar*)(internal::IntPtr(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
27 
28   {
29     Map<VectorType, Alignment, InnerStride<3> > map(array, size);
30     map = v;
31     for(int i = 0; i < size; ++i)
32     {
33       VERIFY(array[3*i] == v[i]);
34       VERIFY(map[i] == v[i]);
35     }
36   }
37 
38   {
39     Map<VectorType, Unaligned, InnerStride<Dynamic> > map(array, size, InnerStride<Dynamic>(2));
40     map = v;
41     for(int i = 0; i < size; ++i)
42     {
43       VERIFY(array[2*i] == v[i]);
44       VERIFY(map[i] == v[i]);
45     }
46   }
47 
48   internal::aligned_delete(a_array, arraysize+1);
49 }
50 
map_class_matrix(const MatrixType & _m)51 template<int Alignment,typename MatrixType> void map_class_matrix(const MatrixType& _m)
52 {
53   typedef typename MatrixType::Index Index;
54   typedef typename MatrixType::Scalar Scalar;
55 
56   Index rows = _m.rows(), cols = _m.cols();
57 
58   MatrixType m = MatrixType::Random(rows,cols);
59   Scalar s1 = internal::random<Scalar>();
60 
61   Index arraysize = 2*(rows+4)*(cols+4);
62 
63   Scalar* a_array1 = internal::aligned_new<Scalar>(arraysize+1);
64   Scalar* array1 = a_array1;
65   if(Alignment!=Aligned)
66     array1 = (Scalar*)(internal::IntPtr(a_array1) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
67 
68   Scalar a_array2[256];
69   Scalar* array2 = a_array2;
70   if(Alignment!=Aligned)
71     array2 = (Scalar*)(internal::IntPtr(a_array2) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
72   else
73     array2 = (Scalar*)(((internal::UIntPtr(a_array2)+EIGEN_MAX_ALIGN_BYTES-1)/EIGEN_MAX_ALIGN_BYTES)*EIGEN_MAX_ALIGN_BYTES);
74   Index maxsize2 = a_array2 - array2 + 256;
75 
76   // test no inner stride and some dynamic outer stride
77   for(int k=0; k<2; ++k)
78   {
79     if(k==1 && (m.innerSize()+1)*m.outerSize() > maxsize2)
80       break;
81     Scalar* array = (k==0 ? array1 : array2);
82 
83     Map<MatrixType, Alignment, OuterStride<Dynamic> > map(array, rows, cols, OuterStride<Dynamic>(m.innerSize()+1));
84     map = m;
85     VERIFY(map.outerStride() == map.innerSize()+1);
86     for(int i = 0; i < m.outerSize(); ++i)
87       for(int j = 0; j < m.innerSize(); ++j)
88       {
89         VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
90         VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
91       }
92     VERIFY_IS_APPROX(s1*map,s1*m);
93     map *= s1;
94     VERIFY_IS_APPROX(map,s1*m);
95   }
96 
97   // test no inner stride and an outer stride of +4. This is quite important as for fixed-size matrices,
98   // this allows to hit the special case where it's vectorizable.
99   for(int k=0; k<2; ++k)
100   {
101     if(k==1 && (m.innerSize()+4)*m.outerSize() > maxsize2)
102       break;
103     Scalar* array = (k==0 ? array1 : array2);
104 
105     enum {
106       InnerSize = MatrixType::InnerSizeAtCompileTime,
107       OuterStrideAtCompileTime = InnerSize==Dynamic ? Dynamic : InnerSize+4
108     };
109     Map<MatrixType, Alignment, OuterStride<OuterStrideAtCompileTime> >
110       map(array, rows, cols, OuterStride<OuterStrideAtCompileTime>(m.innerSize()+4));
111     map = m;
112     VERIFY(map.outerStride() == map.innerSize()+4);
113     for(int i = 0; i < m.outerSize(); ++i)
114       for(int j = 0; j < m.innerSize(); ++j)
115       {
116         VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
117         VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
118       }
119     VERIFY_IS_APPROX(s1*map,s1*m);
120     map *= s1;
121     VERIFY_IS_APPROX(map,s1*m);
122   }
123 
124   // test both inner stride and outer stride
125   for(int k=0; k<2; ++k)
126   {
127     if(k==1 && (2*m.innerSize()+1)*(m.outerSize()*2) > maxsize2)
128       break;
129     Scalar* array = (k==0 ? array1 : array2);
130 
131     Map<MatrixType, Alignment, Stride<Dynamic,Dynamic> > map(array, rows, cols, Stride<Dynamic,Dynamic>(2*m.innerSize()+1, 2));
132     map = m;
133     VERIFY(map.outerStride() == 2*map.innerSize()+1);
134     VERIFY(map.innerStride() == 2);
135     for(int i = 0; i < m.outerSize(); ++i)
136       for(int j = 0; j < m.innerSize(); ++j)
137       {
138         VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j));
139         VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
140       }
141     VERIFY_IS_APPROX(s1*map,s1*m);
142     map *= s1;
143     VERIFY_IS_APPROX(map,s1*m);
144   }
145 
146   internal::aligned_delete(a_array1, arraysize+1);
147 }
148 
test_mapstride()149 void test_mapstride()
150 {
151   for(int i = 0; i < g_repeat; i++) {
152     int maxn = 30;
153     CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) );
154     CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) );
155     CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) );
156     CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) );
157     CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) );
158     CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) );
159     CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) );
160     CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) );
161     CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) );
162     CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) );
163 
164     CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) );
165     CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) );
166     CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) );
167     CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) );
168     CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) );
169     CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) );
170     CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) );
171     CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) );
172     CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
173     CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
174     CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
175     CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
176     CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
177     CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
178 
179     TEST_SET_BUT_UNUSED_VARIABLE(maxn);
180   }
181 }
182