mv_prod.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/numeric/ublas/matrix.hpp>
  10. #include <boost/program_options.hpp>
  11. #include "prod.hpp"
  12. #include <complex>
  13. #include <string>
  14. namespace po = boost::program_options;
  15. namespace ublas = boost::numeric::ublas;
  16. namespace bm = boost::numeric::ublas::benchmark;
  17. template <typename T>
  18. void benchmark(std::string const &type)
  19. {
  20. using matrix = ublas::matrix<T, ublas::basic_row_major<>>;
  21. using vector = ublas::vector<T>;
  22. bm::prod<vector(matrix, vector)> p("prod(matrix<" + type + ">, vector<" + type + ">)");
  23. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
  24. }
  25. int main(int argc, char **argv)
  26. {
  27. po::variables_map vm;
  28. try
  29. {
  30. po::options_description desc("Matrix-vector product\n"
  31. "Allowed options");
  32. desc.add_options()("help,h", "produce help message");
  33. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  34. po::store(po::parse_command_line(argc, argv, desc), vm);
  35. po::notify(vm);
  36. if (vm.count("help"))
  37. {
  38. std::cout << desc << std::endl;
  39. return 0;
  40. }
  41. }
  42. catch(std::exception &e)
  43. {
  44. std::cerr << "error: " << e.what() << std::endl;
  45. return 1;
  46. }
  47. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  48. if (type == "float")
  49. benchmark<float>("float");
  50. else if (type == "double")
  51. benchmark<double>("double");
  52. else if (type == "fcomplex")
  53. benchmark<std::complex<float>>("std::complex<float>");
  54. else if (type == "dcomplex")
  55. benchmark<std::complex<double>>("std::complex<double>");
  56. else
  57. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  58. }