\class context \brief A compute context. The context class represents a compute context. A context object manages a set of OpenCL resources including memory buffers and program objects. Before allocating memory on the device or executing kernels you must set up a context object. To create a context for the default device on the system: \code // get the default compute device boost::compute::dev
| 46 | /// |
| 47 | /// \see device, command_queue |
| 48 | class context |
| 49 | { |
| 50 | public: |
| 51 | /// Create a null context object. |
| 52 | context() |
| 53 | : m_context(0) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | /// Creates a new context for \p device with \p properties. |
| 58 | /// |
| 59 | /// \see_opencl_ref{clCreateContext} |
| 60 | explicit context(const device &device, |
| 61 | const cl_context_properties *properties = 0) |
| 62 | { |
| 63 | BOOST_ASSERT(device.id() != 0); |
| 64 | |
| 65 | cl_device_id device_id = device.id(); |
| 66 | |
| 67 | cl_int error = 0; |
| 68 | m_context = clCreateContext(properties, 1, &device_id, 0, 0, &error); |
| 69 | |
| 70 | if(!m_context){ |
| 71 | BOOST_THROW_EXCEPTION(opencl_error(error)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// Creates a new context for \p devices with \p properties. |
| 76 | /// |
| 77 | /// \see_opencl_ref{clCreateContext} |
| 78 | explicit context(const std::vector<device> &devices, |
| 79 | const cl_context_properties *properties = 0) |
| 80 | { |
| 81 | BOOST_ASSERT(!devices.empty()); |
| 82 | |
| 83 | cl_int error = 0; |
| 84 | |
| 85 | m_context = clCreateContext( |
| 86 | properties, |
| 87 | static_cast<cl_uint>(devices.size()), |
| 88 | reinterpret_cast<const cl_device_id *>(&devices[0]), |
| 89 | 0, |
| 90 | 0, |
| 91 | &error |
| 92 | ); |
| 93 | |
| 94 | if(!m_context){ |
| 95 | BOOST_THROW_EXCEPTION(opencl_error(error)); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Creates a new context object for \p context. If \p retain is |
| 100 | /// \c true, the reference count for \p context will be incremented. |
| 101 | explicit context(cl_context context, bool retain = true) |
| 102 | : m_context(context) |
| 103 | { |
| 104 | if(m_context && retain){ |
| 105 | clRetainContext(m_context); |
no outgoing calls
no test coverage detected