outer_prod.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 outer_prod;
  16. template <typename R, typename V1, typename V2>
  17. class outer_prod<R(V1, V2)> : public benchmark
  18. {
  19. public:
  20. outer_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::outer_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. using matrix = ublas::matrix<T>;
  44. bm::outer_prod<matrix(vector, vector)> p("outer_prod(vector<" + type + ">)");
  45. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
  46. }
  47. int main(int argc, char **argv)
  48. {
  49. po::variables_map vm;
  50. try
  51. {
  52. po::options_description desc("Outer product\n"
  53. "Allowed options");
  54. desc.add_options()("help,h", "produce help message");
  55. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  56. po::store(po::parse_command_line(argc, argv, desc), vm);
  57. po::notify(vm);
  58. if (vm.count("help"))
  59. {
  60. std::cout << desc << std::endl;
  61. return 0;
  62. }
  63. }
  64. catch(std::exception &e)
  65. {
  66. std::cerr << "error: " << e.what() << std::endl;
  67. return 1;
  68. }
  69. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  70. if (type == "float")
  71. benchmark<float>("float");
  72. else if (type == "double")
  73. benchmark<double>("double");
  74. else if (type == "fcomplex")
  75. benchmark<std::complex<float>>("std::complex<float>");
  76. else if (type == "dcomplex")
  77. benchmark<std::complex<double>>("std::complex<double>");
  78. else
  79. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  80. }