mapped_matrix.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-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. #ifndef EIGEN_NO_STATIC_ASSERT
  10. #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
  11. #endif
  12. #include "main.h"
  13. #define EIGEN_TESTMAP_MAX_SIZE 256
  14. template<typename VectorType> void map_class_vector(const VectorType& m)
  15. {
  16. typedef typename VectorType::Scalar Scalar;
  17. Index size = m.size();
  18. Scalar* array1 = internal::aligned_new<Scalar>(size);
  19. Scalar* array2 = internal::aligned_new<Scalar>(size);
  20. Scalar* array3 = new Scalar[size+1];
  21. Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
  22. Scalar array4[EIGEN_TESTMAP_MAX_SIZE];
  23. Map<VectorType, AlignedMax>(array1, size) = VectorType::Random(size);
  24. Map<VectorType, AlignedMax>(array2, size) = Map<VectorType,AlignedMax>(array1, size);
  25. Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
  26. Map<VectorType>(array4, size) = Map<VectorType,AlignedMax>(array1, size);
  27. VectorType ma1 = Map<VectorType, AlignedMax>(array1, size);
  28. VectorType ma2 = Map<VectorType, AlignedMax>(array2, size);
  29. VectorType ma3 = Map<VectorType>(array3unaligned, size);
  30. VectorType ma4 = Map<VectorType>(array4, size);
  31. VERIFY_IS_EQUAL(ma1, ma2);
  32. VERIFY_IS_EQUAL(ma1, ma3);
  33. VERIFY_IS_EQUAL(ma1, ma4);
  34. #ifdef EIGEN_VECTORIZE
  35. if(internal::packet_traits<Scalar>::Vectorizable && size>=AlignedMax)
  36. VERIFY_RAISES_ASSERT((Map<VectorType,AlignedMax>(array3unaligned, size)))
  37. #endif
  38. internal::aligned_delete(array1, size);
  39. internal::aligned_delete(array2, size);
  40. delete[] array3;
  41. }
  42. template<typename MatrixType> void map_class_matrix(const MatrixType& m)
  43. {
  44. typedef typename MatrixType::Scalar Scalar;
  45. Index rows = m.rows(), cols = m.cols(), size = rows*cols;
  46. Scalar s1 = internal::random<Scalar>();
  47. // array1 and array2 -> aligned heap allocation
  48. Scalar* array1 = internal::aligned_new<Scalar>(size);
  49. for(int i = 0; i < size; i++) array1[i] = Scalar(1);
  50. Scalar* array2 = internal::aligned_new<Scalar>(size);
  51. for(int i = 0; i < size; i++) array2[i] = Scalar(1);
  52. // array3unaligned -> unaligned pointer to heap
  53. Scalar* array3 = new Scalar[size+1];
  54. Index sizep1 = size + 1; // <- without this temporary MSVC 2103 generates bad code
  55. for(Index i = 0; i < sizep1; i++) array3[i] = Scalar(1);
  56. Scalar* array3unaligned = (internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
  57. Scalar array4[256];
  58. if(size<=256)
  59. for(int i = 0; i < size; i++) array4[i] = Scalar(1);
  60. Map<MatrixType> map1(array1, rows, cols);
  61. Map<MatrixType, AlignedMax> map2(array2, rows, cols);
  62. Map<MatrixType> map3(array3unaligned, rows, cols);
  63. Map<MatrixType> map4(array4, rows, cols);
  64. VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols));
  65. VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols));
  66. VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols));
  67. map1 = MatrixType::Random(rows,cols);
  68. map2 = map1;
  69. map3 = map1;
  70. MatrixType ma1 = map1;
  71. MatrixType ma2 = map2;
  72. MatrixType ma3 = map3;
  73. VERIFY_IS_EQUAL(map1, map2);
  74. VERIFY_IS_EQUAL(map1, map3);
  75. VERIFY_IS_EQUAL(ma1, ma2);
  76. VERIFY_IS_EQUAL(ma1, ma3);
  77. VERIFY_IS_EQUAL(ma1, map3);
  78. VERIFY_IS_APPROX(s1*map1, s1*map2);
  79. VERIFY_IS_APPROX(s1*ma1, s1*ma2);
  80. VERIFY_IS_EQUAL(s1*ma1, s1*ma3);
  81. VERIFY_IS_APPROX(s1*map1, s1*map3);
  82. map2 *= s1;
  83. map3 *= s1;
  84. VERIFY_IS_APPROX(s1*map1, map2);
  85. VERIFY_IS_APPROX(s1*map1, map3);
  86. if(size<=256)
  87. {
  88. VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols));
  89. map4 = map1;
  90. MatrixType ma4 = map4;
  91. VERIFY_IS_EQUAL(map1, map4);
  92. VERIFY_IS_EQUAL(ma1, map4);
  93. VERIFY_IS_EQUAL(ma1, ma4);
  94. VERIFY_IS_APPROX(s1*map1, s1*map4);
  95. map4 *= s1;
  96. VERIFY_IS_APPROX(s1*map1, map4);
  97. }
  98. internal::aligned_delete(array1, size);
  99. internal::aligned_delete(array2, size);
  100. delete[] array3;
  101. }
  102. template<typename VectorType> void map_static_methods(const VectorType& m)
  103. {
  104. typedef typename VectorType::Scalar Scalar;
  105. Index size = m.size();
  106. Scalar* array1 = internal::aligned_new<Scalar>(size);
  107. Scalar* array2 = internal::aligned_new<Scalar>(size);
  108. Scalar* array3 = new Scalar[size+1];
  109. Scalar* array3unaligned = internal::UIntPtr(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3;
  110. VectorType::MapAligned(array1, size) = VectorType::Random(size);
  111. VectorType::Map(array2, size) = VectorType::Map(array1, size);
  112. VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
  113. VectorType ma1 = VectorType::Map(array1, size);
  114. VectorType ma2 = VectorType::MapAligned(array2, size);
  115. VectorType ma3 = VectorType::Map(array3unaligned, size);
  116. VERIFY_IS_EQUAL(ma1, ma2);
  117. VERIFY_IS_EQUAL(ma1, ma3);
  118. internal::aligned_delete(array1, size);
  119. internal::aligned_delete(array2, size);
  120. delete[] array3;
  121. }
  122. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  123. {
  124. // there's a lot that we can't test here while still having this test compile!
  125. // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
  126. // CMake can help with that.
  127. // verify that map-to-const don't have LvalueBit
  128. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  129. VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
  130. VERIFY( !(internal::traits<Map<ConstPlainObjectType, AlignedMax> >::Flags & LvalueBit) );
  131. VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
  132. VERIFY( !(Map<ConstPlainObjectType, AlignedMax>::Flags & LvalueBit) );
  133. }
  134. template<typename Scalar>
  135. void map_not_aligned_on_scalar()
  136. {
  137. typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
  138. Index size = 11;
  139. Scalar* array1 = internal::aligned_new<Scalar>((size+1)*(size+1)+1);
  140. Scalar* array2 = reinterpret_cast<Scalar*>(sizeof(Scalar)/2+std::size_t(array1));
  141. Map<MatrixType,0,OuterStride<> > map2(array2, size, size, OuterStride<>(size+1));
  142. MatrixType m2 = MatrixType::Random(size,size);
  143. map2 = m2;
  144. VERIFY_IS_EQUAL(m2, map2);
  145. typedef Matrix<Scalar,Dynamic,1> VectorType;
  146. Map<VectorType> map3(array2, size);
  147. MatrixType v3 = VectorType::Random(size);
  148. map3 = v3;
  149. VERIFY_IS_EQUAL(v3, map3);
  150. internal::aligned_delete(array1, (size+1)*(size+1)+1);
  151. }
  152. EIGEN_DECLARE_TEST(mapped_matrix)
  153. {
  154. for(int i = 0; i < g_repeat; i++) {
  155. CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) );
  156. CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
  157. CALL_SUBTEST_2( map_class_vector(Vector4d()) );
  158. CALL_SUBTEST_2( map_class_vector(VectorXd(13)) );
  159. CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
  160. CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
  161. CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
  162. CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
  163. CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
  164. CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) );
  165. CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
  166. CALL_SUBTEST_11( map_class_matrix(Matrix<float,3,5>()) );
  167. CALL_SUBTEST_4( map_class_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
  168. CALL_SUBTEST_5( map_class_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
  169. CALL_SUBTEST_6( map_static_methods(Matrix<double, 1, 1>()) );
  170. CALL_SUBTEST_7( map_static_methods(Vector3f()) );
  171. CALL_SUBTEST_8( map_static_methods(RowVector3d()) );
  172. CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) );
  173. CALL_SUBTEST_10( map_static_methods(VectorXf(12)) );
  174. CALL_SUBTEST_11( map_not_aligned_on_scalar<double>() );
  175. }
  176. }