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 outer_prod;
19
20 template <typename R, typename V1, typename V2>
21 class outer_prod<R(V1, V2)> : public benchmark
22 {
23 public:
outer_prod(std::string const & name)24 outer_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::outer_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 using matrix = ublas::matrix<T>;
51 bm::outer_prod<matrix(vector, vector)> p("outer_prod(vector<" + type + ">)");
52 p.run(std::vector<long>({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096}));
53 }
54
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57 po::variables_map vm;
58 try
59 {
60 po::options_description desc("Outer product\n"
61 "Allowed options");
62 desc.add_options()("help,h", "produce help message");
63 desc.add_options()("type,t", po::value<std::string>(), "select value-type (float, double, fcomplex, dcomplex)");
64
65 po::store(po::parse_command_line(argc, argv, desc), vm);
66 po::notify(vm);
67
68 if (vm.count("help"))
69 {
70 std::cout << desc << std::endl;
71 return 0;
72 }
73 }
74 catch(std::exception &e)
75 {
76 std::cerr << "error: " << e.what() << std::endl;
77 return 1;
78 }
79 std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
80 if (type == "float")
81 benchmark<float>("float");
82 else if (type == "double")
83 benchmark<double>("double");
84 else if (type == "fcomplex")
85 benchmark<std::complex<float>>("std::complex<float>");
86 else if (type == "dcomplex")
87 benchmark<std::complex<double>>("std::complex<double>");
88 else
89 std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
90 }
91