mm_prod.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright (c) 2018 Stefan Seefeld
  3. // All rights reserved.
  4. //
  5. // This file is part of Boost.uBLAS. It is made available under the
  6. // Boost Software License, Version 1.0.
  7. // (Consult LICENSE or http://www.boost.org/LICENSE_1_0.txt)
  8. #define BOOST_UBLAS_ENABLE_OPENCL
  9. #include <boost/numeric/ublas/opencl.hpp>
  10. #include <boost/program_options.hpp>
  11. #include "benchmark.hpp"
  12. #include <complex>
  13. #include <string>
  14. namespace po = boost::program_options;
  15. namespace ublas = boost::numeric::ublas;
  16. namespace bm = boost::numeric::ublas::benchmark;
  17. namespace opencl = boost::numeric::ublas::opencl;
  18. namespace boost { namespace numeric { namespace ublas { namespace benchmark { namespace opencl {
  19. template <typename S, bool C> class prod;
  20. template <typename M, bool C>
  21. class prod<void(M,M,M), C> : public benchmark<void(M,M,M), C>
  22. {
  23. public:
  24. prod(std::string const &name) : benchmark<void(M,M,M), C>(name) {}
  25. virtual void operation(long l)
  26. {
  27. ublas::opencl::prod(*this->a, *this->b, *this->c, this->queue);
  28. }
  29. };
  30. }}}}}
  31. template <typename T>
  32. void benchmark(std::string const &type, bool copy)
  33. {
  34. using matrix = ublas::matrix<T>;
  35. std::string name = "opencl::prod(matrix<" + type + ">)";
  36. std::vector<long> sizes({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096});
  37. if (copy)
  38. {
  39. bm::opencl::prod<void(matrix, matrix, matrix), true> p(name);
  40. p.run(sizes);
  41. }
  42. else
  43. {
  44. bm::opencl::prod<void(matrix, matrix, matrix), false> p(name);
  45. p.run(sizes);
  46. }
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. opencl::library lib;
  51. po::variables_map vm;
  52. try
  53. {
  54. po::options_description desc("Matrix product (OpenCL)\n"
  55. "Allowed options");
  56. desc.add_options()("help,h", "produce help message");
  57. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  58. desc.add_options()("copy,c", po::value<bool>(), "include host<->device copy in timing");
  59. po::store(po::parse_command_line(argc, argv, desc), vm);
  60. po::notify(vm);
  61. if (vm.count("help"))
  62. {
  63. std::cout << desc << std::endl;
  64. return 0;
  65. }
  66. }
  67. catch(std::exception &e)
  68. {
  69. std::cerr << "error: " << e.what() << std::endl;
  70. return 1;
  71. }
  72. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  73. bool copy = vm.count("copy") ? vm["copy"].as<bool>() : false;
  74. if (type == "float")
  75. benchmark<float>("float", copy);
  76. else if (type == "double")
  77. benchmark<double>("double", copy);
  78. else if (type == "fcomplex")
  79. benchmark<std::complex<float>>("std::complex<float>", copy);
  80. else if (type == "dcomplex")
  81. benchmark<std::complex<double>>("std::complex<double>", copy);
  82. else
  83. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  84. }