outer_prod.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. for (int i = 0; i < l; ++i)
  29. for (int j = 0; j < l; ++j)
  30. c(i,j) = - a(i) * b(j);
  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. using matrix = ublas::matrix<T>;
  46. bm::outer_prod<matrix(vector, vector)> p("ref::outer_prod(vector<" + type + ">)");
  47. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. po::variables_map vm;
  52. try
  53. {
  54. po::options_description desc("Outer product (reference implementation)\n"
  55. "Allowed options");
  56. desc.add_options()("help,h", "produce help message");
  57. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  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. if (type == "float")
  73. benchmark<float>("float");
  74. else if (type == "double")
  75. benchmark<double>("double");
  76. else if (type == "fcomplex")
  77. benchmark<std::complex<float>>("std::complex<float>");
  78. else if (type == "dcomplex")
  79. benchmark<std::complex<double>>("std::complex<double>");
  80. else
  81. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  82. }