18 #include <type_traits> 37 class INFERENCE_ENGINE_API_CLASS(Blob) {
42 using Ptr = std::shared_ptr<Blob>;
47 using CPtr = std::shared_ptr<const Blob>;
55 static Ptr CreateFromData(
const DataPtr& data);
69 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>
::type = 0,
70 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
72 return dynamic_cast<T*
>(
this) !=
nullptr;
82 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
83 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
84 bool is() const noexcept {
85 return dynamic_cast<const T*
>(
this) !=
nullptr;
97 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
98 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
100 return dynamic_cast<T*
>(
this);
111 template <
typename T,
112 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
113 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
114 const T*
as() const noexcept {
115 return dynamic_cast<const T*
>(
this);
144 virtual size_t size() const noexcept {
146 return product(tensorDesc.getDims());
153 return size() * element_size();
164 virtual size_t element_size()
const noexcept = 0;
171 virtual void allocate() noexcept = 0;
178 virtual bool deallocate() noexcept = 0;
216 if (dims.empty())
return 0;
217 return std::accumulate(std::begin(dims), std::end(dims), (
size_t)1, std::multiplies<size_t>());
225 virtual const std::shared_ptr<IAllocator>& getAllocator()
const noexcept = 0;
232 virtual void* getHandle()
const noexcept = 0;
235 friend class TBlobProxy;
244 template <
typename T,
245 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>
::type = 0,
246 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
248 return std::dynamic_pointer_cast<T>(blob);
257 template <
typename T,
258 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
259 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
261 return std::dynamic_pointer_cast<
const T>(blob);
271 class INFERENCE_ENGINE_API_CLASS(MemoryBlob):
public Blob {
276 using Ptr = std::shared_ptr<MemoryBlob>;
281 using CPtr = std::shared_ptr<const MemoryBlob>;
312 size_t size() const noexcept
override {
314 return product(tensorDesc.getDims());
322 return size() * element_size();
330 size_t element_size()
const noexcept
override = 0;
337 void allocate() noexcept
override = 0;
345 bool deallocate() noexcept
override = 0;
448 const std::shared_ptr<IAllocator>& getAllocator()
const noexcept
override = 0;
455 void* getHandle()
const noexcept
override = 0;
458 friend class TBlobProxy;
464 using BlobMap = std::map<std::string, Blob::Ptr>;
469 template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
471 template <
typename,
typename>
478 using Ptr = std::shared_ptr<TBlob<T>>;
501 if (data_size == 0) {
505 if (data_size != 0 && ptr ==
nullptr) {
509 _allocator = details::make_pre_allocator(ptr, data_size);
522 :
MemoryBlob(tensorDesc), _allocator(alloc) {}
579 return std::move(lockme<T>());
588 return std::move(lockme<const T>());
595 if (_handle !=
nullptr) {
596 getAllocator()->free(_handle);
598 _handle = getAllocator()->alloc(size() *
sizeof(T));
614 return std::move(lockme<void>());
623 return std::move(lockme<const void>());
627 return std::move(lockme<void>());
631 return std::move(lockme<const void>());
634 return std::move(lockme<void>());
645 return details::BlobIterator<T>(data());
655 details::BlobIterator<T>
end() {
656 return details::BlobIterator<T>(data(), size());
666 details::BlobIterator<const T>
begin()
const {
667 return details::BlobIterator<const T>(readOnly());
677 details::BlobIterator<const T>
end()
const {
678 return details::BlobIterator<const T>(readOnly(), size());
690 void* _handle =
nullptr;
700 auto memptr = data();
701 memcpy(memptr, blob.
readOnly(), byteSize());
713 this->_allocator = std::move(blob.
_allocator);
714 std::swap(this->_handle, blob.
_handle);
721 bool bCanRelease = getAllocator()->free(_handle);
742 const std::shared_ptr<IAllocator>&
getAllocator() const noexcept
override {
779 template <
typename Type>
783 <<
"The blob type cannot be used to store objects of current precision";
784 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
796 template <
typename Type>
801 <<
"The blob type cannot be used to store objects of current precision";
802 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
813 template <
typename Type>
815 const TensorDesc& tensorDesc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
818 <<
"The blob type cannot be used to store objects of current precision";
819 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
829 template <
typename TypeTo>
831 return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
840 template <
typename T,
typename... Args,
typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
842 return std::make_shared<T>(std::forward<Args>(args)...);
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Blob(const TensorDesc &tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:123
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:685
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:111
size_t posX
W upper left coordinate of ROI.
Definition: ie_blob.h:850
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:711
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:144
This structure describes ROI data.
Definition: ie_blob.h:848
size_t sizeX
W size of ROI.
Definition: ie_blob.h:852
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:464
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:754
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:206
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
size_t sizeY
H size of ROI.
Definition: ie_blob.h:853
MemoryBlob(const TensorDesc &tensorDesc)
Constructor. Creates an empty MemoryBlob object with the specified precision.
Definition: ie_blob.h:293
TBlob(TBlob< T > &&blob)
A move constructor.
Definition: ie_blob.h:538
T * as() noexcept
Casts this Blob object to the type T*.
Definition: ie_blob.h:99
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:578
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:548
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:218
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
details::BlobIterator< T > begin()
Gets BlobIterator for the data.
Definition: ie_blob.h:644
const T * as() const noexcept
Casts this Blob object to the type const T*.
Definition: ie_blob.h:114
virtual ~TBlob()
Virtual destructor.
Definition: ie_blob.h:559
This class is for read-only segments.
Definition: ie_locked_memory.hpp:317
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:470
virtual TensorDesc & getTensorDesc() noexcept
Returns the tensor description.
Definition: ie_blob.h:135
static size_t product(const SizeVector &dims) noexcept
Multiplies the dimension vector values.
Definition: ie_blob.h:215
LockedMemory< S > lockme() const
Creates a LockedMemory instance.
Definition: ie_blob.h:733
details::BlobIterator< T > end()
Gets BlobIterator for the end of data.
Definition: ie_blob.h:655
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:587
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:742
size_t size() const noexcept override
Returns the total number of elements, which is a product of all the dimensions.
Definition: ie_blob.h:312
This class defines Tensor description.
Definition: ie_layouts.h:153
A header file that provides Allocator interface.
TBlob(const TBlob< T > &blob)
The copy constructor data is reallocated and copied from the source to the target blob...
Definition: ie_blob.h:529
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:626
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:47
size_t element_size() const noexcept override
Gets the size of the given type.
Definition: ie_blob.h:569
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:697
std::shared_ptr< TBlob< T >> Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:478
A header file that provides class for describing precision of data.
size_t byteSize() const noexcept override
Returns the size of the current Blob in bytes calculated as size() * element_size().
Definition: ie_blob.h:321
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:42
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:53
A header file for data layouts and conversion between them.
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:271
TensorDesc & getTensorDesc() noexcept override
Returns the tensor description.
Definition: ie_blob.h:305
const TensorDesc & getTensorDesc() const noexcept override
Returns the tensor description.
Definition: ie_blob.h:298
size_t id
ID of a ROI.
Definition: ie_blob.h:849
The header file defines utility PreAllocator class.
details::BlobIterator< const T > end() const
Gets a const BlobIterator for the end of read-only data.
Definition: ie_blob.h:677
A header file for generic LockedMemory<> and different variations of locks.
This class represents a universal container in the Inference Engine.
Definition: ie_blob.h:37
details::BlobIterator< const T > begin() const
Gets a const BlobIterator for the read-only data.
Definition: ie_blob.h:666
bool is() noexcept
Checks if the Blob object can be cast to the type T*.
Definition: ie_blob.h:71
LockedMemory< void > buffer() noexcept override
Creates a new LockedMemory instance holding void pointer.
Definition: ie_blob.h:613
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:630
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:594
virtual const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:128
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:633
void * _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:690
A scalar layout.
Definition: ie_common.h:95
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:500
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:521
A header file for the BlobIterator class.
std::string type
Layer type.
Definition: ie_layers.h:47
TBlob(const TensorDesc &tensorDesc)
Creates a TBlob object with the specified dimensions and layout but does not allocate the memory...
Definition: ie_blob.h:487
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:29
virtual size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:152
bool is() const noexcept
Checks if the Blob object can be cast to the type const T*.
Definition: ie_blob.h:84
size_t posY
H upper left coordinate of ROI.
Definition: ie_blob.h:851
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:780
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:604
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:242
This is a header file with common inference engine definitions.
LockedMemory< const void > cbuffer() const noexcept override
Creates a new LockedMemory instance holding constant void pointer.
Definition: ie_blob.h:622
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:247
A header file for the main Inference Engine exception.
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:720
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:92