00001 00002 #include <iostream> 00003 #include <Eigen/Core> 00004 #include <bench/BenchTimer.h> 00005 00006 using namespace Eigen; 00007 using namespace std; 00008 00009 #define END 9 00010 00011 template<int S> struct map_size { enum { ret = S }; }; 00012 template<> struct map_size<10> { enum { ret = 20 }; }; 00013 template<> struct map_size<11> { enum { ret = 50 }; }; 00014 template<> struct map_size<12> { enum { ret = 100 }; }; 00015 template<> struct map_size<13> { enum { ret = 300 }; }; 00016 00017 template<int M, int N,int K> struct alt_prod 00018 { 00019 enum { 00020 ret = M==1 && N==1 ? InnerProduct 00021 : K==1 ? OuterProduct 00022 : M==1 ? GemvProduct 00023 : N==1 ? GemvProduct 00024 : GemmProduct 00025 }; 00026 }; 00027 00028 void print_mode(int mode) 00029 { 00030 if(mode==InnerProduct) std::cout << "i"; 00031 if(mode==OuterProduct) std::cout << "o"; 00032 if(mode==CoeffBasedProductMode) std::cout << "c"; 00033 if(mode==LazyCoeffBasedProductMode) std::cout << "l"; 00034 if(mode==GemvProduct) std::cout << "v"; 00035 if(mode==GemmProduct) std::cout << "m"; 00036 } 00037 00038 template<int Mode, typename Lhs, typename Rhs, typename Res> 00039 EIGEN_DONT_INLINE void prod(const Lhs& a, const Rhs& b, Res& c) 00040 { 00041 c.noalias() += typename ProductReturnType<Lhs,Rhs,Mode>::Type(a,b); 00042 } 00043 00044 template<int M, int N, int K, typename Scalar, int Mode> 00045 EIGEN_DONT_INLINE void bench_prod() 00046 { 00047 typedef Matrix<Scalar,M,K> Lhs; Lhs a; a.setRandom(); 00048 typedef Matrix<Scalar,K,N> Rhs; Rhs b; b.setRandom(); 00049 typedef Matrix<Scalar,M,N> Res; Res c; c.setRandom(); 00050 00051 BenchTimer t; 00052 double n = 2.*double(M)*double(N)*double(K); 00053 int rep = 100000./n; 00054 rep /= 2; 00055 if(rep<1) rep = 1; 00056 do { 00057 rep *= 2; 00058 t.reset(); 00059 BENCH(t,1,rep,prod<CoeffBasedProductMode>(a,b,c)); 00060 } while(t.best()<0.1); 00061 00062 t.reset(); 00063 BENCH(t,5,rep,prod<Mode>(a,b,c)); 00064 00065 print_mode(Mode); 00066 std::cout << int(1e-6*n*rep/t.best()) << "\t"; 00067 } 00068 00069 template<int N> struct print_n; 00070 template<int M, int N, int K> struct loop_on_m; 00071 template<int M, int N, int K, typename Scalar, int Mode> struct loop_on_n; 00072 00073 template<int M, int N, int K> 00074 struct loop_on_k 00075 { 00076 static void run() 00077 { 00078 std::cout << "K=" << K << "\t"; 00079 print_n<N>::run(); 00080 std::cout << "\n"; 00081 00082 loop_on_m<M,N,K>::run(); 00083 std::cout << "\n\n"; 00084 00085 loop_on_k<M,N,K+1>::run(); 00086 } 00087 }; 00088 00089 template<int M, int N> 00090 struct loop_on_k<M,N,END> { static void run(){} }; 00091 00092 00093 template<int M, int N, int K> 00094 struct loop_on_m 00095 { 00096 static void run() 00097 { 00098 std::cout << M << "f\t"; 00099 loop_on_n<M,N,K,float,CoeffBasedProductMode>::run(); 00100 std::cout << "\n"; 00101 00102 std::cout << M << "f\t"; 00103 loop_on_n<M,N,K,float,-1>::run(); 00104 std::cout << "\n"; 00105 00106 loop_on_m<M+1,N,K>::run(); 00107 } 00108 }; 00109 00110 template<int N, int K> 00111 struct loop_on_m<END,N,K> { static void run(){} }; 00112 00113 template<int M, int N, int K, typename Scalar, int Mode> 00114 struct loop_on_n 00115 { 00116 static void run() 00117 { 00118 bench_prod<M,N,K,Scalar,Mode==-1? alt_prod<M,N,K>::ret : Mode>(); 00119 00120 loop_on_n<M,N+1,K,Scalar,Mode>::run(); 00121 } 00122 }; 00123 00124 template<int M, int K, typename Scalar, int Mode> 00125 struct loop_on_n<M,END,K,Scalar,Mode> { static void run(){} }; 00126 00127 template<int N> struct print_n 00128 { 00129 static void run() 00130 { 00131 std::cout << map_size<N>::ret << "\t"; 00132 print_n<N+1>::run(); 00133 } 00134 }; 00135 00136 template<> struct print_n<END> { static void run(){} }; 00137 00138 int main() 00139 { 00140 loop_on_k<1,1,1>::run(); 00141 00142 return 0; 00143 }