benchmark.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifndef opencl_benchmark_hpp_
  8. #define opencl_benchmark_hpp_
  9. #define BOOST_UBLAS_ENABLE_OPENCL
  10. #include <boost/numeric/ublas/opencl.hpp>
  11. #include "../benchmark.hpp"
  12. #include "init.hpp"
  13. #include <memory>
  14. namespace boost { namespace numeric { namespace ublas { namespace benchmark {
  15. namespace opencl {
  16. struct base
  17. {
  18. base(compute::device d = compute::system::default_device())
  19. : context(d),
  20. queue(context, d)
  21. {}
  22. compute::context context;
  23. compute::command_queue queue;
  24. };
  25. template <typename T, bool C> struct data_factory;
  26. template <typename T>
  27. struct data_factory<T, true>
  28. {
  29. typedef T type;
  30. typedef std::unique_ptr<type> ptr_type;
  31. static ptr_type create(long) { return ptr_type(new T());}
  32. };
  33. template <typename T>
  34. struct data_factory<T, false>
  35. {
  36. typedef T type;
  37. typedef std::unique_ptr<type> ptr_type;
  38. static ptr_type create(long, compute::context) { return ptr_type(new T());}
  39. };
  40. template <>
  41. struct data_factory<void, true>
  42. {
  43. typedef void type;
  44. typedef void *ptr_type;
  45. static ptr_type create(long) { return 0;}
  46. };
  47. template <>
  48. struct data_factory<void, false>
  49. {
  50. typedef void type;
  51. typedef void *ptr_type;
  52. static ptr_type create(long, compute::context) { return 0;}
  53. };
  54. template <typename T>
  55. struct data_factory<ublas::vector<T>, true>
  56. {
  57. typedef ublas::vector<T> type;
  58. typedef std::unique_ptr<type> ptr_type;
  59. static ptr_type create(long l) { return ptr_type(new type(l));}
  60. };
  61. template <typename T>
  62. struct data_factory<ublas::vector<T>, false>
  63. {
  64. typedef ublas::vector<T, ublas::opencl::storage> type;
  65. typedef std::unique_ptr<type> ptr_type;
  66. static ptr_type create(long l, compute::context c)
  67. { return ptr_type(new type(l, c));}
  68. };
  69. template <typename T, typename L>
  70. struct data_factory<ublas::matrix<T, L>, true>
  71. {
  72. typedef ublas::matrix<T, L> type;
  73. typedef std::unique_ptr<type> ptr_type;
  74. static ptr_type create(long l)
  75. { return ptr_type(new type(l, l));}
  76. };
  77. template <typename T, typename L>
  78. struct data_factory<ublas::matrix<T, L>, false>
  79. {
  80. typedef ublas::matrix<T, L, ublas::opencl::storage> type;
  81. typedef std::unique_ptr<type> ptr_type;
  82. static ptr_type create(long l, compute::context c)
  83. { return ptr_type(new type(l, l, c));}
  84. };
  85. template <typename S, bool> class benchmark;
  86. template <typename R, typename O>
  87. class benchmark<R(O), true> : public base, public ublas::benchmark::benchmark
  88. {
  89. public:
  90. benchmark(std::string const &name)
  91. : base(),
  92. ublas::benchmark::benchmark(name + " with copy")
  93. {}
  94. virtual void setup(long l)
  95. {
  96. r = data_factory<R, true>::create(l);
  97. a = data_factory<O, true>::create(l);
  98. init(*a, l, 200);
  99. }
  100. typename data_factory<R, true>::ptr_type r;
  101. typename data_factory<O, true>::ptr_type a;
  102. };
  103. template <typename R, typename O1, typename O2>
  104. class benchmark<R(O1, O2), true> : public base, public ublas::benchmark::benchmark
  105. {
  106. public:
  107. benchmark(std::string const &name)
  108. : base(),
  109. ublas::benchmark::benchmark(name + " with copy")
  110. {}
  111. virtual void setup(long l)
  112. {
  113. r = data_factory<R, true>::create(l);
  114. a = data_factory<O1, true>::create(l);
  115. init(*a, l, 200);
  116. b = data_factory<O2, true>::create(l);
  117. init(*b, l, 200);
  118. }
  119. typename data_factory<R, true>::ptr_type r;
  120. typename data_factory<O1, true>::ptr_type a;
  121. typename data_factory<O2, true>::ptr_type b;
  122. };
  123. template <typename R, typename O1, typename O2, typename O3>
  124. class benchmark<R(O1, O2, O3), true> : public base, public ublas::benchmark::benchmark
  125. {
  126. public:
  127. benchmark(std::string const &name)
  128. : base(),
  129. ublas::benchmark::benchmark(name + " with copy")
  130. {}
  131. virtual void setup(long l)
  132. {
  133. r = data_factory<R, true>::create(l);
  134. a = data_factory<O1, true>::create(l);
  135. init(*a, l, 200);
  136. b = data_factory<O2, true>::create(l);
  137. init(*b, l, 200);
  138. c = data_factory<O3, true>::create(l);
  139. init(*c, l, 200);
  140. }
  141. typename data_factory<R, true>::ptr_type r;
  142. typename data_factory<O1, true>::ptr_type a;
  143. typename data_factory<O2, true>::ptr_type b;
  144. typename data_factory<O3, true>::ptr_type c;
  145. };
  146. template <typename R, typename O>
  147. class benchmark<R(O), false> : public base, public ublas::benchmark::benchmark
  148. {
  149. public:
  150. benchmark(std::string const &name)
  151. : base(),
  152. ublas::benchmark::benchmark(name + " w/o copy")
  153. {}
  154. virtual void setup(long l)
  155. {
  156. r = data_factory<R, false>::create(l, context);
  157. a = data_factory<O, false>::create(l, context);
  158. init(*a, l, 200);
  159. }
  160. typename data_factory<R, false>::ptr_type r;
  161. typename data_factory<O, false>::ptr_type a;
  162. };
  163. template <typename R, typename O1, typename O2>
  164. class benchmark<R(O1, O2), false> : public base, public ublas::benchmark::benchmark
  165. {
  166. public:
  167. benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
  168. virtual void setup(long l)
  169. {
  170. r = data_factory<R, false>::create(l, context);
  171. a = data_factory<O1, false>::create(l, context);
  172. init(*a, l, 200);
  173. b = data_factory<O2, false>::create(l, context);
  174. init(*b, l, 200);
  175. }
  176. typename data_factory<R, false>::ptr_type r;
  177. typename data_factory<O1, false>::ptr_type a;
  178. typename data_factory<O2, false>::ptr_type b;
  179. };
  180. template <typename R, typename O1, typename O2, typename O3>
  181. class benchmark<R(O1, O2, O3), false> : public base, public ublas::benchmark::benchmark
  182. {
  183. public:
  184. benchmark(std::string const &name) : base(), ublas::benchmark::benchmark(name + " w/o copy") {}
  185. virtual void setup(long l)
  186. {
  187. r = data_factory<R, false>::create(l, context);
  188. a = data_factory<O1, false>::create(l, context);
  189. init(*a, l, 200);
  190. b = data_factory<O2, false>::create(l, context);
  191. init(*b, l, 200);
  192. c = data_factory<O3, false>::create(l, context);
  193. init(*c, l, 200);
  194. }
  195. typename data_factory<R, false>::ptr_type r;
  196. typename data_factory<O1, false>::ptr_type a;
  197. typename data_factory<O2, false>::ptr_type b;
  198. typename data_factory<O3, false>::ptr_type c;
  199. };
  200. }}}}}
  201. #endif