Files
triton/bench/blas.cpp

125 lines
3.7 KiB
C++
Raw Normal View History

2014-11-06 07:07:27 -05:00
//#define VIENNACL_DEBUG_ALL
2014-10-27 05:35:04 -04:00
#include "viennacl/matrix.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/tools/timer.hpp"
2014-11-06 07:07:27 -05:00
#include "viennacl/linalg/prod.hpp"
#include "viennacl/linalg/inner_prod.hpp"
#include "viennacl/scheduler/execute.hpp"
2014-10-27 05:35:04 -04:00
2014-10-29 17:03:24 +01:00
#include "atidlas/tools/misc.hpp"
2014-10-27 05:35:04 -04:00
#include "atidlas/model/import.hpp"
#include "atidlas/model/model.hpp"
2014-11-06 07:07:27 -05:00
#include "common.hpp"
2014-10-27 05:35:04 -04:00
#include <iomanip>
#include <stdlib.h>
2014-11-06 07:07:27 -05:00
2014-10-27 05:35:04 -04:00
namespace ad = atidlas;
typedef atidlas::atidlas_int_t int_t;
2014-10-30 13:04:33 -04:00
template<class T>
2014-11-06 07:07:27 -05:00
void bench(std::map<std::string, ad::tools::shared_ptr<ad::model> > & models)
2014-10-30 13:04:33 -04:00
{
2014-11-06 07:07:27 -05:00
typedef viennacl::matrix<T,viennacl::column_major> matrix_type;
typedef viennacl::vector<T> vector_type;
2014-10-30 13:04:33 -04:00
2014-10-27 05:35:04 -04:00
viennacl::tools::timer timer;
2014-10-29 17:03:24 +01:00
float total_time = 0;
std::vector<T> times;
2014-10-27 05:35:04 -04:00
#define BENCHMARK(OP, resname) \
2014-10-29 17:03:24 +01:00
times.clear();\
total_time = 0;\
2014-10-27 05:35:04 -04:00
OP;\
viennacl::backend::finish();\
2014-11-06 07:07:27 -05:00
while(total_time < 1e-2){\
2014-10-29 17:03:24 +01:00
timer.start(); \
OP;\
viennacl::backend::finish();\
times.push_back(timer.get());\
total_time += times.back();\
}\
2014-10-27 05:35:04 -04:00
viennacl::backend::finish();\
2014-10-29 17:03:24 +01:00
float resname = ad::tools::median(times);
2014-10-27 05:35:04 -04:00
2014-10-30 13:04:33 -04:00
#define BENCH(declarations, statement_op, sizes, measure, N, key) \
2014-11-06 07:07:27 -05:00
if(models.find(key)!=models.end()){\
if(first==false)\
2014-11-06 07:07:27 -05:00
{\
std::cout << std::endl;\
std::cout << std::endl;\
2014-11-06 07:07:27 -05:00
}\
std::cout << "#" << key << std::endl;\
for(std::vector<int_t>::const_iterator it = sizes.begin() ; it != sizes.end() ; ++it)\
{\
declarations;\
viennacl::scheduler::statement statement(statement_op);\
BENCHMARK(viennacl::scheduler::execute(statement), time_viennacl);\
2014-11-06 07:07:27 -05:00
BENCHMARK(models.at(key)->execute(statement), time_model);\
BENCHMARK(models[key]->execute(statement, true), time_unique_kernel);\
models[key]->tune(statement);\
BENCHMARK(models[key]->execute(statement), time_opt);\
std::cout << *it << " " << measure<T>(N,time_viennacl) << " " << measure<T>(N,time_model) << " " << measure<T>(N,time_opt) << std::endl;\
2014-11-06 07:07:27 -05:00
}\
2014-10-30 13:04:33 -04:00
}\
2014-10-27 05:35:04 -04:00
2014-10-30 13:04:33 -04:00
#define DECLARE(type, ...) type __VA_ARGS__
#define ARGS(...) __VA_ARGS__
2014-10-27 05:35:04 -04:00
2014-11-06 07:07:27 -05:00
/*---------*/
/*--BLAS1--*/
/*---------*/
2014-10-27 05:35:04 -04:00
2014-11-06 07:07:27 -05:00
//AXPY
bool first =true;
BENCH(DECLARE(viennacl::vector<T>, x(*it), y(*it)), ARGS(y, viennacl::op_assign(), x + y),
BLAS1_N, bandwidth, 3*(*it), "vector-axpy-float32");
first=false;
//DOT
BENCH(DECLARE(viennacl::scalar<T> s(0)); DECLARE(vector_type, x(*it), y(*it)), ARGS(s, viennacl::op_assign(), viennacl::linalg::inner_prod(x,y)),
BLAS1_N, bandwidth, 2*(*it), "reduction-float32");
/*---------*/
/*--BLAS2--*/
/*---------*/
//N-layout
for(std::vector<int>::const_iterator Mit = BLAS2_M.begin() ; Mit != BLAS2_M.end() ; ++Mit)
{
BENCH(DECLARE(matrix_type, A(*Mit,*it)); DECLARE(vector_type, y(*Mit), x(*it)),ARGS(y, viennacl::op_assign(), viennacl::linalg::prod(A,x)), BLAS2_N,
bandwidth, (*Mit)*(*it), "row-wise-reductionN-float32");
}
//T-layout
for(std::vector<int>::const_iterator Mit = BLAS2_M.begin() ; Mit != BLAS2_M.end() ; ++Mit)
{
BENCH(DECLARE(matrix_type, A(*it,*Mit)) ; DECLARE(vector_type, y(*Mit), x(*it)), ARGS(y, viennacl::op_assign(), viennacl::linalg::prod(viennacl::trans(A),x)), BLAS2_N,
bandwidth, (*Mit)*(*it), "row-wise-reductionT-float32");
}
/*---------*/
/*--BLAS3--*/
/*---------*/
2014-10-27 05:35:04 -04:00
}
2014-10-29 17:03:24 +01:00
int main(int argc, char* argv[])
2014-10-27 05:35:04 -04:00
{
2014-10-29 17:03:24 +01:00
if(argc != 2)
{
std::cerr << "Usage : PROG model_file" << std::endl;
2014-10-30 13:04:33 -04:00
exit(EXIT_FAILURE);
2014-10-29 17:03:24 +01:00
}
std::map<std::string, ad::tools::shared_ptr<ad::model> > models = ad::import(argv[1]);
2014-10-27 05:35:04 -04:00
2014-10-30 13:04:33 -04:00
std::cout << "#Benchmark : BLAS" << std::endl;
std::cout << "#----------------" << std::endl;
2014-11-06 07:07:27 -05:00
bench<float>(models);
2014-10-27 05:35:04 -04:00
}