valarray.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_CONTAINER_VALARRAY_HPP
  11. #define BOOST_COMPUTE_CONTAINER_VALARRAY_HPP
  12. #include <cstddef>
  13. #include <valarray>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/type_traits.hpp>
  16. #include <boost/compute/buffer.hpp>
  17. #include <boost/compute/algorithm/copy.hpp>
  18. #include <boost/compute/algorithm/fill.hpp>
  19. #include <boost/compute/algorithm/max_element.hpp>
  20. #include <boost/compute/algorithm/min_element.hpp>
  21. #include <boost/compute/algorithm/transform.hpp>
  22. #include <boost/compute/algorithm/accumulate.hpp>
  23. #include <boost/compute/detail/buffer_value.hpp>
  24. #include <boost/compute/functional.hpp>
  25. #include <boost/compute/functional/bind.hpp>
  26. #include <boost/compute/iterator/buffer_iterator.hpp>
  27. #include <boost/compute/type_traits.hpp>
  28. namespace boost {
  29. namespace compute {
  30. template<class T>
  31. class valarray
  32. {
  33. public:
  34. explicit valarray(const context &context = system::default_context())
  35. : m_buffer(context, 0)
  36. {
  37. }
  38. explicit valarray(size_t size,
  39. const context &context = system::default_context())
  40. : m_buffer(context, size * sizeof(T))
  41. {
  42. }
  43. valarray(const T &value,
  44. size_t size,
  45. const context &context = system::default_context())
  46. : m_buffer(context, size * sizeof(T))
  47. {
  48. fill(begin(), end(), value);
  49. }
  50. valarray(const T *values,
  51. size_t size,
  52. const context &context = system::default_context())
  53. : m_buffer(context, size * sizeof(T))
  54. {
  55. copy(values, values + size, begin());
  56. }
  57. valarray(const valarray<T> &other)
  58. : m_buffer(other.m_buffer.get_context(), other.size() * sizeof(T))
  59. {
  60. copy(other.begin(), other.end(), begin());
  61. }
  62. valarray(const std::valarray<T> &valarray,
  63. const context &context = system::default_context())
  64. : m_buffer(context, valarray.size() * sizeof(T))
  65. {
  66. copy(&valarray[0], &valarray[valarray.size()], begin());
  67. }
  68. valarray<T>& operator=(const valarray<T> &other)
  69. {
  70. if(this != &other){
  71. // change to other's OpenCL context
  72. m_buffer = buffer(other.m_buffer.get_context(), other.size() * sizeof(T));
  73. copy(other.begin(), other.end(), begin());
  74. }
  75. return *this;
  76. }
  77. valarray<T>& operator=(const std::valarray<T> &valarray)
  78. {
  79. m_buffer = buffer(m_buffer.get_context(), valarray.size() * sizeof(T));
  80. copy(&valarray[0], &valarray[valarray.size()], begin());
  81. return *this;
  82. }
  83. valarray<T>& operator*=(const T&);
  84. valarray<T>& operator/=(const T&);
  85. valarray<T>& operator%=(const T& val);
  86. valarray<T> operator+() const
  87. {
  88. // This operator can be used with any type.
  89. valarray<T> result(size());
  90. copy(begin(), end(), result.begin());
  91. return result;
  92. }
  93. valarray<T> operator-() const
  94. {
  95. BOOST_STATIC_ASSERT_MSG(
  96. is_fundamental<T>::value,
  97. "This operator can be used with all OpenCL built-in scalar"
  98. " and vector types"
  99. );
  100. valarray<T> result(size());
  101. BOOST_COMPUTE_FUNCTION(T, unary_minus, (T x),
  102. {
  103. return -x;
  104. });
  105. transform(begin(), end(), result.begin(), unary_minus);
  106. return result;
  107. }
  108. valarray<T> operator~() const
  109. {
  110. BOOST_STATIC_ASSERT_MSG(
  111. is_fundamental<T>::value &&
  112. !is_floating_point<typename scalar_type<T>::type>::value,
  113. "This operator can be used with all OpenCL built-in scalar"
  114. " and vector types except the built-in scalar and vector float types"
  115. );
  116. valarray<T> result(size());
  117. BOOST_COMPUTE_FUNCTION(T, bitwise_not, (T x),
  118. {
  119. return ~x;
  120. });
  121. transform(begin(), end(), result.begin(), bitwise_not);
  122. return result;
  123. }
  124. /// In OpenCL there cannot be memory buffer with bool type, for
  125. /// this reason return type is valarray<char> instead of valarray<bool>.
  126. /// 1 means true, 0 means false.
  127. valarray<char> operator!() const
  128. {
  129. BOOST_STATIC_ASSERT_MSG(
  130. is_fundamental<T>::value,
  131. "This operator can be used with all OpenCL built-in scalar"
  132. " and vector types"
  133. );
  134. valarray<char> result(size());
  135. BOOST_COMPUTE_FUNCTION(char, logical_not, (T x),
  136. {
  137. return !x;
  138. });
  139. transform(begin(), end(), &result[0], logical_not);
  140. return result;
  141. }
  142. valarray<T>& operator+=(const T&);
  143. valarray<T>& operator-=(const T&);
  144. valarray<T>& operator^=(const T&);
  145. valarray<T>& operator&=(const T&);
  146. valarray<T>& operator|=(const T&);
  147. valarray<T>& operator<<=(const T&);
  148. valarray<T>& operator>>=(const T&);
  149. valarray<T>& operator*=(const valarray<T>&);
  150. valarray<T>& operator/=(const valarray<T>&);
  151. valarray<T>& operator%=(const valarray<T>&);
  152. valarray<T>& operator+=(const valarray<T>&);
  153. valarray<T>& operator-=(const valarray<T>&);
  154. valarray<T>& operator^=(const valarray<T>&);
  155. valarray<T>& operator&=(const valarray<T>&);
  156. valarray<T>& operator|=(const valarray<T>&);
  157. valarray<T>& operator<<=(const valarray<T>&);
  158. valarray<T>& operator>>=(const valarray<T>&);
  159. ~valarray()
  160. {
  161. }
  162. size_t size() const
  163. {
  164. return m_buffer.size() / sizeof(T);
  165. }
  166. void resize(size_t size, T value = T())
  167. {
  168. m_buffer = buffer(m_buffer.get_context(), size * sizeof(T));
  169. fill(begin(), end(), value);
  170. }
  171. detail::buffer_value<T> operator[](size_t index)
  172. {
  173. return *(begin() + static_cast<ptrdiff_t>(index));
  174. }
  175. const detail::buffer_value<T> operator[](size_t index) const
  176. {
  177. return *(begin() + static_cast<ptrdiff_t>(index));
  178. }
  179. T (min)() const
  180. {
  181. return *(boost::compute::min_element(begin(), end()));
  182. }
  183. T (max)() const
  184. {
  185. return *(boost::compute::max_element(begin(), end()));
  186. }
  187. T sum() const
  188. {
  189. return boost::compute::accumulate(begin(), end(), T(0));
  190. }
  191. template<class UnaryFunction>
  192. valarray<T> apply(UnaryFunction function) const
  193. {
  194. valarray<T> result(size());
  195. transform(begin(), end(), result.begin(), function);
  196. return result;
  197. }
  198. const buffer& get_buffer() const
  199. {
  200. return m_buffer;
  201. }
  202. private:
  203. buffer_iterator<T> begin() const
  204. {
  205. return buffer_iterator<T>(m_buffer, 0);
  206. }
  207. buffer_iterator<T> end() const
  208. {
  209. return buffer_iterator<T>(m_buffer, size());
  210. }
  211. private:
  212. buffer m_buffer;
  213. };
  214. /// \internal_
  215. #define BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT(op, op_name, assert) \
  216. template<class T> \
  217. inline valarray<T>& \
  218. valarray<T>::operator op##=(const T& val) \
  219. { \
  220. assert \
  221. transform(begin(), end(), begin(), \
  222. ::boost::compute::bind(op_name<T>(), placeholders::_1, val)); \
  223. return *this; \
  224. } \
  225. \
  226. template<class T> \
  227. inline valarray<T>& \
  228. valarray<T>::operator op##=(const valarray<T> &rhs) \
  229. { \
  230. assert \
  231. transform(begin(), end(), rhs.begin(), begin(), op_name<T>()); \
  232. return *this; \
  233. }
  234. /// \internal_
  235. #define BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY(op, op_name) \
  236. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT(op, op_name, \
  237. BOOST_STATIC_ASSERT_MSG( \
  238. is_fundamental<T>::value, \
  239. "This operator can be used with all OpenCL built-in scalar" \
  240. " and vector types" \
  241. ); \
  242. )
  243. /// \internal_
  244. /// For some operators class T can't be floating point type.
  245. /// See OpenCL specification, operators chapter.
  246. #define BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(op, op_name) \
  247. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT(op, op_name, \
  248. BOOST_STATIC_ASSERT_MSG( \
  249. is_fundamental<T>::value && \
  250. !is_floating_point<typename scalar_type<T>::type>::value, \
  251. "This operator can be used with all OpenCL built-in scalar" \
  252. " and vector types except the built-in scalar and vector float types" \
  253. ); \
  254. )
  255. // defining operators
  256. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY(+, plus)
  257. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY(-, minus)
  258. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY(*, multiplies)
  259. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY(/, divides)
  260. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(^, bit_xor)
  261. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(&, bit_and)
  262. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(|, bit_or)
  263. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(<<, shift_left)
  264. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP(>>, shift_right)
  265. // The remainder (%) operates on
  266. // integer scalar and integer vector data types only.
  267. // See OpenCL specification.
  268. BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT(%, modulus,
  269. BOOST_STATIC_ASSERT_MSG(
  270. is_integral<typename scalar_type<T>::type>::value,
  271. "This operator can be used only with OpenCL built-in integer types"
  272. );
  273. )
  274. #undef BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_ANY
  275. #undef BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT_NO_FP
  276. #undef BOOST_COMPUTE_DEFINE_VALARRAY_COMPOUND_ASSIGNMENT
  277. /// \internal_
  278. /// Macro for defining binary operators for valarray
  279. #define BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR(op, op_name, assert) \
  280. template<class T> \
  281. valarray<T> operator op (const valarray<T>& lhs, const valarray<T>& rhs) \
  282. { \
  283. assert \
  284. valarray<T> result(lhs.size()); \
  285. transform(buffer_iterator<T>(lhs.get_buffer(), 0), \
  286. buffer_iterator<T>(lhs.get_buffer(), lhs.size()), \
  287. buffer_iterator<T>(rhs.get_buffer(), 0), \
  288. buffer_iterator<T>(result.get_buffer(), 0), \
  289. op_name<T>()); \
  290. return result; \
  291. } \
  292. \
  293. template<class T> \
  294. valarray<T> operator op (const T& val, const valarray<T>& rhs) \
  295. { \
  296. assert \
  297. valarray<T> result(rhs.size()); \
  298. transform(buffer_iterator<T>(rhs.get_buffer(), 0), \
  299. buffer_iterator<T>(rhs.get_buffer(), rhs.size()), \
  300. buffer_iterator<T>(result.get_buffer(), 0), \
  301. ::boost::compute::bind(op_name<T>(), val, placeholders::_1)); \
  302. return result; \
  303. } \
  304. \
  305. template<class T> \
  306. valarray<T> operator op (const valarray<T>& lhs, const T& val) \
  307. { \
  308. assert \
  309. valarray<T> result(lhs.size()); \
  310. transform(buffer_iterator<T>(lhs.get_buffer(), 0), \
  311. buffer_iterator<T>(lhs.get_buffer(), lhs.size()), \
  312. buffer_iterator<T>(result.get_buffer(), 0), \
  313. ::boost::compute::bind(op_name<T>(), placeholders::_1, val)); \
  314. return result; \
  315. }
  316. /// \internal_
  317. #define BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY(op, op_name) \
  318. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR(op, op_name, \
  319. BOOST_STATIC_ASSERT_MSG( \
  320. is_fundamental<T>::value, \
  321. "This operator can be used with all OpenCL built-in scalar" \
  322. " and vector types" \
  323. ); \
  324. )
  325. /// \internal_
  326. /// For some operators class T can't be floating point type.
  327. /// See OpenCL specification, operators chapter.
  328. #define BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(op, op_name) \
  329. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR(op, op_name, \
  330. BOOST_STATIC_ASSERT_MSG( \
  331. is_fundamental<T>::value && \
  332. !is_floating_point<typename scalar_type<T>::type>::value, \
  333. "This operator can be used with all OpenCL built-in scalar" \
  334. " and vector types except the built-in scalar and vector float types" \
  335. ); \
  336. )
  337. // defining binary operators for valarray
  338. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY(+, plus)
  339. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY(-, minus)
  340. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY(*, multiplies)
  341. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY(/, divides)
  342. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(^, bit_xor)
  343. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(&, bit_and)
  344. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(|, bit_or)
  345. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(<<, shift_left)
  346. BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP(>>, shift_right)
  347. #undef BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_ANY
  348. #undef BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR_NO_FP
  349. #undef BOOST_COMPUTE_DEFINE_VALARRAY_BINARY_OPERATOR
  350. /// \internal_
  351. /// Macro for defining valarray comparison operators.
  352. /// For return type valarray<char> is used instead of valarray<bool> because
  353. /// in OpenCL there cannot be memory buffer with bool type.
  354. ///
  355. /// Note it's also used for defining binary logical operators (==, &&)
  356. #define BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(op, op_name) \
  357. template<class T> \
  358. valarray<char> operator op (const valarray<T>& lhs, const valarray<T>& rhs) \
  359. { \
  360. BOOST_STATIC_ASSERT_MSG( \
  361. is_fundamental<T>::value, \
  362. "This operator can be used with all OpenCL built-in scalar" \
  363. " and vector types" \
  364. ); \
  365. valarray<char> result(lhs.size()); \
  366. transform(buffer_iterator<T>(lhs.get_buffer(), 0), \
  367. buffer_iterator<T>(lhs.get_buffer(), lhs.size()), \
  368. buffer_iterator<T>(rhs.get_buffer(), 0), \
  369. buffer_iterator<char>(result.get_buffer(), 0), \
  370. op_name<T>()); \
  371. return result; \
  372. } \
  373. \
  374. template<class T> \
  375. valarray<char> operator op (const T& val, const valarray<T>& rhs) \
  376. { \
  377. BOOST_STATIC_ASSERT_MSG( \
  378. is_fundamental<T>::value, \
  379. "This operator can be used with all OpenCL built-in scalar" \
  380. " and vector types" \
  381. ); \
  382. valarray<char> result(rhs.size()); \
  383. transform(buffer_iterator<T>(rhs.get_buffer(), 0), \
  384. buffer_iterator<T>(rhs.get_buffer(), rhs.size()), \
  385. buffer_iterator<char>(result.get_buffer(), 0), \
  386. ::boost::compute::bind(op_name<T>(), val, placeholders::_1)); \
  387. return result; \
  388. } \
  389. \
  390. template<class T> \
  391. valarray<char> operator op (const valarray<T>& lhs, const T& val) \
  392. { \
  393. BOOST_STATIC_ASSERT_MSG( \
  394. is_fundamental<T>::value, \
  395. "This operator can be used with all OpenCL built-in scalar" \
  396. " and vector types" \
  397. ); \
  398. valarray<char> result(lhs.size()); \
  399. transform(buffer_iterator<T>(lhs.get_buffer(), 0), \
  400. buffer_iterator<T>(lhs.get_buffer(), lhs.size()), \
  401. buffer_iterator<char>(result.get_buffer(), 0), \
  402. ::boost::compute::bind(op_name<T>(), placeholders::_1, val)); \
  403. return result; \
  404. }
  405. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(==, equal_to)
  406. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(!=, not_equal_to)
  407. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(>, greater)
  408. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(<, less)
  409. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(>=, greater_equal)
  410. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(<=, less_equal)
  411. /// \internal_
  412. /// Macro for defining binary logical operators for valarray.
  413. ///
  414. /// For return type valarray<char> is used instead of valarray<bool> because
  415. /// in OpenCL there cannot be memory buffer with bool type.
  416. /// 1 means true, 0 means false.
  417. #define BOOST_COMPUTE_DEFINE_VALARRAY_LOGICAL_OPERATOR(op, op_name) \
  418. BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR(op, op_name)
  419. BOOST_COMPUTE_DEFINE_VALARRAY_LOGICAL_OPERATOR(&&, logical_and)
  420. BOOST_COMPUTE_DEFINE_VALARRAY_LOGICAL_OPERATOR(||, logical_or)
  421. #undef BOOST_COMPUTE_DEFINE_VALARRAY_LOGICAL_OPERATOR
  422. #undef BOOST_COMPUTE_DEFINE_VALARRAY_COMPARISON_OPERATOR
  423. } // end compute namespace
  424. } // end boost namespace
  425. #endif // BOOST_COMPUTE_CONTAINER_VALARRAY_HPP