vectorization_logic.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #define EIGEN_DEBUG_ASSIGN
00026 #include "main.h"
00027 #include <typeinfo>
00028 
00029 std::string demangle_traversal(int t)
00030 {
00031   if(t==DefaultTraversal) return "DefaultTraversal";
00032   if(t==LinearTraversal) return "LinearTraversal";
00033   if(t==InnerVectorizedTraversal) return "InnerVectorizedTraversal";
00034   if(t==LinearVectorizedTraversal) return "LinearVectorizedTraversal";
00035   if(t==SliceVectorizedTraversal) return "SliceVectorizedTraversal";
00036   return "?";
00037 }
00038 std::string demangle_unrolling(int t)
00039 {
00040   if(t==NoUnrolling) return "NoUnrolling";
00041   if(t==InnerUnrolling) return "InnerUnrolling";
00042   if(t==CompleteUnrolling) return "CompleteUnrolling";
00043   return "?";
00044 }
00045 
00046 template<typename Dst, typename Src>
00047 bool test_assign(const Dst&, const Src&, int traversal, int unrolling)
00048 {
00049   internal::assign_traits<Dst,Src>::debug();
00050   bool res = internal::assign_traits<Dst,Src>::Traversal==traversal
00051           && internal::assign_traits<Dst,Src>::Unrolling==unrolling;
00052   if(!res)
00053   {
00054     std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
00055               << " got " << demangle_traversal(internal::assign_traits<Dst,Src>::Traversal) << "\n";
00056     std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
00057               << " got " << demangle_unrolling(internal::assign_traits<Dst,Src>::Unrolling) << "\n";
00058   }
00059   return res;
00060 }
00061 
00062 template<typename Dst, typename Src>
00063 bool test_assign(int traversal, int unrolling)
00064 {
00065   internal::assign_traits<Dst,Src>::debug();
00066   bool res = internal::assign_traits<Dst,Src>::Traversal==traversal
00067           && internal::assign_traits<Dst,Src>::Unrolling==unrolling;
00068   if(!res)
00069   {
00070     std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
00071               << " got " << demangle_traversal(internal::assign_traits<Dst,Src>::Traversal) << "\n";
00072     std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
00073               << " got " << demangle_unrolling(internal::assign_traits<Dst,Src>::Unrolling) << "\n";
00074   }
00075   return res;
00076 }
00077 
00078 template<typename Xpr>
00079 bool test_redux(const Xpr&, int traversal, int unrolling)
00080 {
00081   typedef internal::redux_traits<internal::scalar_sum_op<typename Xpr::Scalar>,Xpr> traits;
00082   bool res = traits::Traversal==traversal && traits::Unrolling==unrolling;
00083   if(!res)
00084   {
00085     std::cerr << " Expected Traversal == " << demangle_traversal(traversal)
00086               << " got " << demangle_traversal(traits::Traversal) << "\n";
00087     std::cerr << " Expected Unrolling == " << demangle_unrolling(unrolling)
00088               << " got " << demangle_unrolling(traits::Unrolling) << "\n";
00089   }
00090   return res;
00091 }
00092 
00093 template<typename Scalar, bool Enable = internal::packet_traits<Scalar>::Vectorizable> struct vectorization_logic
00094 {
00095   enum {
00096     PacketSize = internal::packet_traits<Scalar>::size
00097   };
00098   static void run()
00099   {
00100     
00101     typedef Matrix<Scalar,PacketSize,1> Vector1;
00102     typedef Matrix<Scalar,Dynamic,1> VectorX;
00103     typedef Matrix<Scalar,Dynamic,Dynamic> MatrixXX;
00104     typedef Matrix<Scalar,PacketSize,PacketSize> Matrix11;
00105     typedef Matrix<Scalar,2*PacketSize,2*PacketSize> Matrix22;
00106     typedef Matrix<Scalar,(Matrix11::Flags&RowMajorBit)?16:4*PacketSize,(Matrix11::Flags&RowMajorBit)?4*PacketSize:16> Matrix44;
00107     typedef Matrix<Scalar,(Matrix11::Flags&RowMajorBit)?16:4*PacketSize,(Matrix11::Flags&RowMajorBit)?4*PacketSize:16,DontAlign|EIGEN_DEFAULT_MATRIX_STORAGE_ORDER_OPTION> Matrix44u;
00108     typedef Matrix<Scalar,4*PacketSize,16,ColMajor> Matrix44c;
00109     typedef Matrix<Scalar,4*PacketSize,16,RowMajor> Matrix44r;
00110 
00111     typedef Matrix<Scalar,
00112         (PacketSize==8 ? 4 : PacketSize==4 ? 2 : PacketSize==2 ? 1 : /*PacketSize==1 ?*/ 1),
00113         (PacketSize==8 ? 2 : PacketSize==4 ? 2 : PacketSize==2 ? 2 : /*PacketSize==1 ?*/ 1)
00114       > Matrix1;
00115 
00116     typedef Matrix<Scalar,
00117         (PacketSize==8 ? 4 : PacketSize==4 ? 2 : PacketSize==2 ? 1 : /*PacketSize==1 ?*/ 1),
00118         (PacketSize==8 ? 2 : PacketSize==4 ? 2 : PacketSize==2 ? 2 : /*PacketSize==1 ?*/ 1),
00119       DontAlign|((Matrix1::Flags&RowMajorBit)?RowMajor:ColMajor)> Matrix1u;
00120 
00121     // this type is made such that it can only be vectorized when viewed as a linear 1D vector
00122     typedef Matrix<Scalar,
00123         (PacketSize==8 ? 4 : PacketSize==4 ? 6 : PacketSize==2 ? ((Matrix11::Flags&RowMajorBit)?2:3) : /*PacketSize==1 ?*/ 1),
00124         (PacketSize==8 ? 6 : PacketSize==4 ? 2 : PacketSize==2 ? ((Matrix11::Flags&RowMajorBit)?3:2) : /*PacketSize==1 ?*/ 3)
00125       > Matrix3;
00126     
00127     #if !EIGEN_GCC_AND_ARCH_DOESNT_WANT_STACK_ALIGNMENT
00128     VERIFY(test_assign(Vector1(),Vector1(),
00129       InnerVectorizedTraversal,CompleteUnrolling));
00130     VERIFY(test_assign(Vector1(),Vector1()+Vector1(),
00131       InnerVectorizedTraversal,CompleteUnrolling));
00132     VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()),
00133       InnerVectorizedTraversal,CompleteUnrolling));
00134     VERIFY(test_assign(Vector1(),Vector1().template cast<Scalar>(),
00135       InnerVectorizedTraversal,CompleteUnrolling));
00136 
00137 
00138     VERIFY(test_assign(Vector1(),Vector1(),
00139       InnerVectorizedTraversal,CompleteUnrolling));
00140     VERIFY(test_assign(Vector1(),Vector1()+Vector1(),
00141       InnerVectorizedTraversal,CompleteUnrolling));
00142     VERIFY(test_assign(Vector1(),Vector1().cwiseProduct(Vector1()),
00143       InnerVectorizedTraversal,CompleteUnrolling));
00144 
00145     VERIFY(test_assign(Matrix44(),Matrix44()+Matrix44(),
00146       InnerVectorizedTraversal,InnerUnrolling));
00147 
00148     VERIFY(test_assign(Matrix44u(),Matrix44()+Matrix44(),
00149       LinearTraversal,NoUnrolling));
00150 
00151     VERIFY(test_assign(Matrix1u(),Matrix1()+Matrix1(),
00152       LinearTraversal,CompleteUnrolling));
00153 
00154     VERIFY(test_assign(Matrix44c().col(1),Matrix44c().col(2)+Matrix44c().col(3),
00155       InnerVectorizedTraversal,CompleteUnrolling));
00156     
00157     VERIFY(test_assign(Matrix44r().row(2),Matrix44r().row(1)+Matrix44r().row(1),
00158       InnerVectorizedTraversal,CompleteUnrolling));
00159         
00160     if(PacketSize>1)
00161     {
00162       typedef Matrix<Scalar,3,3,ColMajor> Matrix33c;
00163       VERIFY(test_assign(Matrix33c().row(2),Matrix33c().row(1)+Matrix33c().row(1),
00164         LinearTraversal,CompleteUnrolling));
00165       VERIFY(test_assign(Matrix33c().col(0),Matrix33c().col(1)+Matrix33c().col(1),
00166         LinearTraversal,CompleteUnrolling));
00167       
00168       VERIFY(test_assign(Matrix3(),Matrix3().cwiseQuotient(Matrix3()),
00169         LinearVectorizedTraversal,CompleteUnrolling));
00170 
00171       VERIFY(test_assign(Matrix<Scalar,17,17>(),Matrix<Scalar,17,17>()+Matrix<Scalar,17,17>(),
00172         LinearTraversal,NoUnrolling));
00173 
00174       VERIFY(test_assign(Matrix11(),Matrix<Scalar,17,17>().template block<PacketSize,PacketSize>(2,3)+Matrix<Scalar,17,17>().template block<PacketSize,PacketSize>(10,4),
00175       DefaultTraversal,CompleteUnrolling));
00176     }
00177     
00178     VERIFY(test_redux(Matrix3(),
00179       LinearVectorizedTraversal,CompleteUnrolling));
00180 
00181     VERIFY(test_redux(Matrix44(),
00182       LinearVectorizedTraversal,NoUnrolling));
00183 
00184     VERIFY(test_redux(Matrix44().template block<(Matrix1::Flags&RowMajorBit)?4:PacketSize,(Matrix1::Flags&RowMajorBit)?PacketSize:4>(1,2),
00185       DefaultTraversal,CompleteUnrolling));
00186 
00187     VERIFY(test_redux(Matrix44c().template block<2*PacketSize,1>(1,2),
00188       LinearVectorizedTraversal,CompleteUnrolling));
00189 
00190     VERIFY(test_redux(Matrix44r().template block<1,2*PacketSize>(2,1),
00191       LinearVectorizedTraversal,CompleteUnrolling));
00192     
00193     VERIFY((test_assign<
00194             Map<Matrix22, Aligned, OuterStride<3*PacketSize> >,
00195             Matrix22
00196             >(InnerVectorizedTraversal,CompleteUnrolling)));
00197 
00198     VERIFY((test_assign<
00199             Map<Matrix22, Aligned, InnerStride<3*PacketSize> >,
00200             Matrix22
00201             >(DefaultTraversal,CompleteUnrolling)));
00202 
00203     VERIFY((test_assign(Matrix11(), Matrix11()*Matrix11(), InnerVectorizedTraversal, CompleteUnrolling)));
00204     #endif
00205 
00206     VERIFY(test_assign(MatrixXX(10,10),MatrixXX(20,20).block(10,10,2,3),
00207       SliceVectorizedTraversal,NoUnrolling));
00208 
00209     VERIFY(test_redux(VectorX(10),
00210       LinearVectorizedTraversal,NoUnrolling));
00211 
00212     
00213   }
00214 };
00215 
00216 template<typename Scalar> struct vectorization_logic<Scalar,false>
00217 {
00218   static void run() {}
00219 };
00220 
00221 void test_vectorization_logic()
00222 {
00223 
00224 #ifdef EIGEN_VECTORIZE
00225 
00226   CALL_SUBTEST( vectorization_logic<float>::run() );
00227   CALL_SUBTEST( vectorization_logic<double>::run() );
00228   CALL_SUBTEST( vectorization_logic<std::complex<float> >::run() );
00229   CALL_SUBTEST( vectorization_logic<std::complex<double> >::run() );
00230   
00231   if(internal::packet_traits<float>::Vectorizable)
00232   {
00233     VERIFY(test_assign(Matrix<float,3,3>(),Matrix<float,3,3>()+Matrix<float,3,3>(),
00234       LinearTraversal,CompleteUnrolling));
00235       
00236     VERIFY(test_redux(Matrix<float,5,2>(),
00237       DefaultTraversal,CompleteUnrolling));
00238   }
00239   
00240   if(internal::packet_traits<double>::Vectorizable)
00241   {
00242     VERIFY(test_assign(Matrix<double,3,3>(),Matrix<double,3,3>()+Matrix<double,3,3>(),
00243       LinearTraversal,CompleteUnrolling));
00244     
00245     VERIFY(test_redux(Matrix<double,7,3>(),
00246       DefaultTraversal,CompleteUnrolling));
00247   }
00248 #endif // EIGEN_VECTORIZE
00249 
00250 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:40