1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_CONTAINER_MAPPED_VIEW_HPP
12 #define BOOST_COMPUTE_CONTAINER_MAPPED_VIEW_HPP
17 #include <boost/config.hpp>
18 #include <boost/throw_exception.hpp>
20 #include <boost/compute/buffer.hpp>
21 #include <boost/compute/system.hpp>
22 #include <boost/compute/context.hpp>
23 #include <boost/compute/command_queue.hpp>
24 #include <boost/compute/iterator/buffer_iterator.hpp>
29 /// \class mapped_view
30 /// \brief A mapped view of host memory.
32 /// The mapped_view class simplifies mapping host-memory to a compute
33 /// device. This allows for host-allocated memory to be used with the
34 /// Boost.Compute algorithms.
36 /// The following example shows how to map a simple C-array containing
37 /// data on the host to the device and run the reduce() algorithm to
38 /// calculate the sum:
40 /// \snippet test/test_mapped_view.cpp reduce
48 typedef size_t size_type;
49 typedef ptrdiff_t difference_type;
50 typedef buffer_iterator<T> iterator;
51 typedef buffer_iterator<T> const_iterator;
53 /// Creates a null mapped_view object.
59 /// Creates a mapped_view for \p host_ptr with \p n elements. After
60 /// constructing a mapped_view the data is available for use by a
61 /// compute device. Use the \p unmap() method to make the updated data
62 /// available to the host.
63 mapped_view(T *host_ptr,
65 const context &context = system::default_context())
66 : m_buffer(_make_mapped_buffer(host_ptr, n, context))
71 /// Creates a read-only mapped_view for \p host_ptr with \p n elements.
72 /// After constructing a mapped_view the data is available for use by a
73 /// compute device. Use the \p unmap() method to make the updated data
74 /// available to the host.
75 mapped_view(const T *host_ptr,
77 const context &context = system::default_context())
78 : m_buffer(_make_mapped_buffer(host_ptr, n, context))
83 /// Creates a copy of \p other.
84 mapped_view(const mapped_view<T> &other)
85 : m_buffer(other.m_buffer)
90 /// Copies the mapped buffer from \p other.
91 mapped_view<T>& operator=(const mapped_view<T> &other)
94 m_buffer = other.m_buffer;
101 /// Destroys the mapped_view object.
106 /// Returns an iterator to the first element in the mapped_view.
109 return ::boost::compute::make_buffer_iterator<T>(m_buffer, 0);
112 /// Returns a const_iterator to the first element in the mapped_view.
113 const_iterator begin() const
115 return ::boost::compute::make_buffer_iterator<T>(m_buffer, 0);
118 /// Returns a const_iterator to the first element in the mapped_view.
119 const_iterator cbegin() const
124 /// Returns an iterator to one past the last element in the mapped_view.
127 return ::boost::compute::make_buffer_iterator<T>(m_buffer, size());
130 /// Returns a const_iterator to one past the last element in the mapped_view.
131 const_iterator end() const
133 return ::boost::compute::make_buffer_iterator<T>(m_buffer, size());
136 /// Returns a const_iterator to one past the last element in the mapped_view.
137 const_iterator cend() const
142 /// Returns the number of elements in the mapped_view.
143 size_type size() const
145 return m_buffer.size() / sizeof(T);
148 /// Returns the host data pointer.
151 return static_cast<T *>(m_buffer.get_info<void *>(CL_MEM_HOST_PTR));
154 /// Returns the host data pointer.
155 const T* get_host_ptr() const
157 return static_cast<T *>(m_buffer.get_info<void *>(CL_MEM_HOST_PTR));
160 /// Resizes the mapped_view to \p size elements.
161 void resize(size_type size)
163 T *old_ptr = get_host_ptr();
165 m_buffer = _make_mapped_buffer(old_ptr, size, m_buffer.get_context());
168 /// Returns \c true if the mapped_view is empty.
174 /// Returns the mapped buffer.
175 const buffer& get_buffer() const
180 /// Maps the buffer into the host address space.
182 /// \see_opencl_ref{clEnqueueMapBuffer}
183 void map(cl_map_flags flags, command_queue &queue)
185 BOOST_ASSERT(m_mapped_ptr == 0);
187 m_mapped_ptr = queue.enqueue_map_buffer(
188 m_buffer, flags, 0, m_buffer.size()
192 /// Maps the buffer into the host address space for reading and writing.
196 /// map(CL_MAP_READ | CL_MAP_WRITE, queue);
198 void map(command_queue &queue)
200 map(CL_MAP_READ | CL_MAP_WRITE, queue);
203 /// Unmaps the buffer from the host address space.
205 /// \see_opencl_ref{clEnqueueUnmapMemObject}
206 void unmap(command_queue &queue)
208 BOOST_ASSERT(m_mapped_ptr != 0);
210 queue.enqueue_unmap_buffer(m_buffer, m_mapped_ptr);
217 static buffer _make_mapped_buffer(T *host_ptr,
219 const context &context)
224 buffer::read_write | buffer::use_host_ptr,
230 static buffer _make_mapped_buffer(const T *host_ptr,
232 const context &context)
237 buffer::read_only | buffer::use_host_ptr,
238 const_cast<void *>(static_cast<const void *>(host_ptr))
247 } // end compute namespace
248 } // end boost namespace
250 #endif // BOOST_COMPUTE_CONTAINER_MAPPED_VIEW_HPP