From da855c19a8bb3baa384240a6a7ab46731bf75d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkjak@fjfi.cvut.cz> Date: Wed, 30 Dec 2015 11:20:21 +0100 Subject: [PATCH] Splitting main benchmark functions into separate header --- tests/benchmarks/benchmarks.h | 94 ++++++++++++++++++++++++++ tests/benchmarks/tnl-cuda-benchmarks.h | 86 ++--------------------- 2 files changed, 98 insertions(+), 82 deletions(-) create mode 100644 tests/benchmarks/benchmarks.h diff --git a/tests/benchmarks/benchmarks.h b/tests/benchmarks/benchmarks.h new file mode 100644 index 0000000000..b61b027400 --- /dev/null +++ b/tests/benchmarks/benchmarks.h @@ -0,0 +1,94 @@ +#pragma once + +#include <iostream> + +#include <core/tnlTimerRT.h> + +namespace tnl +{ +namespace benchmarks +{ + +// TODO: add data member for error message +struct BenchmarkError {}; + +auto trueFunc = []() { return true; }; +auto voidFunc = [](){}; + +template< typename ComputeFunction, + typename CheckFunction, + typename ResetFunction > +double +benchmarkSingle( const int & loops, + const double & datasetSize, // in GB + ComputeFunction compute, + // TODO: check that default argument works here + CheckFunction check = trueFunc, + ResetFunction reset = voidFunc ) +{ + tnlTimerRT timer; + timer.reset(); + + for(int i = 0; i < loops; ++i) { + timer.start(); + compute(); + timer.stop(); + + if( ! check() ) + throw BenchmarkError(); + + reset(); + } + + const double time = timer.getTime(); + const double bandwidth = datasetSize / time; + std::cout << "bandwidth: " << bandwidth << " GB/sec, time: " << time << " sec." << std::endl; + + return time; +} + +template< typename ComputeHostFunction, + typename ComputeCudaFunction, + typename CheckFunction, + typename ResetFunction > +void +benchmarkCuda( const int & loops, + const double & datasetSize, // in GB + ComputeHostFunction computeHost, + ComputeCudaFunction computeCuda, + // TODO: check that default argument works here + CheckFunction check = trueFunc, + ResetFunction reset = voidFunc ) +{ + tnlTimerRT timerHost, timerCuda; + timerHost.reset(); + timerHost.stop(); + timerCuda.reset(); + timerCuda.stop(); + + for(int i = 0; i < loops; ++i) { + timerHost.start(); + computeHost(); + timerHost.stop(); + + timerCuda.start(); + computeCuda(); + timerCuda.stop(); + + if( ! check() ) + throw BenchmarkError(); + + reset(); + } + + const double timeHost = timerHost.getTime(); + const double timeCuda = timerCuda.getTime(); + const double bandwidthHost = datasetSize / timeHost; + const double bandwidthCuda = datasetSize / timeCuda; + std::cout << " CPU: bandwidth: " << bandwidthHost << " GB/sec, time: " << timeHost << " sec." << std::endl; + std::cout << " GPU: bandwidth: " << bandwidthCuda << " GB/sec, time: " << timeCuda << " sec." << std::endl; + std::cout << " CPU/GPU speedup: " << timeHost / timeCuda << std::endl; +} + +} // namespace benchmarks +} // namespace tnl diff --git a/tests/benchmarks/tnl-cuda-benchmarks.h b/tests/benchmarks/tnl-cuda-benchmarks.h index 473ca4277a..9699ac029c 100644 --- a/tests/benchmarks/tnl-cuda-benchmarks.h +++ b/tests/benchmarks/tnl-cuda-benchmarks.h @@ -20,12 +20,15 @@ #include <tnlConfig.h> #include <core/vectors/tnlVector.h> -#include <core/tnlTimerRT.h> #include <core/tnlList.h> #include <matrices/tnlSlicedEllpackMatrix.h> #include <matrices/tnlEllpackMatrix.h> #include <matrices/tnlCSRMatrix.h> +#include "benchmarks.h" + +using namespace tnl::benchmarks; + #ifdef HAVE_CUBLAS //#include <cublas.h> #endif @@ -110,87 +113,6 @@ void setCudaTestMatrix( Matrix& matrix, } -// TODO: add data member for error message -struct BenchmarkError {}; - -auto trueFunc = []() { return true; }; -auto voidFunc = [](){}; - -template< typename ComputeFunction, - typename CheckFunction, - typename ResetFunction > -double -benchmarkSingle( const int & loops, - const double & datasetSize, // in GB - ComputeFunction compute, - // TODO: check that default argument works here - CheckFunction check = trueFunc, - ResetFunction reset = voidFunc ) -{ - tnlTimerRT timer; - timer.reset(); - - for(int i = 0; i < loops; ++i) { - timer.start(); - compute(); - timer.stop(); - - if( ! check() ) - throw BenchmarkError(); - - reset(); - } - - const double time = timer.getTime(); - const double bandwidth = datasetSize / time; - cout << "bandwidth: " << bandwidth << " GB/sec, time: " << time << " sec." << endl; - - return time; -} - -template< typename ComputeHostFunction, - typename ComputeCudaFunction, - typename CheckFunction, - typename ResetFunction > -void -benchmarkCuda( const int & loops, - const double & datasetSize, // in GB - ComputeHostFunction computeHost, - ComputeCudaFunction computeCuda, - // TODO: check that default argument works here - CheckFunction check = trueFunc, - ResetFunction reset = voidFunc ) -{ - tnlTimerRT timerHost, timerCuda; - timerHost.reset(); - timerHost.stop(); - timerCuda.reset(); - timerCuda.stop(); - - for(int i = 0; i < loops; ++i) { - timerHost.start(); - computeHost(); - timerHost.stop(); - - timerCuda.start(); - computeCuda(); - timerCuda.stop(); - - if( ! check() ) - throw BenchmarkError(); - - reset(); - } - - const double timeHost = timerHost.getTime(); - const double timeCuda = timerCuda.getTime(); - const double bandwidthHost = datasetSize / timeHost; - const double bandwidthCuda = datasetSize / timeCuda; - cout << " CPU: bandwidth: " << bandwidthHost << " GB/sec, time: " << timeHost << " sec." << endl; - cout << " GPU: bandwidth: " << bandwidthCuda << " GB/sec, time: " << timeCuda << " sec." << endl; - cout << " CPU/GPU speedup: " << timeHost / timeCuda << endl; -} - template< typename Real, template< typename, typename, typename > class Matrix, template< typename, typename, typename > class Vector = tnlVector > -- GitLab