inner_prod.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <boost/numeric/ublas/vector.hpp>
  9. #include <boost/program_options.hpp>
  10. #include "../init.hpp"
  11. #include "../benchmark.hpp"
  12. #include <complex>
  13. #include <string>
  14. namespace boost { namespace numeric { namespace ublas { namespace benchmark {
  15. template <typename S> class inner_prod;
  16. template <typename R, typename V1, typename V2>
  17. class inner_prod<R(V1, V2)> : public benchmark
  18. {
  19. public:
  20. inner_prod(std::string const &name) : benchmark(name) {}
  21. virtual void setup(long l)
  22. {
  23. init(a, l, 200);
  24. init(b, l, 200);
  25. }
  26. virtual void operation(long l)
  27. {
  28. c = R(0);
  29. for (int i = 0; i < l; ++i)
  30. c += a(i) * b(i);
  31. }
  32. private:
  33. V1 a;
  34. V2 b;
  35. R c;
  36. };
  37. }}}}
  38. namespace po = boost::program_options;
  39. namespace ublas = boost::numeric::ublas;
  40. namespace bm = boost::numeric::ublas::benchmark;
  41. template <typename T>
  42. void benchmark(std::string const &type)
  43. {
  44. using vector = ublas::vector<T>;
  45. bm::inner_prod<T(vector, vector)> p("ref::inner_prod(vector<" + type + ">)");
  46. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}));
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. po::variables_map vm;
  51. try
  52. {
  53. po::options_description desc("Inner product (reference implementation)\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. po::store(po::parse_command_line(argc, argv, desc), vm);
  58. po::notify(vm);
  59. if (vm.count("help"))
  60. {
  61. std::cout << desc << std::endl;
  62. return 0;
  63. }
  64. }
  65. catch(std::exception &e)
  66. {
  67. std::cerr << "error: " << e.what() << std::endl;
  68. return 1;
  69. }
  70. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  71. if (type == "float")
  72. benchmark<float>("float");
  73. else if (type == "double")
  74. benchmark<double>("double");
  75. else if (type == "fcomplex")
  76. benchmark<std::complex<float>>("std::complex<float>");
  77. else if (type == "dcomplex")
  78. benchmark<std::complex<double>>("std::complex<double>");
  79. else
  80. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  81. }