18 #include <type_traits>
38 class INFERENCE_ENGINE_API_CLASS(Blob) {
43 using Ptr = std::shared_ptr<Blob>;
48 using CPtr = std::shared_ptr<const Blob>;
70 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
71 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
73 return dynamic_cast<T*
>(
this) !=
nullptr;
83 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
84 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
85 bool is() const noexcept {
86 return dynamic_cast<const T*
>(
this) !=
nullptr;
98 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
99 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
101 return dynamic_cast<T*
>(
this);
112 template <
typename T,
113 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
114 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
115 const T*
as() const noexcept {
116 return dynamic_cast<const T*
>(
this);
149 virtual size_t size() const noexcept {
150 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
151 return product(tensorDesc.getDims());
159 return size() * element_size();
179 virtual
void allocate() noexcept = 0;
188 virtual
bool deallocate() noexcept = 0;
237 if (dims.empty())
return 0;
238 return std::accumulate(std::begin(dims), std::end(dims), (
size_t)1, std::multiplies<size_t>());
246 virtual const std::shared_ptr<IAllocator>&
getAllocator() const noexcept = 0;
253 virtual
void* getHandle() const noexcept = 0;
257 friend class TBlobProxy;
266 template <typename T,
267 typename
std::enable_if<!
std::is_pointer<T>::value && !
std::is_reference<T>::value,
int>::type = 0,
268 typename
std::enable_if<
std::is_base_of<
Blob, T>::value,
int>::type = 0>
270 return std::dynamic_pointer_cast<T>(blob);
279 template <
typename T,
280 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
281 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
283 return std::dynamic_pointer_cast<const T>(blob);
293 class INFERENCE_ENGINE_API_CLASS(MemoryBlob):
public Blob {
298 using Ptr = std::shared_ptr<MemoryBlob>;
303 using CPtr = std::shared_ptr<const MemoryBlob>;
335 size_t size() const noexcept
override {
336 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
337 return product(tensorDesc.getDims());
345 return size() * element_size();
360 void allocate() noexcept override = 0;
368 bool deallocate() noexcept override = 0;
469 const
std::shared_ptr<
IAllocator>& getAllocator() const noexcept override = 0;
476 void* getHandle() const noexcept override = 0;
480 friend class TBlobProxy;
491 template <typename T, typename =
std::enable_if<
std::is_pod<T>::value>>
493 template <
typename,
typename>
500 using Ptr = std::shared_ptr<TBlob<T>>;
523 if (data_size == 0) {
527 if (data_size != 0 && ptr ==
nullptr) {
531 _allocator = details::make_pre_allocator(ptr, data_size);
545 if (_allocator ==
nullptr)
THROW_IE_EXCEPTION <<
"TBlob allocator was not initialized.";
581 #if defined(__clang__) && !defined(__SYCL_COMPILER_VERSION)
604 return std::move(lockme<T>());
613 return std::move(lockme<const T>());
620 const auto allocator = getAllocator();
621 const auto rawHandle = allocator->alloc(size() *
sizeof(T));
623 if (rawHandle ==
nullptr) {
629 [allocator](
void* rawHandle) {
630 allocator->free(rawHandle);
647 return std::move(lockme<void>());
656 return std::move(lockme<const void>());
660 return std::move(lockme<void>());
664 return std::move(lockme<const void>());
667 return std::move(lockme<void>());
682 return details::BlobIterator<T>(data());
692 details::BlobIterator<T>
end() {
693 return details::BlobIterator<T>(data(), size());
703 details::BlobIterator<const T>
begin()
const {
704 return details::BlobIterator<const T>(readOnly());
714 details::BlobIterator<const T>
end()
const {
715 return details::BlobIterator<const T>(readOnly(), size());
735 tensorDesc = blob.tensorDesc;
737 auto memptr = data();
738 memcpy(memptr, blob.readOnly(), byteSize());
749 tensorDesc = blob.tensorDesc;
750 this->_allocator = std::move(blob._allocator);
751 std::swap(this->_handle, blob._handle);
758 bool bCanRelease = _handle !=
nullptr;
779 const std::shared_ptr<IAllocator>&
getAllocator() const noexcept
override {
792 return _handle.get();
802 _allocator(origBlob._allocator) {
804 <<
"Original Blob must be allocated before ROI creation";
810 #if defined(__clang__) && !defined(__SYCL_COMPILER_VERSION)
832 template <
typename Type>
836 <<
"The blob type cannot be used to store objects of current precision";
837 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
849 template <
typename Type>
854 <<
"The blob type cannot be used to store objects of current precision";
855 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
866 template <
typename Type>
868 const TensorDesc& tensorDesc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
871 <<
"The blob type cannot be used to store objects of current precision";
872 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
882 template <
typename TypeTo>
884 return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
893 template <
typename T,
typename... Args,
typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
895 return std::make_shared<T>(std::forward<Args>(args)...);
This class represents a universal container in the Inference Engine.
Definition: ie_blob.h:38
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:48
virtual size_t size() const noexcept
By default, returns the total number of elements (a product of all the dims or 1 for scalar)
Definition: ie_blob.h:149
virtual size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:158
bool is() const noexcept
Checks if the Blob object can be cast to the type const T*.
Definition: ie_blob.h:85
virtual ~Blob()
Blob virtual destructor.
bool is() noexcept
Checks if the Blob object can be cast to the type T*.
Definition: ie_blob.h:72
const T * as() const noexcept
Casts this Blob object to the type const T*.
Definition: ie_blob.h:115
virtual TensorDesc & getTensorDesc() noexcept
Returns the tensor description.
Definition: ie_blob.h:138
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
T * as() noexcept
Casts this Blob object to the type T*.
Definition: ie_blob.h:100
virtual const std::shared_ptr< IAllocator > & getAllocator() const noexcept=0
Gets an allocator for allocator-based blobs.
virtual const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:130
Blob(const TensorDesc &tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:124
static Ptr CreateFromData(const DataPtr &data)
Creates a TBlob<> object from a Data node.
virtual size_t element_size() const noexcept=0
Provides the number of bytes per element.
Allocator concept to be used for memory management and is used as part of the Blob.
Definition: ie_allocator.hpp:29
This class is for read-only segments.
Definition: ie_locked_memory.hpp:319
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:219
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:111
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:293
const TensorDesc & getTensorDesc() const noexcept override
Returns the tensor description.
Definition: ie_blob.h:320
size_t element_size() const noexcept override=0
Provides the number of bytes per element. Abstract method.
size_t size() const noexcept override
Returns the total number of elements, which is a product of all the dimensions.
Definition: ie_blob.h:335
virtual ~MemoryBlob()
MemoryBlob virtual destructor.
MemoryBlob(const TensorDesc &tensorDesc)
Constructor. Creates an empty MemoryBlob object with the specified precision.
Definition: ie_blob.h:315
size_t byteSize() const noexcept override
Returns the size of the current Blob in bytes calculated as size() * element_size().
Definition: ie_blob.h:344
TensorDesc & getTensorDesc() noexcept override
Returns the tensor description.
Definition: ie_blob.h:327
bool hasStorageType(const char *typeName=nullptr) const noexcept
checks whether given storage class T can be used to store objects of current precision
Definition: ie_precision.hpp:99
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:492
details::BlobIterator< T > end()
Gets BlobIterator for the end of data.
Definition: ie_blob.h:692
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:572
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:757
std::shared_ptr< void > _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:727
details::BlobIterator< const T > end() const
Gets a const BlobIterator for the end of read-only data.
Definition: ie_blob.h:714
TBlob(TBlob< T > &&blob)
A move constructor.
Definition: ie_blob.h:562
LockedMemory< void > buffer() noexcept override
Creates a new LockedMemory instance holding void pointer.
Definition: ie_blob.h:646
TBlob(const TensorDesc &tensorDesc, const std::shared_ptr< IAllocator > &alloc)
Creates a TBlob object with the specified dimensions, layout and custom memory allocator but does not...
Definition: ie_blob.h:543
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:722
Blob::Ptr createROI(const ROI &roi) const override
Creates a blob describing given ROI object based on the current blob with memory sharing.
Definition: ie_blob.h:670
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:619
details::BlobIterator< T > begin()
Gets BlobIterator for the data.
Definition: ie_blob.h:681
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:612
LockedMemory< void > rwmap() noexcept override
Gets read/write access to the memory in virtual space of the process. The function returns object whi...
Definition: ie_blob.h:659
TBlob(const TensorDesc &tensorDesc)
Creates a TBlob object with the specified dimensions and layout but does not allocate the memory.
Definition: ie_blob.h:509
LockedMemory< const void > rmap() const noexcept override
Gets read only access to the memory in virtual space of the process. The function returns object whic...
Definition: ie_blob.h:663
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:748
TBlob(const TensorDesc &tensorDesc, T *ptr, size_t data_size=0)
The constructor creates a TBlob object with the specified dimensions and layout on the pre-allocated ...
Definition: ie_blob.h:522
TBlob(const TBlob &origBlob, const ROI &roi)
Creates a blob from the existing blob with a given ROI.
Definition: ie_blob.h:800
LockedMemory< const void > cbuffer() const noexcept override
Creates a new LockedMemory instance holding constant void pointer.
Definition: ie_blob.h:655
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:791
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:734
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:637
virtual ~TBlob()
Virtual destructor.
Definition: ie_blob.h:584
LockedMemory< void > wmap() noexcept override
Gets "write only direction" access to the memory in virtual space of the process. The function return...
Definition: ie_blob.h:666
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:603
details::BlobIterator< const T > begin() const
Gets a const BlobIterator for the read-only data.
Definition: ie_blob.h:703
std::shared_ptr< TBlob< T > > Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:500
TBlob(const TBlob< T > &blob)
The copy constructor data is reallocated and copied from the source to the target blob.
Definition: ie_blob.h:553
size_t element_size() const noexcept override
Gets the size of the given type.
Definition: ie_blob.h:594
LockedMemory< S > lockme() const
Creates a LockedMemory instance.
Definition: ie_blob.h:770
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:779
This class defines Tensor description.
Definition: ie_layouts.h:158
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:247
A header file that provides Allocator interface.
A header file for the BlobIterator class.
This is a header file with common inference engine definitions.
A header file for the main Inference Engine exception.
#define THROW_IE_EXCEPTION
A macro used to throw general exception with a description.
Definition: ie_exception.hpp:25
#define IE_ASSERT(EXPRESSION)
Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise.
Definition: ie_exception.hpp:54
A header file for data layouts and conversion between them.
A header file for generic LockedMemory<> and different variations of locks.
The header file defines utility PreAllocator class.
A header file that provides class for describing precision of data.
Inference Engine C++ API.
Definition: cldnn_config.hpp:15
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:833
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:269
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:27
std::map< std::string, Blob::Ptr > BlobMap
This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance)...
Definition: ie_blob.h:486
TensorDesc make_roi_desc(const TensorDesc &origDesc, const ROI &roi, bool useOrigMemDesc)
Creates a TensorDesc object for ROI.
This structure describes ROI data for image-like tensors.
Definition: ie_layouts.h:329