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