1 //
2 // Copyright (c) 2018 Stefan Seefeld
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or
6 // copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #define BOOST_UBLAS_ENABLE_OPENCL
9 #include <boost/numeric/ublas/opencl.hpp>
10 #include <boost/program_options.hpp>
11 #include "benchmark.hpp"
12 #include <complex>
13 #include <string>
14
15 namespace po = boost::program_options;
16 namespace ublas = boost::numeric::ublas;
17 namespace bm = boost::numeric::ublas::benchmark;
18 namespace opencl = boost::numeric::ublas::opencl;
19
20 namespace boost { namespace numeric { namespace ublas { namespace benchmark { namespace opencl {
21
22 template <typename S, bool C> class add;
23
24 template <typename V, bool C>
25 class add<void(V,V,V), C> : public benchmark<void(V,V,V), C>
26 {
27 public:
add(std::string const & name)28 add(std::string const &name) : benchmark<void(V,V,V), C>(name) {}
operation(long l)29 virtual void operation(long l)
30 {
31 ublas::opencl::element_add(*this->a, *this->b, *this->c, this->queue);
32 }
33 };
34
35 }}}}}
36
37 template <typename T>
benchmark(std::string const & type,bool copy)38 void benchmark(std::string const &type, bool copy)
39 {
40 using vector = ublas::vector<T>;
41 std::string name = "opencl::elementwise_add(vector<" + type + ">)";
42 std::vector<long> sizes({1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096});
43 if (copy)
44 {
45 bm::opencl::add<void(vector, vector, vector), true> p(name);
46 p.run(sizes);
47 }
48 else
49 {
50 bm::opencl::add<void(vector, vector, vector), false> p(name);
51 p.run(sizes);
52 }
53 }
54
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57 opencl::library lib;
58 po::variables_map vm;
59 try
60 {
61 po::options_description desc("Matrix-vector product (OpenCL)\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 desc.add_options()("copy,c", po::value<bool>(), "include host<->device copy in timing");
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 bool copy = vm.count("copy") ? vm["copy"].as<bool>() : false;
83 if (type == "float")
84 benchmark<float>("float", copy);
85 else if (type == "double")
86 benchmark<double>("double", copy);
87 else if (type == "fcomplex")
88 benchmark<std::complex<float>>("std::complex<float>", copy);
89 else if (type == "dcomplex")
90 benchmark<std::complex<double>>("std::complex<double>", copy);
91 else
92 std::cerr << "unsupported value-type \"" << vm["type"].as<std::string>() << '\"' << std::endl;
93 }
94