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