mapped_view.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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_MAPPED_VIEW_HPP
  11. #define BOOST_COMPUTE_CONTAINER_MAPPED_VIEW_HPP
  12. #include <cstddef>
  13. #include <exception>
  14. #include <boost/config.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/compute/buffer.hpp>
  17. #include <boost/compute/system.hpp>
  18. #include <boost/compute/context.hpp>
  19. #include <boost/compute/command_queue.hpp>
  20. #include <boost/compute/iterator/buffer_iterator.hpp>
  21. namespace boost {
  22. namespace compute {
  23. /// \class mapped_view
  24. /// \brief A mapped view of host memory.
  25. ///
  26. /// The mapped_view class simplifies mapping host-memory to a compute
  27. /// device. This allows for host-allocated memory to be used with the
  28. /// Boost.Compute algorithms.
  29. ///
  30. /// The following example shows how to map a simple C-array containing
  31. /// data on the host to the device and run the reduce() algorithm to
  32. /// calculate the sum:
  33. ///
  34. /// \snippet test/test_mapped_view.cpp reduce
  35. ///
  36. /// \see buffer
  37. template<class T>
  38. class mapped_view
  39. {
  40. public:
  41. typedef T value_type;
  42. typedef size_t size_type;
  43. typedef ptrdiff_t difference_type;
  44. typedef buffer_iterator<T> iterator;
  45. typedef buffer_iterator<T> const_iterator;
  46. /// Creates a null mapped_view object.
  47. mapped_view()
  48. {
  49. m_mapped_ptr = 0;
  50. }
  51. /// Creates a mapped_view for \p host_ptr with \p n elements. After
  52. /// constructing a mapped_view the data is available for use by a
  53. /// compute device. Use the \p unmap() method to make the updated data
  54. /// available to the host.
  55. mapped_view(T *host_ptr,
  56. size_type n,
  57. const context &context = system::default_context())
  58. : m_buffer(_make_mapped_buffer(host_ptr, n, context))
  59. {
  60. m_mapped_ptr = 0;
  61. }
  62. /// Creates a read-only mapped_view for \p host_ptr with \p n elements.
  63. /// After constructing a mapped_view the data is available for use by a
  64. /// compute device. Use the \p unmap() method to make the updated data
  65. /// available to the host.
  66. mapped_view(const T *host_ptr,
  67. size_type n,
  68. const context &context = system::default_context())
  69. : m_buffer(_make_mapped_buffer(host_ptr, n, context))
  70. {
  71. m_mapped_ptr = 0;
  72. }
  73. /// Creates a copy of \p other.
  74. mapped_view(const mapped_view<T> &other)
  75. : m_buffer(other.m_buffer)
  76. {
  77. m_mapped_ptr = 0;
  78. }
  79. /// Copies the mapped buffer from \p other.
  80. mapped_view<T>& operator=(const mapped_view<T> &other)
  81. {
  82. if(this != &other){
  83. m_buffer = other.m_buffer;
  84. m_mapped_ptr = 0;
  85. }
  86. return *this;
  87. }
  88. /// Destroys the mapped_view object.
  89. ~mapped_view()
  90. {
  91. }
  92. /// Returns an iterator to the first element in the mapped_view.
  93. iterator begin()
  94. {
  95. return ::boost::compute::make_buffer_iterator<T>(m_buffer, 0);
  96. }
  97. /// Returns a const_iterator to the first element in the mapped_view.
  98. const_iterator begin() const
  99. {
  100. return ::boost::compute::make_buffer_iterator<T>(m_buffer, 0);
  101. }
  102. /// Returns a const_iterator to the first element in the mapped_view.
  103. const_iterator cbegin() const
  104. {
  105. return begin();
  106. }
  107. /// Returns an iterator to one past the last element in the mapped_view.
  108. iterator end()
  109. {
  110. return ::boost::compute::make_buffer_iterator<T>(m_buffer, size());
  111. }
  112. /// Returns a const_iterator to one past the last element in the mapped_view.
  113. const_iterator end() const
  114. {
  115. return ::boost::compute::make_buffer_iterator<T>(m_buffer, size());
  116. }
  117. /// Returns a const_iterator to one past the last element in the mapped_view.
  118. const_iterator cend() const
  119. {
  120. return end();
  121. }
  122. /// Returns the number of elements in the mapped_view.
  123. size_type size() const
  124. {
  125. return m_buffer.size() / sizeof(T);
  126. }
  127. /// Returns the host data pointer.
  128. T* get_host_ptr()
  129. {
  130. return static_cast<T *>(m_buffer.get_info<void *>(CL_MEM_HOST_PTR));
  131. }
  132. /// Returns the host data pointer.
  133. const T* get_host_ptr() const
  134. {
  135. return static_cast<T *>(m_buffer.get_info<void *>(CL_MEM_HOST_PTR));
  136. }
  137. /// Resizes the mapped_view to \p size elements.
  138. void resize(size_type size)
  139. {
  140. T *old_ptr = get_host_ptr();
  141. m_buffer = _make_mapped_buffer(old_ptr, size, m_buffer.get_context());
  142. }
  143. /// Returns \c true if the mapped_view is empty.
  144. bool empty() const
  145. {
  146. return size() == 0;
  147. }
  148. /// Returns the mapped buffer.
  149. const buffer& get_buffer() const
  150. {
  151. return m_buffer;
  152. }
  153. /// Maps the buffer into the host address space.
  154. ///
  155. /// \see_opencl_ref{clEnqueueMapBuffer}
  156. void map(cl_map_flags flags, command_queue &queue)
  157. {
  158. BOOST_ASSERT(m_mapped_ptr == 0);
  159. m_mapped_ptr = queue.enqueue_map_buffer(
  160. m_buffer, flags, 0, m_buffer.size()
  161. );
  162. }
  163. /// Maps the buffer into the host address space for reading and writing.
  164. ///
  165. /// Equivalent to:
  166. /// \code
  167. /// map(CL_MAP_READ | CL_MAP_WRITE, queue);
  168. /// \endcode
  169. void map(command_queue &queue)
  170. {
  171. map(CL_MAP_READ | CL_MAP_WRITE, queue);
  172. }
  173. /// Unmaps the buffer from the host address space.
  174. ///
  175. /// \see_opencl_ref{clEnqueueUnmapMemObject}
  176. void unmap(command_queue &queue)
  177. {
  178. BOOST_ASSERT(m_mapped_ptr != 0);
  179. queue.enqueue_unmap_buffer(m_buffer, m_mapped_ptr);
  180. m_mapped_ptr = 0;
  181. }
  182. private:
  183. /// \internal_
  184. static buffer _make_mapped_buffer(T *host_ptr,
  185. size_t n,
  186. const context &context)
  187. {
  188. return buffer(
  189. context,
  190. n * sizeof(T),
  191. buffer::read_write | buffer::use_host_ptr,
  192. host_ptr
  193. );
  194. }
  195. /// \internal_
  196. static buffer _make_mapped_buffer(const T *host_ptr,
  197. size_t n,
  198. const context &context)
  199. {
  200. return buffer(
  201. context,
  202. n * sizeof(T),
  203. buffer::read_only | buffer::use_host_ptr,
  204. const_cast<void *>(static_cast<const void *>(host_ptr))
  205. );
  206. }
  207. private:
  208. buffer m_buffer;
  209. void *m_mapped_ptr;
  210. };
  211. } // end compute namespace
  212. } // end boost namespace
  213. #endif // BOOST_COMPUTE_CONTAINER_MAPPED_VIEW_HPP