You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
7.1 KiB

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