add.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/numeric/ublas/vector.hpp>
  10. #include <boost/program_options.hpp>
  11. #include "../init.hpp"
  12. #include "../benchmark.hpp"
  13. #include <complex>
  14. #include <string>
  15. namespace po = boost::program_options;
  16. namespace ublas = boost::numeric::ublas;
  17. namespace boost { namespace numeric { namespace ublas { namespace benchmark {
  18. template <typename T>
  19. class add : public benchmark
  20. {
  21. public:
  22. add(std::string const &name) : benchmark(name) {}
  23. virtual void setup(long l)
  24. {
  25. init(a, l, 200);
  26. init(b, l, 200);
  27. }
  28. virtual void operation(long l)
  29. {
  30. for (int i = 0; i < l; ++i)
  31. c(i) = a(i) + b(i);
  32. }
  33. private:
  34. ublas::vector<T> a;
  35. ublas::vector<T> b;
  36. ublas::vector<T> c;
  37. };
  38. }}}}
  39. namespace bm = boost::numeric::ublas::benchmark;
  40. template <typename T>
  41. void benchmark(std::string const &type)
  42. {
  43. bm::add<T> p("ref::add(vector<" + type + ">)");
  44. p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. po::variables_map vm;
  49. try
  50. {
  51. po::options_description desc("Vector-vector addition (reference implementation)\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. }