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