add.cpp 2.7 KB

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