123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- #ifndef BOOST_COMPUTE_BUFFER_HPP
- #define BOOST_COMPUTE_BUFFER_HPP
- #include <boost/compute/config.hpp>
- #include <boost/compute/context.hpp>
- #include <boost/compute/exception.hpp>
- #include <boost/compute/memory_object.hpp>
- #include <boost/compute/detail/get_object_info.hpp>
- namespace boost {
- namespace compute {
- class command_queue;
- class buffer : public memory_object
- {
- public:
-
- buffer()
- : memory_object()
- {
- }
-
-
- explicit buffer(cl_mem mem, bool retain = true)
- : memory_object(mem, retain)
- {
- }
-
-
-
-
- buffer(const context &context,
- size_t size,
- cl_mem_flags flags = read_write,
- void *host_ptr = 0)
- {
- cl_int error = 0;
- m_mem = clCreateBuffer(context,
- flags,
- (std::max)(size, size_t(1)),
- host_ptr,
- &error);
- if(!m_mem){
- BOOST_THROW_EXCEPTION(opencl_error(error));
- }
- }
-
- buffer(const buffer &other)
- : memory_object(other)
- {
- }
-
- buffer& operator=(const buffer &other)
- {
- if(this != &other){
- memory_object::operator=(other);
- }
- return *this;
- }
- #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
-
- buffer(buffer&& other) BOOST_NOEXCEPT
- : memory_object(std::move(other))
- {
- }
-
- buffer& operator=(buffer&& other) BOOST_NOEXCEPT
- {
- memory_object::operator=(std::move(other));
- return *this;
- }
- #endif
-
- ~buffer()
- {
- }
-
- size_t size() const
- {
- return get_memory_size();
- }
-
- size_t max_size() const
- {
- return get_context().get_device().max_memory_alloc_size();
- }
-
-
-
- template<class T>
- T get_info(cl_mem_info info) const
- {
- return get_memory_info<T>(info);
- }
-
- template<int Enum>
- typename detail::get_object_info_type<buffer, Enum>::type
- get_info() const;
-
-
- buffer clone(command_queue &queue) const;
- #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
-
-
- buffer create_subbuffer(cl_mem_flags flags, size_t origin,
- size_t size)
- {
- BOOST_ASSERT(origin + size <= this->size());
- BOOST_ASSERT(origin % (get_context().
- get_device().
- get_info<CL_DEVICE_MEM_BASE_ADDR_ALIGN>() / 8) == 0);
- cl_int error = 0;
- cl_buffer_region region = { origin, size };
- cl_mem mem = clCreateSubBuffer(m_mem,
- flags,
- CL_BUFFER_CREATE_TYPE_REGION,
- ®ion,
- &error);
- if(!mem){
- BOOST_THROW_EXCEPTION(opencl_error(error));
- }
- return buffer(mem, false);
- }
- #endif
- };
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(buffer,
- ((cl_mem_object_type, CL_MEM_TYPE))
- ((cl_mem_flags, CL_MEM_FLAGS))
- ((size_t, CL_MEM_SIZE))
- ((void *, CL_MEM_HOST_PTR))
- ((cl_uint, CL_MEM_MAP_COUNT))
- ((cl_uint, CL_MEM_REFERENCE_COUNT))
- ((cl_context, CL_MEM_CONTEXT))
- )
- #ifdef BOOST_COMPUTE_CL_VERSION_1_1
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(buffer,
- ((cl_mem, CL_MEM_ASSOCIATED_MEMOBJECT))
- ((size_t, CL_MEM_OFFSET))
- )
- #endif
- namespace detail {
- template<>
- struct set_kernel_arg<buffer>
- {
- void operator()(kernel &kernel_, size_t index, const buffer &buffer_)
- {
- kernel_.set_arg(index, buffer_.get());
- }
- };
- }
- }
- }
- #endif
|