product_threshold.cpp
Go to the documentation of this file.
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>
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";
80  std::cout << "\n";
81 
83  std::cout << "\n\n";
84 
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";
100  std::cout << "\n";
101 
102  std::cout << M << "f\t";
104  std::cout << "\n";
105 
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 
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  {
133  }
134 };
135 
136 template<> struct print_n<END> { static void run(){} };
137 
138 int main()
139 {
141 
142  return 0;
143 }
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
loop_on_k
Definition: product_threshold.cpp:74
Eigen::internal::Lhs
@ Lhs
Definition: TensorContractionMapper.h:19
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::CoeffBasedProductMode
@ CoeffBasedProductMode
Definition: Constants.h:500
Eigen::GemvProduct
@ GemvProduct
Definition: Constants.h:500
END
#define END
Definition: product_threshold.cpp:9
loop_on_m::run
static void run()
Definition: product_threshold.cpp:96
loop_on_k< M, N, END >::run
static void run()
Definition: product_threshold.cpp:90
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
loop_on_n::run
static void run()
Definition: product_threshold.cpp:116
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
b
Scalar * b
Definition: benchVecAdd.cpp:17
ret
DenseIndex ret
Definition: level1_cplx_impl.h:44
Eigen::LazyCoeffBasedProductMode
@ LazyCoeffBasedProductMode
Definition: Constants.h:500
loop_on_m< END, N, K >::run
static void run()
Definition: product_threshold.cpp:111
n
int n
Definition: BiCGSTAB_simple.cpp:1
map_size
Definition: product_threshold.cpp:11
loop_on_n< M, END, K, Scalar, Mode >::run
static void run()
Definition: product_threshold.cpp:125
Eigen::Architecture::Type
Type
Definition: Constants.h:471
Eigen::BenchTimer
Definition: BenchTimer.h:59
BENCH
#define BENCH(TIMER, TRIES, REP, CODE)
Definition: BenchTimer.h:174
bench_prod
EIGEN_DONT_INLINE void bench_prod()
Definition: product_threshold.cpp:45
Eigen::GemmProduct
@ GemmProduct
Definition: Constants.h:500
print_n
Definition: product_threshold.cpp:69
loop_on_m
Definition: product_threshold.cpp:70
Eigen::InnerProduct
@ InnerProduct
Definition: Constants.h:500
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
K
#define K
Definition: igam.h:8
Eigen::internal::Rhs
@ Rhs
Definition: TensorContractionMapper.h:18
alt_prod
Definition: product_threshold.cpp:17
print_n< END >::run
static void run()
Definition: product_threshold.cpp:136
std
Definition: BFloat16.h:88
print_mode
void print_mode(int mode)
Definition: product_threshold.cpp:28
BenchTimer.h
print_n::run
static void run()
Definition: product_threshold.cpp:129
loop_on_n
Definition: product_threshold.cpp:71
mode
static const DiscreteKey mode(modeKey, 2)
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
N
#define N
Definition: igam.h:9
loop_on_k::run
static void run()
Definition: product_threshold.cpp:76
align_3::t
Point2 t(10, 10)
Eigen::OuterProduct
@ OuterProduct
Definition: Constants.h:500
main
int main()
Definition: product_threshold.cpp:138
Eigen::prod
const Product< Lhs, Rhs > prod(const Lhs &lhs, const Rhs &rhs)
Definition: evaluators.cpp:8
S
DiscreteKey S(1, 2)
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_DONT_INLINE
#define EIGEN_DONT_INLINE
Definition: Macros.h:940
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:04:30