44 #define __CL_ENABLE_EXCEPTIONS
46 #undef __CL_ENABLE_EXCEPTIONS
48 #include <pvsutil/Assert.h>
49 #include <pvsutil/Logger.h>
51 #include "SkelCL/detail/DeviceBuffer.h"
53 #include "SkelCL/detail/Device.h"
54 #include "SkelCL/detail/DeviceList.h"
58 using namespace skelcl::detail;
60 cl::Buffer createCLBuffer(
const std::shared_ptr<Device>& devicePtr,
62 const size_t elemSize,
66 buffer = cl::Buffer(devicePtr->clContext(), flags, size * elemSize);
67 }
catch (cl::Error& err) {
68 ABORT_WITH_ERROR(err);
79 DeviceBuffer::DeviceBuffer()
80 : _device(), _size(), _elemSize(), _flags(), _buffer()
84 DeviceBuffer::DeviceBuffer(
const std::shared_ptr<Device>& devicePtr,
86 const size_t elemSize,
92 _buffer(::createCLBuffer(_device, _size, _elemSize, _flags))
94 LOG_DEBUG_INFO(
"Created new DeviceBuffer object (",
this,
") with ",
98 DeviceBuffer::DeviceBuffer(
const DeviceBuffer& rhs)
99 : _device(rhs._device),
101 _elemSize(rhs._elemSize),
106 _buffer = ::createCLBuffer(_device, _size, _elemSize, _flags);
107 _device->enqueueCopy(rhs, *
this);
109 LOG_DEBUG_INFO(
"Created new DeviceBuffer object (",
this,
") by copying (",
110 &rhs,
") with ", getInfo());
113 DeviceBuffer::DeviceBuffer(DeviceBuffer&& rhs)
114 : _device(std::move(rhs._device)),
115 _size(std::move(rhs._size)),
116 _elemSize(std::move(rhs._elemSize)),
117 _flags(std::move(rhs._flags)),
118 _buffer(std::move(rhs._buffer))
122 rhs._buffer = cl::Buffer();
123 LOG_DEBUG_INFO(
"Created new DeviceBuffer object (",
this,
") by moving with ",
127 DeviceBuffer& DeviceBuffer::operator=(
const DeviceBuffer& rhs)
129 if (
this == &rhs)
return *
this;
130 _device = rhs._device;
132 _elemSize = rhs._elemSize;
135 _buffer = ::createCLBuffer(_device, _size, _elemSize, _flags);
136 _device->enqueueCopy(rhs, *
this);
138 LOG_DEBUG_INFO(
"Assignement to DeviceBuffer object (",
this,
") now with ",
143 DeviceBuffer& DeviceBuffer::operator=(DeviceBuffer&& rhs)
145 if (
this == &rhs)
return *
this;
146 _device = std::move(rhs._device);
147 _size = std::move(rhs._size);
148 _elemSize = std::move(rhs._elemSize);
149 _flags = std::move(rhs._flags);
150 _buffer = std::move(rhs._buffer);
154 rhs._buffer = cl::Buffer();
155 LOG_DEBUG_INFO(
"Move assignment to DeviceBuffer object (",
this,
156 ") now with ", getInfo());
160 DeviceBuffer::~DeviceBuffer()
163 LOG_DEBUG_INFO(
"Empty DeviceBuffer object (",
this,
") destroyed");
165 LOG_DEBUG_INFO(
"DeviceBuffer object (",
this,
") destroyed");
167 if (_buffer() !=
nullptr) {
168 auto refCount = _buffer.getInfo<CL_MEM_REFERENCE_COUNT>();
170 LOG_DEBUG_INFO(
"OpenCL Buffer object remains alive (Ref count ",
176 std::shared_ptr<Device> DeviceBuffer::devicePtr()
const
181 DeviceBuffer::size_type DeviceBuffer::size()
const
186 DeviceBuffer::size_type DeviceBuffer::elemSize()
const
191 DeviceBuffer::size_type DeviceBuffer::sizeInBytes()
const
193 return _size * _elemSize;
196 const cl::Buffer& DeviceBuffer::clBuffer()
const
201 bool DeviceBuffer::isValid()
const
203 return (_buffer() != NULL);
206 std::string DeviceBuffer::getInfo()
const
209 s <<
"device: " << _device->id()
210 <<
", size: " << _size
211 <<
", flags: " << _flags
212 <<
", buffer: " << _buffer();