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.

146 lines
5.9 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. Index arraysize = 2*(rows+4)*(cols+4);
  48. Scalar* a_array = internal::aligned_new<Scalar>(arraysize+1);
  49. Scalar* array = a_array;
  50. if(Alignment!=Aligned)
  51. array = (Scalar*)(ptrdiff_t(a_array) + (internal::packet_traits<Scalar>::AlignedOnScalar?sizeof(Scalar):sizeof(typename NumTraits<Scalar>::Real)));
  52. // test no inner stride and some dynamic outer stride
  53. {
  54. Map<MatrixType, Alignment, OuterStride<Dynamic> > map(array, rows, cols, OuterStride<Dynamic>(m.innerSize()+1));
  55. map = m;
  56. VERIFY(map.outerStride() == map.innerSize()+1);
  57. for(int i = 0; i < m.outerSize(); ++i)
  58. for(int j = 0; j < m.innerSize(); ++j)
  59. {
  60. VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
  61. VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
  62. }
  63. }
  64. // test no inner stride and an outer stride of +4. This is quite important as for fixed-size matrices,
  65. // this allows to hit the special case where it's vectorizable.
  66. {
  67. enum {
  68. InnerSize = MatrixType::InnerSizeAtCompileTime,
  69. OuterStrideAtCompileTime = InnerSize==Dynamic ? Dynamic : InnerSize+4
  70. };
  71. Map<MatrixType, Alignment, OuterStride<OuterStrideAtCompileTime> >
  72. map(array, rows, cols, OuterStride<OuterStrideAtCompileTime>(m.innerSize()+4));
  73. map = m;
  74. VERIFY(map.outerStride() == map.innerSize()+4);
  75. for(int i = 0; i < m.outerSize(); ++i)
  76. for(int j = 0; j < m.innerSize(); ++j)
  77. {
  78. VERIFY(array[map.outerStride()*i+j] == m.coeffByOuterInner(i,j));
  79. VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
  80. }
  81. }
  82. // test both inner stride and outer stride
  83. {
  84. Map<MatrixType, Alignment, Stride<Dynamic,Dynamic> > map(array, rows, cols, Stride<Dynamic,Dynamic>(2*m.innerSize()+1, 2));
  85. map = m;
  86. VERIFY(map.outerStride() == 2*map.innerSize()+1);
  87. VERIFY(map.innerStride() == 2);
  88. for(int i = 0; i < m.outerSize(); ++i)
  89. for(int j = 0; j < m.innerSize(); ++j)
  90. {
  91. VERIFY(array[map.outerStride()*i+map.innerStride()*j] == m.coeffByOuterInner(i,j));
  92. VERIFY(map.coeffByOuterInner(i,j) == m.coeffByOuterInner(i,j));
  93. }
  94. }
  95. internal::aligned_delete(a_array, arraysize+1);
  96. }
  97. void test_mapstride()
  98. {
  99. for(int i = 0; i < g_repeat; i++) {
  100. EIGEN_UNUSED int maxn = 30;
  101. CALL_SUBTEST_1( map_class_vector<Aligned>(Matrix<float, 1, 1>()) );
  102. CALL_SUBTEST_1( map_class_vector<Unaligned>(Matrix<float, 1, 1>()) );
  103. CALL_SUBTEST_2( map_class_vector<Aligned>(Vector4d()) );
  104. CALL_SUBTEST_2( map_class_vector<Unaligned>(Vector4d()) );
  105. CALL_SUBTEST_3( map_class_vector<Aligned>(RowVector4f()) );
  106. CALL_SUBTEST_3( map_class_vector<Unaligned>(RowVector4f()) );
  107. CALL_SUBTEST_4( map_class_vector<Aligned>(VectorXcf(internal::random<int>(1,maxn))) );
  108. CALL_SUBTEST_4( map_class_vector<Unaligned>(VectorXcf(internal::random<int>(1,maxn))) );
  109. CALL_SUBTEST_5( map_class_vector<Aligned>(VectorXi(internal::random<int>(1,maxn))) );
  110. CALL_SUBTEST_5( map_class_vector<Unaligned>(VectorXi(internal::random<int>(1,maxn))) );
  111. CALL_SUBTEST_1( map_class_matrix<Aligned>(Matrix<float, 1, 1>()) );
  112. CALL_SUBTEST_1( map_class_matrix<Unaligned>(Matrix<float, 1, 1>()) );
  113. CALL_SUBTEST_2( map_class_matrix<Aligned>(Matrix4d()) );
  114. CALL_SUBTEST_2( map_class_matrix<Unaligned>(Matrix4d()) );
  115. CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,3,5>()) );
  116. CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,3,5>()) );
  117. CALL_SUBTEST_3( map_class_matrix<Aligned>(Matrix<float,4,8>()) );
  118. CALL_SUBTEST_3( map_class_matrix<Unaligned>(Matrix<float,4,8>()) );
  119. CALL_SUBTEST_4( map_class_matrix<Aligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  120. CALL_SUBTEST_4( map_class_matrix<Unaligned>(MatrixXcf(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  121. CALL_SUBTEST_5( map_class_matrix<Aligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  122. CALL_SUBTEST_5( map_class_matrix<Unaligned>(MatrixXi(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  123. CALL_SUBTEST_6( map_class_matrix<Aligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  124. CALL_SUBTEST_6( map_class_matrix<Unaligned>(MatrixXcd(internal::random<int>(1,maxn),internal::random<int>(1,maxn))) );
  125. }
  126. }