Class buffer

boost::compute::buffer — A memory buffer on a compute device.


// In header: <boost/compute/buffer.hpp>

class buffer : public boost::compute::memory_object {
  // construct/copy/destruct
  explicit buffer(cl_mem, bool = true);
  buffer(const context &, size_t, cl_mem_flags = read_write, void * = 0);
  buffer(const buffer &);
  buffer(buffer &&) noexcept;
  buffer & operator=(const buffer &);
  buffer & operator=(buffer &&) noexcept;

  // public member functions
  size_t size() const;
  template<typename T> T get_info(cl_mem_info) const;
  template<int Enum> unspecified get_info() const;
  buffer clone(command_queue &) const;


The buffer class represents a memory buffer on a compute device.

Buffers are allocated within a compute context. For example, to allocate a memory buffer for 32 float's:

boost::compute::buffer buf(context, 32 * sizeof(float));

Once created, data can be copied to and from the buffer using the enqueue_*_buffer() methods in the command_queue class. For example, to copy a set of int values from the host to the device:

int data[] = { 1, 2, 3, 4 };

queue.enqueue_write_buffer(buf, 0, 4 * sizeof(int), data);

Also see the copy() algorithm for a higher-level interface to copying data between the host and the device. For a higher-level, dynamically-resizable, type-safe container for data on a compute device, use the vector<T> class.

Buffer objects have reference semantics. Creating a copy of a buffer object simply creates another reference to the underlying OpenCL memory object. To create an actual copy use the buffer::clone() method.

See Also:

context, command_queue

buffer public construct/copy/destruct

  1. buffer();
    Creates a null buffer object.
  2. explicit buffer(cl_mem mem, bool retain = true);

    Creates a buffer object for mem. If retain is true, the reference count for mem will be incremented.

  3. buffer(const context & context, size_t size, cl_mem_flags flags = read_write, 
           void * host_ptr = 0);

    Create a new memory buffer in of size with flags in context.

    See the documentation for clCreateBuffer() for more information.

  4. buffer(const buffer & other);
    Creates a new buffer object as a copy of other.
  5. buffer(buffer && other) noexcept;
    Move-constructs a new buffer object from other.
  6. buffer & operator=(const buffer & other);
    Copies the buffer object from other to *this.
  7. buffer & operator=(buffer && other) noexcept;
    Move-assigns the buffer from other to *this.
  8. ~buffer();
    Destroys the buffer object.

buffer public member functions

  1. size_t size() const;
    Returns the size of the buffer in bytes.
  2. template<typename T> T get_info(cl_mem_info info) const;

    Returns information about the buffer.

    See the documentation for clGetMemObjectInfo() for more information.

  3. template<int Enum> unspecified get_info() const;

    This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

  4. buffer clone(command_queue & queue) const;

    Creates a new buffer with a copy of the data in *this. Uses queue to perform the copy.