20 #include "cldnn_defs.h" 21 #include "compounds.h" 53 if (
size == 0)
throw std::invalid_argument(
"size should be more than 0");
54 return check_status<cldnn_memory>(
"memory allocation failed", [&](status_t* status)
67 if (!ptr)
throw std::invalid_argument(
"pointer should not be null");
68 size_t data_size =
size *
sizeof(T);
70 std::string err_str(
"buffer size mismatch - input size " + std::to_string(data_size) +
" layout size " + std::to_string(
layout.
bytes_count()));
71 throw std::invalid_argument(err_str);
74 return check_status<cldnn_memory>(
"memory attach failed", [&](status_t* status)
81 :_impl(other._impl), _layout(other._layout)
82 ,_size(other._size), _count(other._count)
87 memory& operator=(
const memory& other)
89 if (_impl == other._impl)
return *
this;
92 _layout = other._layout;
94 _count = other._count;
104 friend bool operator==(
const memory& lhs,
const memory& rhs) {
return lhs._impl == rhs._impl; }
105 friend bool operator!=(
const memory& lhs,
const memory& rhs) {
return !(lhs == rhs); }
108 size_t count()
const {
return _count; }
111 size_t size()
const {
return _size; }
119 auto my_engine = check_status<cldnn_engine>(
"get memory engine failed", [&](status_t* status)
126 bool is_the_same_buffer(
const memory& other)
const 128 return check_status<bool>(
"checking if two memories refers to the same buffer failed", [&](status_t* status)
150 if (!mem)
throw std::invalid_argument(
"mem");
152 return check_status<layout>(
"get memory layout failed", [=](status_t* status)
159 :_impl(data), _layout(get_layout_impl(data))
160 , _size(_layout.bytes_count()), _count(_layout.
count())
162 if (_impl ==
nullptr)
163 throw std::invalid_argument(
"implementation pointer should not be null");
168 check_status<void>(
"retain memory failed", [=](status_t* status) {
cldnn_retain_memory(_impl, status); });
172 check_status<void>(
"release memory failed", [=](status_t* status) {
cldnn_release_memory(_impl, status); });
178 if (data_type_traits::align_of(_layout.
data_type) %
alignof(T) != 0)
180 throw std::logic_error(
"memory data type alignment do not match");
182 return check_status<T*>(
"memory lock failed", [=](status_t* status) {
return static_cast<T*
>(
cldnn_lock_memory(_impl, status)); });
187 check_status<void>(
"memory unlock failed", [=](status_t* status) {
return cldnn_unlock_memory(_impl, status); });
216 , _size(_mem.
size()/sizeof(T))
217 , _ptr(_mem.lock<T>())
229 if (this->_mem != other._mem)
235 size_t size()
const {
return _size; }
237 #if defined(_SECURE_SCL) && (_SECURE_SCL > 0) 238 typedef stdext::checked_array_iterator<T*> iterator;
239 typedef stdext::checked_array_iterator<const T*> const_iterator;
241 iterator begin() & {
return stdext::make_checked_array_iterator(_ptr,
size()); }
242 iterator end() & {
return stdext::make_checked_array_iterator(_ptr,
size(),
size()); }
244 const_iterator begin() const& {
return stdext::make_checked_array_iterator(_ptr,
size()); }
245 const_iterator end() const& {
return stdext::make_checked_array_iterator(_ptr,
size(),
size()); }
248 typedef const T* const_iterator;
249 iterator begin() & {
return _ptr; }
250 iterator end() & {
return _ptr +
size(); }
251 const_iterator begin() const& {
return _ptr; }
252 const_iterator end() const& {
return _ptr +
size(); }
265 const T*
data() const& {
return _ptr; }
267 friend bool operator==(
const pointer& lhs,
const pointer& rhs) {
return lhs._mem == rhs._mem; }
268 friend bool operator!=(
const pointer& lhs,
const pointer& rhs) {
return !(lhs == rhs); }
288 void do_copy(
const memory& mem)
290 auto ptr = mem.lock<T>();
293 _size = _mem.
size() /
sizeof(T);
298 #ifndef DOXYGEN_SHOULD_SKIP_THIS 299 template <
typename T>
CLDNN_API void cldnn_unlock_memory(cldnn_memory memory, cldnn_status *status)
Unlocks memory locked by cldnn_lock_memory(cldnn_memory memory, cldnn_status* status).
T & operator[](size_t idx) const &
Provides indexed access to pointed memory.
::cldnn_engine get() const
get C API engine handler.
size_t size() const
Returns the number of elements (of type T) stored in memory.
void operator[](size_t idx) &&
Prevents to use pointer as temporary object.
pointer(const memory &mem)
Constructs pointer from memory and locks (pin) ref@ memory object.
CLDNN_API int32_t cldnn_is_the_same_buffer(cldnn_memory mem1, cldnn_memory mem2, cldnn_status *status)
Checks if two memory objects refer to the same underlaying buffer.
Represents network output returned by network::get_output().
CLDNN_API cldnn_layout cldnn_get_memory_layout(cldnn_memory memory, cldnn_status *status)
Returns memory layout.
~pointer()
Unlocks memory.
data_types data_type
Data type stored in memory (see. data_types)
CLDNN_API cldnn_engine cldnn_get_memory_engine(cldnn_memory memory, cldnn_status *status)
Returns reference to the engine associated with memory object.
Provides input data to topology.
struct cldnn_memory_impl * cldnn_memory
Memory object.
static memory attach(const cldnn::layout &layout, T *ptr, size_t size)
CLDNN_API cldnn_memory cldnn_allocate_memory(cldnn_engine engine, cldnn_layout layout, cldnn_status *status)
Allocate memory on engine using specified layout.
void data() &&
Prevents to use pointer as temporary object.
bool is_allocated_by(const engine &engine) const
Test if memory is allocated by engine.
T * data() &
Returns the raw pointer to pointed memory.
CLDNN_API void cldnn_release_memory(cldnn_memory memory, cldnn_status *status)
Decrement reference counter for the memory object. Deletes object when counter becomes zero...
static memory allocate(const engine &engine, const layout &layout)
Allocate memory on engine using specified layout.
CLDNN_API cldnn_memory cldnn_attach_memory(cldnn_layout layout, void *pointer, size_t size, cldnn_status *status)
Create memory object attached to the buffer allocated by user.
const T * data() const &
Returns the constant raw pointer to pointed memory.
CLDNN_API void * cldnn_lock_memory(cldnn_memory memory, cldnn_status *status)
Locks memory buffer. Provides direct access to memory data.
size_t bytes_count() const
Number of bytes needed to store this layout.
CLDNN_API void cldnn_retain_memory(cldnn_memory memory, cldnn_status *status)
Increment reference counter for the memory object.
Executable network allocated from program.
const layout & get_layout() const
Associated layout.
pointer(const pointer &other)
Copy construction.
Represents clDNN engine object.
Helper class to get an access memory data.
Represents buffer with particular layout.
pointer & operator=(const pointer &other)
Copy assignment.
size_t size() const
number of bytes used by memory
void begin() &&
Prevents to use pointer as temporary object.
void end() &&
Prevents to use pointer as temporary object.
size_t count() const
number of elements of _layout.data_type stored in memory