• 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 #define BOOST_UBLAS_ENABLE_OPENCL
10 #include <boost/numeric/ublas/opencl.hpp>
11 #include <boost/program_options.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 bm = boost::numeric::ublas::benchmark;
19 namespace opencl = boost::numeric::ublas::opencl;
20 
21 namespace boost { namespace numeric { namespace ublas { namespace benchmark { namespace opencl {
22 
23 template <typename S, bool C> class outer_prod;
24 
25 template <typename V, typename M, bool C>
26 class outer_prod<void(V,V,M), C> : public benchmark<void(V,V,M), C>
27 {
28 public:
outer_prod(std::string const & name)29   outer_prod(std::string const &name) : benchmark<void(V,V,M), C>(name) {}
operation(long l)30   virtual void operation(long l)
31   {
32     ublas::opencl::outer_prod(*this->a, *this->b, *this->c, this->queue);
33   }
34 };
35 
36 }}}}}
37 
38 template <typename T>
benchmark(std::string const & type,bool copy)39 void benchmark(std::string const &type, bool copy)
40 {
41   using vector = ublas::vector<T>;
42   using matrix = ublas::matrix<T>;
43   std::string name = "opencl::outer_prod(vector<" + type + ">)";
44   std::vector<long> sizes({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096});
45   if (copy)
46   {
47     bm::opencl::outer_prod<void(vector, vector, matrix), true> p(name);
48     p.run(sizes);
49   }
50   else
51   {
52     bm::opencl::outer_prod<void(vector, vector, matrix), false> p(name);
53     p.run(sizes);
54   }
55 }
56 
main(int argc,char ** argv)57 int main(int argc, char **argv)
58 {
59   opencl::library lib;
60   po::variables_map vm;
61   try
62   {
63     po::options_description desc("Outer product\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     desc.add_options()("copy,c", po::value<bool>(), "include host<->device copy in timing");
68 
69     po::store(po::parse_command_line(argc, argv, desc), vm);
70     po::notify(vm);
71 
72     if (vm.count("help"))
73     {
74       std::cout << desc << std::endl;
75       return 0;
76     }
77   }
78   catch(std::exception &e)
79   {
80     std::cerr << "error: " << e.what() << std::endl;
81     return 1;
82   }
83   std::string type = vm.count("type") ? vm["type"].as<std::string>() : "float";
84   bool copy = vm.count("copy") ? vm["copy"].as<bool>() : false;
85   if (type == "float")
86     benchmark<float>("float", copy);
87   else if (type == "double")
88     benchmark<double>("double", copy);
89   else if (type == "fcomplex")
90     benchmark<std::complex<float>>("std::complex<float>", copy);
91   else if (type == "dcomplex")
92     benchmark<std::complex<double>>("std::complex<double>", copy);
93   else
94     std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
95 }
96