123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #ifndef BOOST_COMPUTE_CONTEXT_HPP
- #define BOOST_COMPUTE_CONTEXT_HPP
- #include <vector>
- #include <boost/throw_exception.hpp>
- #include <boost/compute/config.hpp>
- #include <boost/compute/device.hpp>
- #include <boost/compute/exception/opencl_error.hpp>
- #include <boost/compute/detail/assert_cl_success.hpp>
- namespace boost {
- namespace compute {
- class context
- {
- public:
-
- context()
- : m_context(0)
- {
- }
-
-
-
- explicit context(const device &device,
- const cl_context_properties *properties = 0)
- {
- BOOST_ASSERT(device.id() != 0);
- cl_device_id device_id = device.id();
- cl_int error = 0;
- m_context = clCreateContext(properties, 1, &device_id, 0, 0, &error);
- if(!m_context){
- BOOST_THROW_EXCEPTION(opencl_error(error));
- }
- }
-
-
-
- explicit context(const std::vector<device> &devices,
- const cl_context_properties *properties = 0)
- {
- BOOST_ASSERT(!devices.empty());
- cl_int error = 0;
- m_context = clCreateContext(
- properties,
- static_cast<cl_uint>(devices.size()),
- reinterpret_cast<const cl_device_id *>(&devices[0]),
- 0,
- 0,
- &error
- );
- if(!m_context){
- BOOST_THROW_EXCEPTION(opencl_error(error));
- }
- }
-
-
- explicit context(cl_context context, bool retain = true)
- : m_context(context)
- {
- if(m_context && retain){
- clRetainContext(m_context);
- }
- }
-
- context(const context &other)
- : m_context(other.m_context)
- {
- if(m_context){
- clRetainContext(m_context);
- }
- }
-
- context& operator=(const context &other)
- {
- if(this != &other){
- if(m_context){
- clReleaseContext(m_context);
- }
- m_context = other.m_context;
- if(m_context){
- clRetainContext(m_context);
- }
- }
- return *this;
- }
- #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
-
- context(context&& other) BOOST_NOEXCEPT
- : m_context(other.m_context)
- {
- other.m_context = 0;
- }
-
- context& operator=(context&& other) BOOST_NOEXCEPT
- {
- if(m_context){
- clReleaseContext(m_context);
- }
- m_context = other.m_context;
- other.m_context = 0;
- return *this;
- }
- #endif
-
- ~context()
- {
- if(m_context){
- BOOST_COMPUTE_ASSERT_CL_SUCCESS(
- clReleaseContext(m_context)
- );
- }
- }
-
- cl_context& get() const
- {
- return const_cast<cl_context &>(m_context);
- }
-
-
- device get_device() const
- {
- std::vector<device> devices = get_devices();
- if(devices.empty()) {
- return device();
- }
- return devices.front();
- }
-
- std::vector<device> get_devices() const
- {
- return get_info<std::vector<device> >(CL_CONTEXT_DEVICES);
- }
-
-
-
- template<class T>
- T get_info(cl_context_info info) const
- {
- return detail::get_object_info<T>(clGetContextInfo, m_context, info);
- }
-
- template<int Enum>
- typename detail::get_object_info_type<context, Enum>::type
- get_info() const;
-
- bool operator==(const context &other) const
- {
- return m_context == other.m_context;
- }
-
- bool operator!=(const context &other) const
- {
- return m_context != other.m_context;
- }
-
- operator cl_context() const
- {
- return m_context;
- }
- private:
- cl_context m_context;
- };
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(context,
- ((cl_uint, CL_CONTEXT_REFERENCE_COUNT))
- ((std::vector<cl_device_id>, CL_CONTEXT_DEVICES))
- ((std::vector<cl_context_properties>, CL_CONTEXT_PROPERTIES))
- )
- #ifdef CL_VERSION_1_1
- BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(context,
- ((cl_uint, CL_CONTEXT_NUM_DEVICES))
- )
- #endif
- }
- }
- #endif
|