Go to the documentation of this file.
18 #include <type_traits>
31 namespace InferenceEngine {
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);
145 virtual size_t size() const noexcept {
146 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
147 return product(tensorDesc.getDims());
154 return size() * element_size();
172 virtual
void allocate() noexcept = 0;
179 virtual
bool deallocate() noexcept = 0;
228 if (dims.empty())
return 0;
229 return std::accumulate(std::begin(dims), std::end(dims), (
size_t)1, std::multiplies<size_t>());
237 virtual const std::shared_ptr<IAllocator>&
getAllocator() const noexcept = 0;
244 virtual
void* getHandle() const noexcept = 0;
247 friend class TBlobProxy;
256 template <typename T,
257 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
258 typename std::enable_if<std::is_base_of<
Blob, T>::value,
int>::type = 0>
259 std::shared_ptr<T> as(const
Blob::Ptr& blob) noexcept {
260 return std::dynamic_pointer_cast<T>(blob);
269 template <
typename T,
270 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
271 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
273 return std::dynamic_pointer_cast<const T>(blob);
283 class INFERENCE_ENGINE_API_CLASS(MemoryBlob):
public Blob {
288 using Ptr = std::shared_ptr<MemoryBlob>;
293 using CPtr = std::shared_ptr<const MemoryBlob>;
324 size_t size() const noexcept
override {
325 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
326 return product(tensorDesc.getDims());
334 return size() * element_size();
349 void allocate() noexcept override = 0;
357 bool deallocate() noexcept override = 0;
458 const std::shared_ptr<
IAllocator>& getAllocator() const noexcept override = 0;
465 void* getHandle() const noexcept override = 0;
468 friend class TBlobProxy;
479 template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
481 template <
typename,
typename>
488 using Ptr = std::shared_ptr<TBlob<T>>;
511 if (data_size == 0) {
515 if (data_size != 0 && ptr ==
nullptr) {
519 _allocator = details::make_pre_allocator(ptr, data_size);
533 if (_allocator ==
nullptr)
THROW_IE_EXCEPTION <<
"TBlob allocator was not initialized.";
591 return std::move(lockme<T>());
600 return std::move(lockme<const T>());
607 const auto allocator = getAllocator();
608 const auto rawHandle = allocator->alloc(size() *
sizeof(T));
610 if (rawHandle ==
nullptr) {
616 [allocator](
void* rawHandle) {
617 allocator->free(rawHandle);
634 return std::move(lockme<void>());
643 return std::move(lockme<const void>());
647 return std::move(lockme<void>());
651 return std::move(lockme<const void>());
654 return std::move(lockme<void>());
669 return details::BlobIterator<T>(data());
679 details::BlobIterator<T>
end() {
680 return details::BlobIterator<T>(data(), size());
690 details::BlobIterator<const T>
begin()
const {
691 return details::BlobIterator<const T>(readOnly());
701 details::BlobIterator<const T>
end()
const {
702 return details::BlobIterator<const T>(readOnly(), size());
722 tensorDesc = blob.tensorDesc;
724 auto memptr = data();
725 memcpy(memptr, blob.readOnly(), byteSize());
736 tensorDesc = blob.tensorDesc;
737 this->_allocator = std::move(blob._allocator);
738 std::swap(this->_handle, blob._handle);
745 bool bCanRelease = _handle !=
nullptr;
766 const std::shared_ptr<IAllocator>&
getAllocator() const noexcept
override {
779 return _handle.get();
784 _allocator(origBlob._allocator) {
786 <<
"Original Blob must be allocated before ROI creation";
814 template <
typename Type>
818 <<
"The blob type cannot be used to store objects of current precision";
819 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
831 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, ptr, size);
848 template <
typename Type>
850 const TensorDesc& tensorDesc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
853 <<
"The blob type cannot be used to store objects of current precision";
854 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
864 template <
typename TypeTo>
866 return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
875 template <
typename T,
typename... Args,
typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
877 return std::make_shared<T>(std::forward<Args>(args)...);
T * as() noexcept
Casts this Blob object to the type T*.
Definition: ie_blob.h:100
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
A header file that provides class for describing precision of data.
details::BlobIterator< T > begin()
Gets BlobIterator for the data.
Definition: ie_blob.h:668
A header file that provides Allocator interface.
TensorDesc make_roi_desc(const TensorDesc &origDesc, const ROI &roi, bool useOrigMemDesc)
Creates a TensorDesc object for ROI.
virtual ~TBlob()
Virtual destructor.
Definition: ie_blob.h:571
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:474
Definition: ie_c_api.h:251
virtual TensorDesc & getTensorDesc() noexcept
Returns the tensor description.
Definition: ie_blob.h:136
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:145
virtual ~Blob()
Blob virtual destructor.
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:744
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:480
LockedMemory< S > lockme() const
Creates a LockedMemory instance.
Definition: ie_blob.h:757
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:111
TBlob(const TBlob< T > &blob)
The copy constructor data is reallocated and copied from the source to the target blob.
Definition: ie_blob.h:541
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:283
size_t element_size() const noexcept override
Gets the size of the given type.
Definition: ie_blob.h:581
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:48
This class defines Tensor description.
Definition: ie_layouts.h:153
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:721
static Ptr CreateFromData(const DataPtr &data)
Creates a TBlob<> object from a Data node.
A header file for generic LockedMemory<> and different variations of locks.
const T * as() const noexcept
Casts this Blob object to the type const T*.
Definition: ie_blob.h:115
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:778
size_t size() const noexcept override
Returns the total number of elements, which is a product of all the dimensions.
Definition: ie_blob.h:324
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:560
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:815
TBlob(TBlob< T > &&blob)
A move constructor.
Definition: ie_blob.h:550
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:590
details::BlobIterator< const T > end() const
Gets a const BlobIterator for the end of read-only data.
Definition: ie_blob.h:701
details::BlobIterator< T > end()
Gets BlobIterator for the end of data.
Definition: ie_blob.h:679
bool is() noexcept
Checks if the Blob object can be cast to the type T*.
Definition: ie_blob.h:72
std::shared_ptr< TBlob< T > > Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:488
A header file for the main Inference Engine exception.
Allocator concept to be used for memory management and is used as part of the Blob.
Definition: ie_allocator.hpp:29
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:606
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:599
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:95
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:653
This is a header file with common inference engine definitions.
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:766
virtual ~MemoryBlob()
MemoryBlob virtual destructor.
size_t byteSize() const noexcept override
Returns the size of the current Blob in bytes calculated as size() * element_size().
Definition: ie_blob.h:333
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:27
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:646
This class is for read-only segments.
Definition: ie_locked_memory.hpp:317
const TensorDesc & getTensorDesc() const noexcept override
Returns the tensor description.
Definition: ie_blob.h:310
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
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:531
A header file for data layouts and conversion between them.
virtual const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:129
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
The header file defines utility PreAllocator class.
TensorDesc & getTensorDesc() noexcept override
Returns the tensor description.
Definition: ie_blob.h:317
A header file for the BlobIterator class.
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
This class represents a universal container in the Inference Engine.
Definition: ie_blob.h:38
LockedMemory< const void > cbuffer() const noexcept override
Creates a new LockedMemory instance holding constant void pointer.
Definition: ie_blob.h:642
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:624
virtual size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:153
virtual size_t element_size() const noexcept=0
Returns the number of bytes per element.
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:218
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:650
details::BlobIterator< const T > begin() const
Gets a const BlobIterator for the read-only data.
Definition: ie_blob.h:690
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:657
This structure describes ROI data for image-like tensors.
Definition: ie_layouts.h:324
Blob(const TensorDesc &tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:124
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:709
LockedMemory< void > buffer() noexcept override
Creates a new LockedMemory instance holding void pointer.
Definition: ie_blob.h:633
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:242
virtual const std::shared_ptr< IAllocator > & getAllocator() const noexcept=0
Gets an allocator for allocator-based blobs.
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:259
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:735
bool is() const noexcept
Checks if the Blob object can be cast to the type const T*.
Definition: ie_blob.h:85
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:510
#define IE_ASSERT(EXPRESSION)
Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise.
Definition: ie_exception.hpp:54
MemoryBlob(const TensorDesc &tensorDesc)
Constructor. Creates an empty MemoryBlob object with the specified precision.
Definition: ie_blob.h:305
TBlob(const TensorDesc &tensorDesc)
Creates a TBlob object with the specified dimensions and layout but does not allocate the memory.
Definition: ie_blob.h:497
std::shared_ptr< void > _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:714
size_t element_size() const noexcept override=0
Provides the number of bytes per element. Abstract method.