mm_prod.cpp 2.4 KB

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