eigen/bench/perf_monitoring/gemm_common.h

62 lines
1.3 KiB
C
Raw Normal View History

2016-12-06 06:01:52 +08:00
#include <iostream>
#include <fstream>
#include <vector>
2016-12-07 23:56:08 +08:00
#include <string>
#include "eigen_src/Eigen/Core"
2017-01-03 18:30:27 +08:00
#include "../BenchTimer.h"
2016-12-06 06:01:52 +08:00
using namespace Eigen;
#ifndef SCALAR
#error SCALAR must be defined
#endif
typedef SCALAR Scalar;
typedef Matrix<Scalar, Dynamic, Dynamic> Mat;
2016-12-06 06:01:52 +08:00
template <typename Func>
EIGEN_DONT_INLINE double bench(long m, long n, long k, const Func& f) {
Mat A(m, k);
Mat B(k, n);
Mat C(m, n);
2016-12-06 06:01:52 +08:00
A.setRandom();
B.setRandom();
C.setZero();
2016-12-06 06:01:52 +08:00
BenchTimer t;
double up = 1e8 * 4 / sizeof(Scalar);
2016-12-06 06:01:52 +08:00
double tm0 = 4, tm1 = 10;
if (NumTraits<Scalar>::IsComplex) {
2016-12-06 06:01:52 +08:00
up /= 4;
tm0 = 2;
tm1 = 4;
}
2016-12-06 06:01:52 +08:00
double flops = 2. * m * n * k;
long rep = std::max(1., std::min(100., up / flops));
long tries = std::max(tm0, std::min(tm1, up / flops));
BENCH(t, tries, rep, f(A, B, C));
2016-12-06 06:01:52 +08:00
return 1e-9 * rep * flops / t.best();
}
template <typename Func>
int main_gemm(int argc, char** argv, const Func& f) {
2016-12-06 06:01:52 +08:00
std::vector<double> results;
2016-12-06 06:01:52 +08:00
std::string filename = std::string("gemm_settings.txt");
if (argc > 1) filename = std::string(argv[1]);
2016-12-06 06:01:52 +08:00
std::ifstream settings(filename);
long m, n, k;
while (settings >> m >> n >> k) {
// std::cerr << " Testing " << m << " " << n << " " << k << std::endl;
results.push_back(bench(m, n, k, f));
2016-12-06 06:01:52 +08:00
}
2016-12-06 06:01:52 +08:00
std::cout << RowVectorXd::Map(results.data(), results.size());
2016-12-06 06:01:52 +08:00
return 0;
}