• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
16 namespace boost { namespace numeric { namespace ublas { namespace benchmark {
17 
18 template <typename S> class inner_prod;
19 
20 template <typename R, typename V1, typename V2>
21 class inner_prod<R(V1, V2)> : public benchmark
22 {
23 public:
inner_prod(std::string const & name)24   inner_prod(std::string const &name) : benchmark(name) {}
setup(long l)25   virtual void setup(long l)
26   {
27     init(a, l, 200);
28     init(b, l, 200);
29   }
operation(long l)30   virtual void operation(long l)
31   {
32     c = ublas::inner_prod(a, b);
33   }
34 private:
35   V1 a;
36   V2 b;
37   R c;
38 };
39 
40 }}}}
41 
42 namespace po = boost::program_options;
43 namespace ublas = boost::numeric::ublas;
44 namespace bm = boost::numeric::ublas::benchmark;
45 
46 template <typename T>
benchmark(std::string const & type)47 void benchmark(std::string const &type)
48 {
49   using vector = ublas::vector<T>;
50   bm::inner_prod<T(vector, vector)> p("inner_prod(vector<" + type + ">)");
51   p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}));
52 }
53 
main(int argc,char ** argv)54 int main(int argc, char **argv)
55 {
56   po::variables_map vm;
57   try
58   {
59     po::options_description desc("Inner product\n"
60                                  "Allowed options");
61     desc.add_options()("help,h", "produce help message");
62     desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
63 
64     po::store(po::parse_command_line(argc, argv, desc), vm);
65     po::notify(vm);
66 
67     if (vm.count("help"))
68     {
69       std::cout << desc << std::endl;
70       return 0;
71     }
72   }
73   catch(std::exception &e)
74   {
75     std::cerr << "error: " << e.what() << std::endl;
76     return 1;
77   }
78   std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
79   if (type == "float")
80     benchmark<float>("float");
81   else if (type == "double")
82     benchmark<double>("double");
83   else if (type == "fcomplex")
84     benchmark<std::complex<float>>("std::complex<float>");
85   else if (type == "dcomplex")
86     benchmark<std::complex<double>>("std::complex<double>");
87   else
88     std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
89 }
90