add.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "add.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 vector = ublas::vector<T>;
  21. bm::add<vector(vector, vector)> a("add(vector<" + type + ">, vector<" + type + ">)");
  22. a.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
  23. }
  24. int main(int argc, char **argv)
  25. {
  26. po::variables_map vm;
  27. try
  28. {
  29. po::options_description desc("Vector-vector addition\n"
  30. "Allowed options");
  31. desc.add_options()("help,h", "produce help message");
  32. desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
  33. po::store(po::parse_command_line(argc, argv, desc), vm);
  34. po::notify(vm);
  35. if (vm.count("help"))
  36. {
  37. std::cout << desc << std::endl;
  38. return 0;
  39. }
  40. }
  41. catch(std::exception &e)
  42. {
  43. std::cerr << "error: " << e.what() << std::endl;
  44. return 1;
  45. }
  46. std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
  47. if (type == "float")
  48. benchmark<float>("float");
  49. else if (type == "double")
  50. benchmark<double>("double");
  51. else if (type == "fcomplex")
  52. benchmark<std::complex<float>>("std::complex<float>");
  53. else if (type == "dcomplex")
  54. benchmark<std::complex<double>>("std::complex<double>");
  55. else
  56. std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
  57. }