aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/product_threshold.cpp
blob: dd6d15a07105d5f596ed6c01900a7d2ac900ac3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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; 
}