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.

235 lines
9.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #define EIGEN_DEBUG_ASSIGN
  10. #include "main.h"
  11. #include <typeinfo>
  12. std::string demangle_traversal(int t)
  13. {
  14. if(t==DefaultTraversal) return "DefaultTraversal";
  15. if(t==LinearTraversal) return "LinearTraversal";
  16. if(t==InnerVectorizedTraversal) return "InnerVectorizedTraversal";
  17. if(t==LinearVectorizedTraversal) return "LinearVectorizedTraversal";
  18. if(t==SliceVectorizedTraversal) return "SliceVectorizedTraversal";
  19. return "?";
  20. }
  21. std::string demangle_unrolling(int t)
  22. {
  23. if(t==NoUnrolling) return "NoUnrolling";
  24. if(t==InnerUnrolling) return "InnerUnrolling";
  25. if(t==CompleteUnrolling) return "CompleteUnrolling";
  26. return "?";
  27. }
  28. template<typename Dst, typename Src>
  29. bool test_assign(const Dst&, const Src&, int traversal, int unrolling)
  30. {
  31. internal::assign_traits<Dst,Src>::debug();
  32. bool res = internal::assign_traits<Dst,Src>::Traversal==traversal
  33. && internal::assign_traits<Dst,Src>::Unrolling==unrolling;
  34. if(!res)
  35. {
  36. std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
  37. << " got " << demangle_traversal(internal::assign_traits<Dst,Src>::Traversal) << "\n";
  38. std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
  39. << " got " << demangle_unrolling(internal::assign_traits<Dst,Src>::Unrolling) << "\n";
  40. }
  41. return res;
  42. }
  43. template<typename Dst, typename Src>
  44. bool test_assign(int traversal, int unrolling)
  45. {
  46. internal::assign_traits<Dst,Src>::debug();
  47. bool res = internal::assign_traits<Dst,Src>::Traversal==traversal
  48. && internal::assign_traits<Dst,Src>::Unrolling==unrolling;
  49. if(!res)
  50. {
  51. std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
  52. << " got " << demangle_traversal(internal::assign_traits<Dst,Src>::Traversal) << "\n";
  53. std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
  54. << " got " << demangle_unrolling(internal::assign_traits<Dst,Src>::Unrolling) << "\n";
  55. }
  56. return res;
  57. }
  58. template<typename Xpr>
  59. bool test_redux(const Xpr&, int traversal, int unrolling)
  60. {
  61. typedef internal::redux_traits<internal::scalar_sum_op<typename Xpr::Scalar>,Xpr> traits;
  62. bool res = traits::Traversal==traversal && traits::Unrolling==unrolling;
  63. if(!res)
  64. {
  65. std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
  66. << " got " << demangle_traversal(traits::Traversal) << "\n";
  67. std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
  68. << " got " << demangle_unrolling(traits::Unrolling) << "\n";
  69. }
  70. return res;
  71. }
  72. template<typename Scalar, bool Enable = internal::packet_traits<Scalar>::Vectorizable> struct vectorization_logic
  73. {
  74. enum {
  75. PacketSize = internal::packet_traits<Scalar>::size
  76. };
  77. static void run()
  78. {
  79. typedef Matrix<Scalar,PacketSize,1> Vector1;
  80. typedef Matrix<Scalar,Dynamic,1> VectorX;
  81. typedef Matrix<Scalar,Dynamic,Dynamic> MatrixXX;
  82. typedef Matrix<Scalar,PacketSize,PacketSize> Matrix11;
  83. typedef Matrix<Scalar,2*PacketSize,2*PacketSize> Matrix22;
  84. typedef Matrix<Scalar,(Matrix11::Flags&RowMajorBit)?16:4*PacketSize,(Matrix11::Flags&RowMajorBit)?4*PacketSize:16> Matrix44;
  85. typedef Matrix<Scalar,(Matrix11::Flags&RowMajorBit)?16:4*PacketSize,(Matrix11::Flags&RowMajorBit)?4*PacketSize:16,DontAlign|EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION> Matrix44u;
  86. typedef Matrix<Scalar,4*PacketSize,16,ColMajor> Matrix44c;
  87. typedef Matrix<Scalar,4*PacketSize,16,RowMajor> Matrix44r;
  88. typedef Matrix<Scalar,
  89. (PacketSize==8 ? 4 : PacketSize==4 ? 2 : PacketSize==2 ? 1 : /*PacketSize==1 ?*/ 1),
  90. (PacketSize==8 ? 2 : PacketSize==4 ? 2 : PacketSize==2 ? 2 : /*PacketSize==1 ?*/ 1)
  91. > Matrix1;
  92. typedef Matrix<Scalar,
  93. (PacketSize==8 ? 4 : PacketSize==4 ? 2 : PacketSize==2 ? 1 : /*PacketSize==1 ?*/ 1),
  94. (PacketSize==8 ? 2 : PacketSize==4 ? 2 : PacketSize==2 ? 2 : /*PacketSize==1 ?*/ 1),
  95. DontAlign|((Matrix1::Flags&RowMajorBit)?RowMajor:ColMajor)> Matrix1u;
  96. // this type is made such that it can only be vectorized when viewed as a linear 1D vector
  97. typedef Matrix<Scalar,
  98. (PacketSize==8 ? 4 : PacketSize==4 ? 6 : PacketSize==2 ? ((Matrix11::Flags&RowMajorBit)?2:3) : /*PacketSize==1 ?*/ 1),
  99. (PacketSize==8 ? 6 : PacketSize==4 ? 2 : PacketSize==2 ? ((Matrix11::Flags&RowMajorBit)?3:2) : /*PacketSize==1 ?*/ 3)
  100. > Matrix3;
  101. #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT
  102. VERIFY(test_assign(Vector1(),Vector1(),
  103. InnerVectorizedTraversal,CompleteUnrolling));
  104. VERIFY(test_assign(Vector1(),Vector1()+Vector1(),
  105. InnerVectorizedTraversal,CompleteUnrolling));
  106. VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()),
  107. InnerVectorizedTraversal,CompleteUnrolling));
  108. VERIFY(test_assign(Vector1(),Vector1().template cast<Scalar>(),
  109. InnerVectorizedTraversal,CompleteUnrolling));
  110. VERIFY(test_assign(Vector1(),Vector1(),
  111. InnerVectorizedTraversal,CompleteUnrolling));
  112. VERIFY(test_assign(Vector1(),Vector1()+Vector1(),
  113. InnerVectorizedTraversal,CompleteUnrolling));
  114. VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()),
  115. InnerVectorizedTraversal,CompleteUnrolling));
  116. VERIFY(test_assign(Matrix44(),Matrix44()+Matrix44(),
  117. InnerVectorizedTraversal,InnerUnrolling));
  118. VERIFY(test_assign(Matrix44u(),Matrix44()+Matrix44(),
  119. LinearTraversal,NoUnrolling));
  120. VERIFY(test_assign(Matrix1u(),Matrix1()+Matrix1(),
  121. LinearTraversal,CompleteUnrolling));
  122. VERIFY(test_assign(Matrix44c().col(1),Matrix44c().col(2)+Matrix44c().col(3),
  123. InnerVectorizedTraversal,CompleteUnrolling));
  124. VERIFY(test_assign(Matrix44r().row(2),Matrix44r().row(1)+Matrix44r().row(1),
  125. InnerVectorizedTraversal,CompleteUnrolling));
  126. if(PacketSize>1)
  127. {
  128. typedef Matrix<Scalar,3,3,ColMajor> Matrix33c;
  129. VERIFY(test_assign(Matrix33c().row(2),Matrix33c().row(1)+Matrix33c().row(1),
  130. LinearTraversal,CompleteUnrolling));
  131. VERIFY(test_assign(Matrix33c().col(0),Matrix33c().col(1)+Matrix33c().col(1),
  132. LinearTraversal,CompleteUnrolling));
  133. VERIFY(test_assign(Matrix3(),Matrix3().cwiseQuotient(Matrix3()),
  134. LinearVectorizedTraversal,CompleteUnrolling));
  135. VERIFY(test_assign(Matrix<Scalar,17,17>(),Matrix<Scalar,17,17>()+Matrix<Scalar,17,17>(),
  136. LinearTraversal,NoUnrolling));
  137. VERIFY(test_assign(Matrix11(),Matrix<Scalar,17,17>().template block<PacketSize,PacketSize>(2,3)+Matrix<Scalar,17,17>().template block<PacketSize,PacketSize>(10,4),
  138. DefaultTraversal,CompleteUnrolling));
  139. }
  140. VERIFY(test_redux(Matrix3(),
  141. LinearVectorizedTraversal,CompleteUnrolling));
  142. VERIFY(test_redux(Matrix44(),
  143. LinearVectorizedTraversal,NoUnrolling));
  144. VERIFY(test_redux(Matrix44().template block<(Matrix1::Flags&RowMajorBit)?4:PacketSize,(Matrix1::Flags&RowMajorBit)?PacketSize:4>(1,2),
  145. DefaultTraversal,CompleteUnrolling));
  146. VERIFY(test_redux(Matrix44c().template block<2*PacketSize,1>(1,2),
  147. LinearVectorizedTraversal,CompleteUnrolling));
  148. VERIFY(test_redux(Matrix44r().template block<1,2*PacketSize>(2,1),
  149. LinearVectorizedTraversal,CompleteUnrolling));
  150. VERIFY((test_assign<
  151. Map<Matrix22, Aligned, OuterStride<3*PacketSize> >,
  152. Matrix22
  153. >(InnerVectorizedTraversal,CompleteUnrolling)));
  154. VERIFY((test_assign<
  155. Map<Matrix22, Aligned, InnerStride<3*PacketSize> >,
  156. Matrix22
  157. >(DefaultTraversal,CompleteUnrolling)));
  158. VERIFY((test_assign(Matrix11(), Matrix11()*Matrix11(), InnerVectorizedTraversal, CompleteUnrolling)));
  159. #endif
  160. VERIFY(test_assign(MatrixXX(10,10),MatrixXX(20,20).block(10,10,2,3),
  161. SliceVectorizedTraversal,NoUnrolling));
  162. VERIFY(test_redux(VectorX(10),
  163. LinearVectorizedTraversal,NoUnrolling));
  164. }
  165. };
  166. template<typename Scalar> struct vectorization_logic<Scalar,false>
  167. {
  168. static void run() {}
  169. };
  170. void test_vectorization_logic()
  171. {
  172. #ifdef EIGEN_VECTORIZE
  173. CALL_SUBTEST( vectorization_logic<float>::run() );
  174. CALL_SUBTEST( vectorization_logic<double>::run() );
  175. CALL_SUBTEST( vectorization_logic<std::complex<float> >::run() );
  176. CALL_SUBTEST( vectorization_logic<std::complex<double> >::run() );
  177. if(internal::packet_traits<float>::Vectorizable)
  178. {
  179. VERIFY(test_assign(Matrix<float,3,3>(),Matrix<float,3,3>()+Matrix<float,3,3>(),
  180. LinearTraversal,CompleteUnrolling));
  181. VERIFY(test_redux(Matrix<float,5,2>(),
  182. DefaultTraversal,CompleteUnrolling));
  183. }
  184. if(internal::packet_traits<double>::Vectorizable)
  185. {
  186. VERIFY(test_assign(Matrix<double,3,3>(),Matrix<double,3,3>()+Matrix<double,3,3>(),
  187. LinearTraversal,CompleteUnrolling));
  188. VERIFY(test_redux(Matrix<double,7,3>(),
  189. DefaultTraversal,CompleteUnrolling));
  190. }
  191. #endif // EIGEN_VECTORIZE
  192. }