1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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_MEMORY_OBJECT_HPP
12 #define BOOST_COMPUTE_MEMORY_OBJECT_HPP
14 #include <boost/compute/config.hpp>
15 #include <boost/compute/context.hpp>
16 #include <boost/compute/kernel.hpp>
17 #include <boost/compute/detail/get_object_info.hpp>
18 #include <boost/compute/detail/assert_cl_success.hpp>
23 /// \class memory_object
24 /// \brief Base-class for memory objects.
26 /// The memory_object class is the base-class for memory objects on
29 /// \see buffer, vector
33 /// Flags for the creation of memory objects.
35 read_write = CL_MEM_READ_WRITE,
36 read_only = CL_MEM_READ_ONLY,
37 write_only = CL_MEM_WRITE_ONLY,
38 use_host_ptr = CL_MEM_USE_HOST_PTR,
39 alloc_host_ptr = CL_MEM_ALLOC_HOST_PTR,
40 copy_host_ptr = CL_MEM_COPY_HOST_PTR
41 #ifdef BOOST_COMPUTE_CL_VERSION_1_2
43 host_write_only = CL_MEM_HOST_WRITE_ONLY,
44 host_read_only = CL_MEM_HOST_READ_ONLY,
45 host_no_access = CL_MEM_HOST_NO_ACCESS
49 /// Symbolic names for the OpenCL address spaces.
57 /// Returns the underlying OpenCL memory object.
60 return const_cast<cl_mem &>(m_mem);
63 /// Returns the size of the memory object in bytes.
64 size_t get_memory_size() const
66 return get_memory_info<size_t>(CL_MEM_SIZE);
69 /// Returns the type for the memory object.
70 cl_mem_object_type get_memory_type() const
72 return get_memory_info<cl_mem_object_type>(CL_MEM_TYPE);
75 /// Returns the flags for the memory object.
76 cl_mem_flags get_memory_flags() const
78 return get_memory_info<cl_mem_flags>(CL_MEM_FLAGS);
81 /// Returns the context for the memory object.
82 context get_context() const
84 return context(get_memory_info<cl_context>(CL_MEM_CONTEXT));
87 /// Returns the host pointer associated with the memory object.
88 void* get_host_ptr() const
90 return get_memory_info<void *>(CL_MEM_HOST_PTR);
93 /// Returns the reference count for the memory object.
94 uint_ reference_count() const
96 return get_memory_info<uint_>(CL_MEM_REFERENCE_COUNT);
99 /// Returns information about the memory object.
101 /// \see_opencl_ref{clGetMemObjectInfo}
103 T get_memory_info(cl_mem_info info) const
105 return detail::get_object_info<T>(clGetMemObjectInfo, m_mem, info);
108 #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
109 /// Registers a function to be called when the memory object is deleted
110 /// and its resources freed.
112 /// \see_opencl_ref{clSetMemObjectDestructorCallback}
114 /// \opencl_version_warning{1,1}
115 void set_destructor_callback(void (BOOST_COMPUTE_CL_CALLBACK *callback)(
116 cl_mem memobj, void *user_data
120 cl_int ret = clSetMemObjectDestructorCallback(m_mem, callback, user_data);
121 if(ret != CL_SUCCESS){
122 BOOST_THROW_EXCEPTION(opencl_error(ret));
125 /// Registers a function to be called when the memory object is deleted
126 /// and its resources freed.
128 /// The function specified by \p callback must be invokable with zero
129 /// arguments (e.g. \c callback()).
131 /// \opencl_version_warning{1,1}
132 template<class Function>
133 void set_destructor_callback(Function callback)
135 set_destructor_callback(
136 destructor_callback_invoker,
137 new boost::function<void()>(callback)
140 #endif // BOOST_COMPUTE_CL_VERSION_1_1
142 /// Returns \c true if the memory object is the same as \p other.
143 bool operator==(const memory_object &other) const
145 return m_mem == other.m_mem;
148 /// Returns \c true if the memory object is different from \p other.
149 bool operator!=(const memory_object &other) const
151 return m_mem != other.m_mem;
155 #ifdef BOOST_COMPUTE_CL_VERSION_1_1
157 static void BOOST_COMPUTE_CL_CALLBACK
158 destructor_callback_invoker(cl_mem, void *user_data)
160 boost::function<void()> *callback =
161 static_cast<boost::function<void()> *>(user_data);
167 #endif // BOOST_COMPUTE_CL_VERSION_1_1
177 explicit memory_object(cl_mem mem, bool retain = true)
181 clRetainMemObject(m_mem);
186 memory_object(const memory_object &other)
190 clRetainMemObject(m_mem);
195 memory_object& operator=(const memory_object &other)
199 clReleaseMemObject(m_mem);
205 clRetainMemObject(m_mem);
212 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
214 memory_object(memory_object&& other) BOOST_NOEXCEPT
221 memory_object& operator=(memory_object&& other) BOOST_NOEXCEPT
224 clReleaseMemObject(m_mem);
232 #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
238 BOOST_COMPUTE_ASSERT_CL_SUCCESS(
239 clReleaseMemObject(m_mem)
250 // set_kernel_arg specialization for memory_object
252 struct set_kernel_arg<memory_object>
254 void operator()(kernel &kernel_, size_t index, const memory_object &mem)
256 kernel_.set_arg(index, mem.get());
260 } // end detail namespace
261 } // end compute namespace
262 } // end boost namespace
264 #endif // BOOST_COMPUTE_MEMORY_OBJECT_HPP