aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2010-03-05 17:16:19 +0100
committerGravatar Gael Guennebaud <g.gael@free.fr>2010-03-05 17:16:19 +0100
commitf2a246c2258c400993a1bf52999a4b8e29bf4ff1 (patch)
tree0e731a1995672a82d632cd6bc8c3f738dd2fd145
parentc44220835864d2994a2a2b030100125fa00aa378 (diff)
add a small program to bench all combinations of small products
-rw-r--r--Eigen/src/Core/Product.h46
-rw-r--r--bench/product_threshold.cpp143
2 files changed, 166 insertions, 23 deletions
diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h
index 865387b11..f814382a9 100644
--- a/Eigen/src/Core/Product.h
+++ b/Eigen/src/Core/Product.h
@@ -84,28 +84,28 @@ public:
* based on the three dimensions of the product.
* This is a compile time mapping from {1,Small,Large}^3 -> {product types} */
// FIXME I'm not sure the current mapping is the ideal one.
-template<int Rows, int Cols> struct ei_product_type_selector<Rows, Cols, 1> { enum { ret = OuterProduct }; };
-template<int Depth> struct ei_product_type_selector<1, 1, Depth> { enum { ret = InnerProduct }; };
-template<> struct ei_product_type_selector<1, 1, 1> { enum { ret = InnerProduct }; };
-template<> struct ei_product_type_selector<Small,1, Small> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<1, Small,Small> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Small,Small,Small> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Small, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Small, Large, 1> { enum { ret = LazyCoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Large, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<1, Large,Small> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<1, Large,Large> { enum { ret = GemvProduct }; };
-template<> struct ei_product_type_selector<1, Small,Large> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Large,1, Small> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Large,1, Large> { enum { ret = GemvProduct }; };
-template<> struct ei_product_type_selector<Small,1, Large> { enum { ret = CoeffBasedProductMode }; };
-template<> struct ei_product_type_selector<Small,Small,Large> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Large,Small,Large> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Small,Large,Large> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Large,Large,Large> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Large,Small,Small> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Small,Large,Small> { enum { ret = GemmProduct }; };
-template<> struct ei_product_type_selector<Large,Large,Small> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Large,Large,1> { enum { ret = OuterProduct }; };
+template<int Depth> struct ei_product_type_selector<1, 1, Depth> { enum { ret = InnerProduct }; };
+template<> struct ei_product_type_selector<1, 1, 1> { enum { ret = InnerProduct }; };
+template<> struct ei_product_type_selector<Small,1, Small> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<1, Small,Small> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Small,Small,Small> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Small, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Small, Large, 1> { enum { ret = LazyCoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Large, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<1, Large,Small> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<1, Large,Large> { enum { ret = GemvProduct }; };
+template<> struct ei_product_type_selector<1, Small,Large> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Large,1, Small> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Large,1, Large> { enum { ret = GemvProduct }; };
+template<> struct ei_product_type_selector<Small,1, Large> { enum { ret = CoeffBasedProductMode }; };
+template<> struct ei_product_type_selector<Small,Small,Large> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Large,Small,Large> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Small,Large,Large> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Large,Large,Large> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Large,Small,Small> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Small,Large,Small> { enum { ret = GemmProduct }; };
+template<> struct ei_product_type_selector<Large,Large,Small> { enum { ret = GemmProduct }; };
/** \class ProductReturnType
*
@@ -298,7 +298,7 @@ struct ei_gemv_selector<OnTheLeft,StorageOrder,BlasCompatible>
{
Transpose<Dest> destT(dest);
ei_gemv_selector<OnTheRight,!StorageOrder,BlasCompatible>
- ::run(GeneralProduct<Transpose<typename ProductType::_RhsNested>,Transpose<typename ProductType::_LhsNested> >
+ ::run(GeneralProduct<Transpose<typename ProductType::_RhsNested>,Transpose<typename ProductType::_LhsNested>, GemvProduct>
(prod.rhs().transpose(), prod.lhs().transpose()), destT, alpha);
}
};
diff --git a/bench/product_threshold.cpp b/bench/product_threshold.cpp
new file mode 100644
index 000000000..9e73731e8
--- /dev/null
+++ b/bench/product_threshold.cpp
@@ -0,0 +1,143 @@
+
+#include <iostream>
+#include <Eigen/Core>
+#include <bench/BenchTimer.h>
+
+using namespace Eigen;
+using namespace std;
+
+#define END 9
+
+template<int S> struct map_size { enum { ret = S }; };
+template<> struct map_size<10> { enum { ret = 20 }; };
+template<> struct map_size<11> { enum { ret = 50 }; };
+template<> struct map_size<12> { enum { ret = 100 }; };
+template<> struct map_size<13> { enum { ret = 300 }; };
+
+template<int M, int N,int K> struct alt_prod
+{
+ enum {
+ ret = M==1 && N==1 ? InnerProduct
+ : K==1 ? OuterProduct
+ : M==1 ? GemvProduct
+ : N==1 ? GemvProduct
+ : GemmProduct
+ };
+};
+
+void print_mode(int mode)
+{
+ if(mode==InnerProduct) std::cout << "i";
+ if(mode==OuterProduct) std::cout << "o";
+ if(mode==CoeffBasedProductMode) std::cout << "c";
+ if(mode==LazyCoeffBasedProductMode) std::cout << "l";
+ if(mode==GemvProduct) std::cout << "v";
+ if(mode==GemmProduct) std::cout << "m";
+}
+
+template<int Mode, typename Lhs, typename Rhs, typename Res>
+EIGEN_DONT_INLINE void prod(const Lhs& a, const Rhs& b, Res& c)
+{
+ c.noalias() += typename ProductReturnType<Lhs,Rhs,Mode>::Type(a,b);
+}
+
+template<int M, int N, int K, typename Scalar, int Mode>
+EIGEN_DONT_INLINE void bench_prod()
+{
+ typedef Matrix<Scalar,M,K> Lhs; Lhs a; a.setRandom();
+ typedef Matrix<Scalar,K,N> Rhs; Rhs b; b.setRandom();
+ typedef Matrix<Scalar,M,N> Res; Res c; c.setRandom();
+
+ BenchTimer t;
+ double n = 2.*double(M)*double(N)*double(K);
+ int rep = 100000./n;
+ rep /= 2;
+ if(rep<1) rep = 1;
+ do {
+ rep *= 2;
+ t.reset();
+ BENCH(t,1,rep,prod<CoeffBasedProductMode>(a,b,c));
+ } while(t.best()<0.1);
+
+ t.reset();
+ BENCH(t,5,rep,prod<Mode>(a,b,c));
+
+ print_mode(Mode);
+ std::cout << int(1e-6*n*rep/t.best()) << "\t";
+}
+
+template<int N> struct print_n;
+template<int M, int N, int K> struct loop_on_m;
+template<int M, int N, int K, typename Scalar, int Mode> struct loop_on_n;
+
+template<int M, int N, int K>
+struct loop_on_k
+{
+ static void run()
+ {
+ std::cout << "K=" << K << "\t";
+ print_n<N>::run();
+ std::cout << "\n";
+
+ loop_on_m<M,N,K>::run();
+ std::cout << "\n\n";
+
+ loop_on_k<M,N,K+1>::run();
+ }
+};
+
+template<int M, int N>
+struct loop_on_k<M,N,END> { static void run(){} };
+
+
+template<int M, int N, int K>
+struct loop_on_m
+{
+ static void run()
+ {
+ std::cout << M << "f\t";
+ loop_on_n<M,N,K,float,CoeffBasedProductMode>::run();
+ std::cout << "\n";
+
+ std::cout << M << "f\t";
+ loop_on_n<M,N,K,float,-1>::run();
+ std::cout << "\n";
+
+ loop_on_m<M+1,N,K>::run();
+ }
+};
+
+template<int N, int K>
+struct loop_on_m<END,N,K> { static void run(){} };
+
+template<int M, int N, int K, typename Scalar, int Mode>
+struct loop_on_n
+{
+ static void run()
+ {
+ bench_prod<M,N,K,Scalar,Mode==-1? alt_prod<M,N,K>::ret : Mode>();
+
+ loop_on_n<M,N+1,K,Scalar,Mode>::run();
+ }
+};
+
+template<int M, int K, typename Scalar, int Mode>
+struct loop_on_n<M,END,K,Scalar,Mode> { static void run(){} };
+
+template<int N> struct print_n
+{
+ static void run()
+ {
+ std::cout << map_size<N>::ret << "\t";
+ print_n<N+1>::run();
+ }
+};
+
+template<> struct print_n<END> { static void run(){} };
+
+int main()
+{
+ loop_on_k<1,1,1>::run();
+
+ return 0;
+}