[136] | 1 |
|
---|
| 2 | #include <iostream>
|
---|
| 3 | #include <Eigen/Core>
|
---|
| 4 | #include <bench/BenchTimer.h>
|
---|
| 5 |
|
---|
| 6 | using namespace Eigen;
|
---|
| 7 | using namespace std;
|
---|
| 8 |
|
---|
| 9 | #define END 9
|
---|
| 10 |
|
---|
| 11 | template<int S> struct map_size { enum { ret = S }; };
|
---|
| 12 | template<> struct map_size<10> { enum { ret = 20 }; };
|
---|
| 13 | template<> struct map_size<11> { enum { ret = 50 }; };
|
---|
| 14 | template<> struct map_size<12> { enum { ret = 100 }; };
|
---|
| 15 | template<> struct map_size<13> { enum { ret = 300 }; };
|
---|
| 16 |
|
---|
| 17 | template<int M, int N,int K> struct alt_prod
|
---|
| 18 | {
|
---|
| 19 | enum {
|
---|
| 20 | ret = M==1 && N==1 ? InnerProduct
|
---|
| 21 | : K==1 ? OuterProduct
|
---|
| 22 | : M==1 ? GemvProduct
|
---|
| 23 | : N==1 ? GemvProduct
|
---|
| 24 | : GemmProduct
|
---|
| 25 | };
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | void print_mode(int mode)
|
---|
| 29 | {
|
---|
| 30 | if(mode==InnerProduct) std::cout << "i";
|
---|
| 31 | if(mode==OuterProduct) std::cout << "o";
|
---|
| 32 | if(mode==CoeffBasedProductMode) std::cout << "c";
|
---|
| 33 | if(mode==LazyCoeffBasedProductMode) std::cout << "l";
|
---|
| 34 | if(mode==GemvProduct) std::cout << "v";
|
---|
| 35 | if(mode==GemmProduct) std::cout << "m";
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | template<int Mode, typename Lhs, typename Rhs, typename Res>
|
---|
| 39 | EIGEN_DONT_INLINE void prod(const Lhs& a, const Rhs& b, Res& c)
|
---|
| 40 | {
|
---|
| 41 | c.noalias() += typename ProductReturnType<Lhs,Rhs,Mode>::Type(a,b);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | template<int M, int N, int K, typename Scalar, int Mode>
|
---|
| 45 | EIGEN_DONT_INLINE void bench_prod()
|
---|
| 46 | {
|
---|
| 47 | typedef Matrix<Scalar,M,K> Lhs; Lhs a; a.setRandom();
|
---|
| 48 | typedef Matrix<Scalar,K,N> Rhs; Rhs b; b.setRandom();
|
---|
| 49 | typedef Matrix<Scalar,M,N> Res; Res c; c.setRandom();
|
---|
| 50 |
|
---|
| 51 | BenchTimer t;
|
---|
| 52 | double n = 2.*double(M)*double(N)*double(K);
|
---|
| 53 | int rep = 100000./n;
|
---|
| 54 | rep /= 2;
|
---|
| 55 | if(rep<1) rep = 1;
|
---|
| 56 | do {
|
---|
| 57 | rep *= 2;
|
---|
| 58 | t.reset();
|
---|
| 59 | BENCH(t,1,rep,prod<CoeffBasedProductMode>(a,b,c));
|
---|
| 60 | } while(t.best()<0.1);
|
---|
| 61 |
|
---|
| 62 | t.reset();
|
---|
| 63 | BENCH(t,5,rep,prod<Mode>(a,b,c));
|
---|
| 64 |
|
---|
| 65 | print_mode(Mode);
|
---|
| 66 | std::cout << int(1e-6*n*rep/t.best()) << "\t";
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | template<int N> struct print_n;
|
---|
| 70 | template<int M, int N, int K> struct loop_on_m;
|
---|
| 71 | template<int M, int N, int K, typename Scalar, int Mode> struct loop_on_n;
|
---|
| 72 |
|
---|
| 73 | template<int M, int N, int K>
|
---|
| 74 | struct loop_on_k
|
---|
| 75 | {
|
---|
| 76 | static void run()
|
---|
| 77 | {
|
---|
| 78 | std::cout << "K=" << K << "\t";
|
---|
| 79 | print_n<N>::run();
|
---|
| 80 | std::cout << "\n";
|
---|
| 81 |
|
---|
| 82 | loop_on_m<M,N,K>::run();
|
---|
| 83 | std::cout << "\n\n";
|
---|
| 84 |
|
---|
| 85 | loop_on_k<M,N,K+1>::run();
|
---|
| 86 | }
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | template<int M, int N>
|
---|
| 90 | struct loop_on_k<M,N,END> { static void run(){} };
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | template<int M, int N, int K>
|
---|
| 94 | struct loop_on_m
|
---|
| 95 | {
|
---|
| 96 | static void run()
|
---|
| 97 | {
|
---|
| 98 | std::cout << M << "f\t";
|
---|
| 99 | loop_on_n<M,N,K,float,CoeffBasedProductMode>::run();
|
---|
| 100 | std::cout << "\n";
|
---|
| 101 |
|
---|
| 102 | std::cout << M << "f\t";
|
---|
| 103 | loop_on_n<M,N,K,float,-1>::run();
|
---|
| 104 | std::cout << "\n";
|
---|
| 105 |
|
---|
| 106 | loop_on_m<M+1,N,K>::run();
|
---|
| 107 | }
|
---|
| 108 | };
|
---|
| 109 |
|
---|
| 110 | template<int N, int K>
|
---|
| 111 | struct loop_on_m<END,N,K> { static void run(){} };
|
---|
| 112 |
|
---|
| 113 | template<int M, int N, int K, typename Scalar, int Mode>
|
---|
| 114 | struct loop_on_n
|
---|
| 115 | {
|
---|
| 116 | static void run()
|
---|
| 117 | {
|
---|
| 118 | bench_prod<M,N,K,Scalar,Mode==-1? alt_prod<M,N,K>::ret : Mode>();
|
---|
| 119 |
|
---|
| 120 | loop_on_n<M,N+1,K,Scalar,Mode>::run();
|
---|
| 121 | }
|
---|
| 122 | };
|
---|
| 123 |
|
---|
| 124 | template<int M, int K, typename Scalar, int Mode>
|
---|
| 125 | struct loop_on_n<M,END,K,Scalar,Mode> { static void run(){} };
|
---|
| 126 |
|
---|
| 127 | template<int N> struct print_n
|
---|
| 128 | {
|
---|
| 129 | static void run()
|
---|
| 130 | {
|
---|
| 131 | std::cout << map_size<N>::ret << "\t";
|
---|
| 132 | print_n<N+1>::run();
|
---|
| 133 | }
|
---|
| 134 | };
|
---|
| 135 |
|
---|
| 136 | template<> struct print_n<END> { static void run(){} };
|
---|
| 137 |
|
---|
| 138 | int main()
|
---|
| 139 | {
|
---|
| 140 | loop_on_k<1,1,1>::run();
|
---|
| 141 |
|
---|
| 142 | return 0;
|
---|
| 143 | }
|
---|