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