outer_prod.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 outer_prod;
  20. template <typename V, typename M, bool C>
  21. class outer_prod<void(V,V,M), C> : public benchmark<void(V,V,M), C>
  22. {
  23. public:
  24. outer_prod(std::string const &name) : benchmark<void(V,V,M), C>(name) {}
  25. virtual void operation(long l)
  26. {
  27. ublas::opencl::outer_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 vector = ublas::vector<T>;
  35. using matrix = ublas::matrix<T>;
  36. std::string name = "opencl::outer_prod(vector<" + type + ">)";
  37. std::vector<long> sizes({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096});
  38. if (copy)
  39. {
  40. bm::opencl::outer_prod<void(vector, vector, matrix), true> p(name);
  41. p.run(sizes);
  42. }
  43. else
  44. {
  45. bm::opencl::outer_prod<void(vector, vector, matrix), false> p(name);
  46. p.run(sizes);
  47. }
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. opencl::library lib;
  52. po::variables_map vm;
  53. try
  54. {
  55. po::options_description desc("Outer product\n"
  56. "Allowed options");
  57. desc.add_options()("help,h", "produce help message");
  58. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  59. desc.add_options()("copy,c", po::value<bool>(), "include host<->device copy in timing");
  60. po::store(po::parse_command_line(argc, argv, desc), vm);
  61. po::notify(vm);
  62. if (vm.count("help"))
  63. {
  64. std::cout << desc << std::endl;
  65. return 0;
  66. }
  67. }
  68. catch(std::exception &e)
  69. {
  70. std::cerr << "error: " << e.what() << std::endl;
  71. return 1;
  72. }
  73. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  74. bool copy = vm.count("copy") ? vm["copy"].as<bool>() : false;
  75. if (type == "float")
  76. benchmark<float>("float", copy);
  77. else if (type == "double")
  78. benchmark<double>("double", copy);
  79. else if (type == "fcomplex")
  80. benchmark<std::complex<float>>("std::complex<float>", copy);
  81. else if (type == "dcomplex")
  82. benchmark<std::complex<double>>("std::complex<double>", copy);
  83. else
  84. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  85. }