inner_prod.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = ublas::inner_prod(a, b);
  29. }
  30. private:
  31. V1 a;
  32. V2 b;
  33. R c;
  34. };
  35. }}}}
  36. namespace po = boost::program_options;
  37. namespace ublas = boost::numeric::ublas;
  38. namespace bm = boost::numeric::ublas::benchmark;
  39. template <typename T>
  40. void benchmark(std::string const &type)
  41. {
  42. using vector = ublas::vector<T>;
  43. bm::inner_prod<T(vector, vector)> p("inner_prod(vector<" + type + ">)");
  44. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}));
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. po::variables_map vm;
  49. try
  50. {
  51. po::options_description desc("Inner product\n"
  52. "Allowed options");
  53. desc.add_options()("help,h", "produce help message");
  54. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  55. po::store(po::parse_command_line(argc, argv, desc), vm);
  56. po::notify(vm);
  57. if (vm.count("help"))
  58. {
  59. std::cout << desc << std::endl;
  60. return 0;
  61. }
  62. }
  63. catch(std::exception &e)
  64. {
  65. std::cerr << "error: " << e.what() << std::endl;
  66. return 1;
  67. }
  68. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  69. if (type == "float")
  70. benchmark<float>("float");
  71. else if (type == "double")
  72. benchmark<double>("double");
  73. else if (type == "fcomplex")
  74. benchmark<std::complex<float>>("std::complex<float>");
  75. else if (type == "dcomplex")
  76. benchmark<std::complex<double>>("std::complex<double>");
  77. else
  78. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  79. }