prod.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Boost.uBLAS
  2. //
  3. // Copyright (c) 2018 Fady Essam
  4. // Copyright (c) 2018 Stefan Seefeld
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or
  8. // copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef boost_numeric_ublas_opencl_prod_hpp_
  10. #define boost_numeric_ublas_opencl_prod_hpp_
  11. #include <boost/numeric/ublas/opencl/library.hpp>
  12. #include <boost/numeric/ublas/opencl/vector.hpp>
  13. #include <boost/numeric/ublas/opencl/matrix.hpp>
  14. #include <boost/numeric/ublas/opencl/transpose.hpp>
  15. #include <boost/compute/buffer.hpp>
  16. namespace boost { namespace numeric { namespace ublas { namespace opencl {
  17. #define ONE_DOUBLE_COMPLEX {{1.0, 00.0}}
  18. #define ONE_FLOAT_COMPLEX {{1.0f, 00.0f}}
  19. template <typename T, typename L1, typename L2>
  20. typename std::enable_if<is_numeric<T>::value>::type
  21. prod(ublas::matrix<T, L1, opencl::storage> const &a,
  22. ublas::matrix<T, L2, opencl::storage> const &b,
  23. ublas::matrix<T, L1, opencl::storage> &result,
  24. compute::command_queue &queue)
  25. {
  26. assert(a.device() == b.device() &&
  27. a.device() == result.device() &&
  28. a.device() == queue.get_device());
  29. assert(a.size2() == b.size1());
  30. result.fill(0, queue);
  31. //to hold matrix b with layout 1 if the b has different layout
  32. std::unique_ptr<ublas::matrix<T, L1, opencl::storage>> bl1;
  33. cl_event event = NULL;
  34. cl_mem buffer_a = a.begin().get_buffer().get();
  35. cl_mem buffer_b = b.begin().get_buffer().get();
  36. cl_mem buffer_result = result.begin().get_buffer().get();
  37. if (!(std::is_same<L1, L2>::value))
  38. {
  39. bl1.reset(new ublas::matrix<T, L1, opencl::storage>(b.size1(), b.size2(), queue.get_context()));
  40. change_layout(b, *bl1, queue);
  41. buffer_b = bl1->begin().get_buffer().get();
  42. }
  43. clblasOrder Order = std::is_same<L1, ublas::basic_row_major<> >::value ? clblasRowMajor : clblasColumnMajor;
  44. size_t lda = Order == clblasRowMajor ? a.size2() : a.size1();
  45. size_t ldb = Order == clblasRowMajor ? b.size2() : a.size2();
  46. size_t ldc = Order == clblasRowMajor ? b.size2() : a.size1();
  47. if (std::is_same<T, float>::value)
  48. clblasSgemm(Order, clblasNoTrans, clblasNoTrans,
  49. a.size1(), b.size2(), a.size2(),
  50. 1, buffer_a, 0, lda,
  51. buffer_b, 0, ldb, 1,
  52. buffer_result, 0, ldc,
  53. 1, &(queue.get()), 0, NULL, &event);
  54. else if (std::is_same<T, double>::value)
  55. clblasDgemm(Order, clblasNoTrans, clblasNoTrans,
  56. a.size1(), b.size2(), a.size2(),
  57. 1, buffer_a, 0, lda,
  58. buffer_b, 0, ldb, 1,
  59. buffer_result, 0, ldc,
  60. 1, &(queue.get()), 0, NULL, &event);
  61. else if (std::is_same<T, std::complex<float>>::value)
  62. clblasCgemm(Order, clblasNoTrans, clblasNoTrans,
  63. a.size1(), b.size2(), a.size2(),
  64. ONE_FLOAT_COMPLEX, buffer_a, 0, lda,
  65. buffer_b, 0, ldb, ONE_FLOAT_COMPLEX,
  66. buffer_result, 0, ldc,
  67. 1, &(queue.get()), 0, NULL, &event);
  68. else if (std::is_same<T, std::complex<double>>::value)
  69. clblasZgemm(Order, clblasNoTrans, clblasNoTrans,
  70. a.size1(), b.size2(), a.size2(),
  71. ONE_DOUBLE_COMPLEX, buffer_a, 0, lda,
  72. buffer_b, 0, ldb, ONE_DOUBLE_COMPLEX,
  73. buffer_result, 0, ldc,
  74. 1, &(queue.get()), 0, NULL, &event);
  75. clWaitForEvents(1, &event);
  76. }
  77. template <typename T, typename L1, typename L2, typename A>
  78. typename std::enable_if<is_numeric<T>::value>::type
  79. prod(ublas::matrix<T, L1, A> const &a,
  80. ublas::matrix<T, L2, A> const &b,
  81. ublas::matrix<T, L1, A> &result,
  82. compute::command_queue &queue)
  83. {
  84. ublas::matrix<T, L1, opencl::storage> adev(a, queue);
  85. ublas::matrix<T, L2, opencl::storage> bdev(b, queue);
  86. ublas::matrix<T, L1, opencl::storage> rdev(a.size1(), b.size2(), queue.get_context());
  87. prod(adev, bdev, rdev, queue);
  88. rdev.to_host(result,queue);
  89. }
  90. template <typename T, typename L1, typename L2, typename A>
  91. typename std::enable_if<is_numeric<T>::value, ublas::matrix<T, L1, A>>::type
  92. prod(ublas::matrix<T, L1, A> const &a,
  93. ublas::matrix<T, L2, A> const &b,
  94. compute::command_queue &queue)
  95. {
  96. ublas::matrix<T, L1, A> result(a.size1(), b.size2());
  97. prod(a, b, result, queue);
  98. return result;
  99. }
  100. template <typename T, typename L>
  101. typename std::enable_if<is_numeric<T>::value>::type
  102. prod(ublas::matrix<T, L, opencl::storage> const &a,
  103. ublas::vector<T, opencl::storage> const &b,
  104. ublas::vector<T, opencl::storage> &result,
  105. compute::command_queue &queue)
  106. {
  107. assert(a.device() == b.device() &&
  108. a.device() == result.device() &&
  109. a.device() == queue.get_device());
  110. assert(a.size2() == b.size());
  111. result.fill(0, queue);
  112. cl_event event = NULL;
  113. clblasOrder Order = std::is_same<L, ublas::basic_row_major<> >::value ? clblasRowMajor : clblasColumnMajor;
  114. int lda = Order == clblasRowMajor ? a.size2() : a.size1();
  115. int ldb = Order == clblasRowMajor ? 1 : a.size2();
  116. int ldc = Order == clblasRowMajor ? 1 : a.size1();
  117. if (std::is_same<T, float>::value)
  118. clblasSgemm(Order, clblasNoTrans, clblasNoTrans,
  119. a.size1(), 1, a.size2(),
  120. 1, a.begin().get_buffer().get(), 0, lda,
  121. b.begin().get_buffer().get(), 0, ldb, 1,
  122. result.begin().get_buffer().get(), 0, ldc,
  123. 1, &(queue.get()), 0, NULL, &event);
  124. else if (std::is_same<T, double>::value)
  125. clblasDgemm(Order, clblasNoTrans, clblasNoTrans,
  126. a.size1(), 1, a.size2(),
  127. 1, a.begin().get_buffer().get(), 0, lda,
  128. b.begin().get_buffer().get(), 0, ldb, 1,
  129. result.begin().get_buffer().get(), 0, ldc,
  130. 1, &(queue.get()), 0, NULL, &event);
  131. else if (std::is_same<T, std::complex<float>>::value)
  132. clblasCgemm(Order, clblasNoTrans, clblasNoTrans,
  133. a.size1(), 1, a.size2(),
  134. ONE_FLOAT_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  135. b.begin().get_buffer().get(), 0, ldb, ONE_FLOAT_COMPLEX,
  136. result.begin().get_buffer().get(), 0, ldc,
  137. 1, &(queue.get()), 0, NULL, &event);
  138. else if (std::is_same<T, std::complex<double>>::value)
  139. clblasZgemm(Order, clblasNoTrans, clblasNoTrans,
  140. a.size1(), 1, a.size2(),
  141. ONE_DOUBLE_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  142. b.begin().get_buffer().get(), 0, ldb, ONE_DOUBLE_COMPLEX,
  143. result.begin().get_buffer().get(), 0, ldc,
  144. 1, &(queue.get()), 0, NULL, &event);
  145. clWaitForEvents(1, &event);
  146. }
  147. template <typename T, typename L, typename A>
  148. typename std::enable_if<is_numeric<T>::value>::type
  149. prod(ublas::matrix<T, L, A> const &a,
  150. ublas::vector<T, A> const &b,
  151. ublas::vector<T, A> &result,
  152. compute::command_queue &queue)
  153. {
  154. ublas::matrix<T, L, opencl::storage> adev(a, queue);
  155. ublas::vector<T, opencl::storage> bdev(b, queue);
  156. ublas::vector<T, opencl::storage> rdev(a.size1(), queue.get_context());
  157. prod(adev, bdev, rdev, queue);
  158. rdev.to_host(result, queue);
  159. }
  160. template <typename T, typename L, typename A>
  161. typename std::enable_if<is_numeric<T>::value, ublas::vector<T, A>>::type
  162. prod(ublas::matrix<T, L, A> const &a,
  163. ublas::vector<T, A> const &b,
  164. compute::command_queue &queue)
  165. {
  166. ublas::vector<T, A> result(a.size1());
  167. prod(a, b, result, queue);
  168. return result;
  169. }
  170. template <typename T, typename L>
  171. typename std::enable_if<is_numeric<T>::value>::type
  172. prod(ublas::vector<T, opencl::storage> const &a,
  173. ublas::matrix<T, L, opencl::storage> const &b,
  174. ublas::vector<T, opencl::storage> &result,
  175. compute::command_queue &queue)
  176. {
  177. assert(a.device() == b.device() &&
  178. a.device() == result.device() &&
  179. a.device() == queue.get_device());
  180. assert(a.size() == b.size1());
  181. result.fill(0, queue);
  182. cl_event event = NULL;
  183. clblasOrder Order = std::is_same<L, ublas::basic_row_major<> >::value ? clblasRowMajor : clblasColumnMajor;
  184. size_t lda = Order == clblasRowMajor ? a.size() : 1;
  185. size_t ldb = Order == clblasRowMajor ? b.size2() : a.size();
  186. size_t ldc = Order == clblasRowMajor ? b.size2() : 1;
  187. if (std::is_same<T, float>::value)
  188. clblasSgemm(Order, clblasNoTrans, clblasNoTrans,
  189. 1, b.size2(), a.size(),
  190. 1, a.begin().get_buffer().get(), 0, lda,
  191. b.begin().get_buffer().get(), 0, ldb, 1,
  192. result.begin().get_buffer().get(), 0, ldc,
  193. 1, &(queue.get()), 0, NULL, &event);
  194. else if (std::is_same<T, double>::value)
  195. clblasDgemm(Order, clblasNoTrans, clblasNoTrans,
  196. 1, b.size2(), a.size(),
  197. 1, a.begin().get_buffer().get(), 0, lda,
  198. b.begin().get_buffer().get(), 0, ldb, 1,
  199. result.begin().get_buffer().get(), 0, ldc,
  200. 1, &(queue.get()), 0, NULL, &event);
  201. else if (std::is_same<T, std::complex<float>>::value)
  202. clblasCgemm(Order, clblasNoTrans, clblasNoTrans,
  203. 1, b.size2(), a.size(),
  204. ONE_FLOAT_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  205. b.begin().get_buffer().get(), 0, ldb, ONE_FLOAT_COMPLEX,
  206. result.begin().get_buffer().get(), 0, ldc,
  207. 1, &(queue.get()), 0, NULL, &event);
  208. else if (std::is_same<T, std::complex<double>>::value)
  209. clblasZgemm(Order, clblasNoTrans, clblasNoTrans,
  210. 1, b.size2(), a.size(),
  211. ONE_DOUBLE_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  212. b.begin().get_buffer().get(), 0, ldb, ONE_DOUBLE_COMPLEX,
  213. result.begin().get_buffer().get(), 0, ldc,
  214. 1, &(queue.get()), 0, NULL, &event);
  215. clWaitForEvents(1, &event);
  216. }
  217. template <class T, class L, class A>
  218. typename std::enable_if<is_numeric<T>::value>::type
  219. prod(ublas::vector<T, A> const &a,
  220. ublas::matrix<T, L, A> const &b,
  221. ublas::vector<T, A> &result,
  222. compute::command_queue &queue)
  223. {
  224. ublas::vector<T, opencl::storage> adev(a, queue);
  225. ublas::matrix<T, L, opencl::storage> bdev(b, queue);
  226. ublas::vector<T, opencl::storage> rdev(b.size2(), queue.get_context());
  227. prod(adev, bdev, rdev, queue);
  228. rdev.to_host(result, queue);
  229. }
  230. template <class T, class L, class A>
  231. typename std::enable_if<is_numeric<T>::value, ublas::vector<T, A>>::type
  232. prod(ublas::vector<T, A> const &a,
  233. ublas::matrix<T, L, A> const &b,
  234. compute::command_queue &queue)
  235. {
  236. ublas::vector<T, A> result(b.size2());
  237. prod(a, b, result, queue);
  238. return result;
  239. }
  240. template<class T>
  241. typename std::enable_if<std::is_fundamental<T>::value, T>::type
  242. inner_prod(ublas::vector<T, opencl::storage> const &a,
  243. ublas::vector<T, opencl::storage> const &b,
  244. compute::command_queue &queue)
  245. {
  246. assert(a.device() == b.device() && a.device() == queue.get_device());
  247. assert(a.size() == b.size());
  248. return compute::inner_product(a.begin(), a.end(), b.begin(), T(0), queue);
  249. }
  250. template<class T, class A>
  251. typename std::enable_if<std::is_fundamental<T>::value, T>::type
  252. inner_prod(ublas::vector<T, A> const &a,
  253. ublas::vector<T, A> const &b,
  254. compute::command_queue& queue)
  255. {
  256. ublas::vector<T, opencl::storage> adev(a, queue);
  257. ublas::vector<T, opencl::storage> bdev(b, queue);
  258. return inner_prod(adev, bdev, queue);
  259. }
  260. template <class T, class L>
  261. typename std::enable_if<is_numeric<T>::value>::type
  262. outer_prod(ublas::vector<T, opencl::storage> const &a,
  263. ublas::vector<T, opencl::storage> const &b,
  264. ublas::matrix<T, L, opencl::storage> &result,
  265. compute::command_queue & queue)
  266. {
  267. assert(a.device() == b.device() &&
  268. a.device() == result.device() &&
  269. a.device() == queue.get_device());
  270. result.fill(0, queue);
  271. cl_event event = NULL;
  272. clblasOrder Order = std::is_same<L, ublas::basic_row_major<> >::value ? clblasRowMajor : clblasColumnMajor;
  273. size_t lda = Order == clblasRowMajor ? 1 : a.size();
  274. size_t ldb = Order == clblasRowMajor ? b.size() : 1;
  275. size_t ldc = Order == clblasRowMajor ? b.size() : a.size();
  276. if (std::is_same<T, float>::value)
  277. clblasSgemm(Order, clblasNoTrans, clblasNoTrans,
  278. a.size(), b.size(), 1,
  279. 1, a.begin().get_buffer().get(), 0, lda,
  280. b.begin().get_buffer().get(), 0, ldb, 1,
  281. result.begin().get_buffer().get(), 0, ldc,
  282. 1, &(queue.get()), 0, NULL, &event);
  283. else if (std::is_same<T, double>::value)
  284. clblasDgemm(Order, clblasNoTrans, clblasNoTrans,
  285. a.size(), b.size(), 1,
  286. 1, a.begin().get_buffer().get(), 0, lda,
  287. b.begin().get_buffer().get(), 0, ldb, 1,
  288. result.begin().get_buffer().get(), 0, ldc,
  289. 1, &(queue.get()), 0, NULL, &event);
  290. else if (std::is_same<T, std::complex<float>>::value)
  291. clblasCgemm(Order, clblasNoTrans, clblasNoTrans,
  292. a.size(), b.size(), 1,
  293. ONE_FLOAT_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  294. b.begin().get_buffer().get(), 0, ldb, ONE_FLOAT_COMPLEX,
  295. result.begin().get_buffer().get(), 0, ldc,
  296. 1, &(queue.get()), 0, NULL, &event);
  297. else if (std::is_same<T, std::complex<double>>::value)
  298. clblasZgemm(Order, clblasNoTrans, clblasNoTrans,
  299. a.size(), b.size(), 1,
  300. ONE_DOUBLE_COMPLEX, a.begin().get_buffer().get(), 0, lda,
  301. b.begin().get_buffer().get(), 0, ldb, ONE_DOUBLE_COMPLEX,
  302. result.begin().get_buffer().get(), 0, ldc,
  303. 1, &(queue.get()), 0, NULL, &event);
  304. clWaitForEvents(1, &event);
  305. }
  306. template <class T, class L, class A>
  307. typename std::enable_if<is_numeric<T>::value>::type
  308. outer_prod(ublas::vector<T, A> const &a,
  309. ublas::vector<T, A> const &b,
  310. ublas::matrix<T, L, A> &result,
  311. compute::command_queue &queue)
  312. {
  313. ublas::vector<T, opencl::storage> adev(a, queue);
  314. ublas::vector<T, opencl::storage> bdev(b, queue);
  315. ublas::matrix<T, L, opencl::storage> rdev(a.size(), b.size(), queue.get_context());
  316. outer_prod(adev, bdev, rdev, queue);
  317. rdev.to_host(result, queue);
  318. }
  319. template <class T,class L = ublas::basic_row_major<>, class A>
  320. typename std::enable_if<is_numeric<T>::value, ublas::matrix<T, L, A>>::type
  321. outer_prod(ublas::vector<T, A> const &a,
  322. ublas::vector<T, A> const &b,
  323. compute::command_queue &queue)
  324. {
  325. ublas::matrix<T, L, A> result(a.size(), b.size());
  326. outer_prod(a, b, result, queue);
  327. return result;
  328. }
  329. #undef ONE_DOUBLE_COMPLEX
  330. #undef ONE_FLOAT_COMPLEX
  331. }}}}
  332. #endif