Austin Schuh | 189376f | 2018-12-20 22:11:15 +1100 | [diff] [blame^] | 1 | #include <iostream> |
| 2 | #include <fstream> |
| 3 | #include <vector> |
| 4 | #include <Eigen/Core> |
| 5 | #include "../../BenchTimer.h" |
| 6 | using namespace Eigen; |
| 7 | |
| 8 | #ifndef SCALAR |
| 9 | #error SCALAR must be defined |
| 10 | #endif |
| 11 | |
| 12 | typedef SCALAR Scalar; |
| 13 | |
| 14 | typedef Matrix<Scalar,Dynamic,Dynamic> Mat; |
| 15 | |
| 16 | EIGEN_DONT_INLINE |
| 17 | void gemm(const Mat &A, const Mat &B, Mat &C) |
| 18 | { |
| 19 | C.noalias() += A * B; |
| 20 | } |
| 21 | |
| 22 | EIGEN_DONT_INLINE |
| 23 | double bench(long m, long n, long k) |
| 24 | { |
| 25 | Mat A(m,k); |
| 26 | Mat B(k,n); |
| 27 | Mat C(m,n); |
| 28 | A.setRandom(); |
| 29 | B.setRandom(); |
| 30 | C.setZero(); |
| 31 | |
| 32 | BenchTimer t; |
| 33 | |
| 34 | double up = 1e8*4/sizeof(Scalar); |
| 35 | double tm0 = 4, tm1 = 10; |
| 36 | if(NumTraits<Scalar>::IsComplex) |
| 37 | { |
| 38 | up /= 4; |
| 39 | tm0 = 2; |
| 40 | tm1 = 4; |
| 41 | } |
| 42 | |
| 43 | double flops = 2. * m * n * k; |
| 44 | long rep = std::max(1., std::min(100., up/flops) ); |
| 45 | long tries = std::max(tm0, std::min(tm1, up/flops) ); |
| 46 | |
| 47 | BENCH(t, tries, rep, gemm(A,B,C)); |
| 48 | |
| 49 | return 1e-9 * rep * flops / t.best(); |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) |
| 53 | { |
| 54 | std::vector<double> results; |
| 55 | |
| 56 | std::ifstream settings("gemm_settings.txt"); |
| 57 | long m, n, k; |
| 58 | while(settings >> m >> n >> k) |
| 59 | { |
| 60 | //std::cerr << " Testing " << m << " " << n << " " << k << std::endl; |
| 61 | results.push_back( bench(m, n, k) ); |
| 62 | } |
| 63 | |
| 64 | std::cout << RowVectorXd::Map(results.data(), results.size()); |
| 65 | |
| 66 | return 0; |
| 67 | } |