17 #include <type_traits>
35 class INFERENCE_ENGINE_API_CLASS(Blob) {
40 using Ptr = std::shared_ptr<Blob>;
45 using CPtr = std::shared_ptr<const Blob>;
52 static Ptr CreateFromData(
const DataPtr& data);
65 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
66 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
68 return dynamic_cast<T*
>(
this) !=
nullptr;
77 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
78 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
79 bool is() const noexcept {
80 return dynamic_cast<const T*
>(
this) !=
nullptr;
90 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
91 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
93 return dynamic_cast<T*
>(
this);
102 template <
typename T,
103 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
104 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
105 const T*
as() const noexcept {
106 return dynamic_cast<const T*
>(
this);
134 virtual size_t size() const noexcept {
135 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
136 return product(tensorDesc.getDims());
143 return size() * element_size();
150 virtual size_t element_size()
const noexcept = 0;
156 virtual void allocate() noexcept = 0;
162 virtual bool deallocate() noexcept = 0;
190 if (dims.empty())
return 0;
191 return std::accumulate(std::begin(dims), std::end(dims), (
size_t)1, std::multiplies<size_t>());
198 virtual const std::shared_ptr<IAllocator>& getAllocator()
const noexcept = 0;
204 virtual void* getHandle()
const noexcept = 0;
207 friend class TBlobProxy;
215 template <
typename T,
216 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
217 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
219 return std::dynamic_pointer_cast<T>(blob);
227 template <
typename T,
228 typename std::enable_if<!std::is_pointer<T>::value && !std::is_reference<T>::value,
int>::type = 0,
229 typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
231 return std::dynamic_pointer_cast<
const T>(blob);
240 class INFERENCE_ENGINE_API_CLASS(MemoryBlob):
public Blob {
245 using Ptr = std::shared_ptr<MemoryBlob>;
250 using CPtr = std::shared_ptr<const MemoryBlob>;
280 size_t size() const noexcept
override {
281 if (tensorDesc.getLayout() == Layout::SCALAR)
return 1;
282 return product(tensorDesc.getDims());
289 return size() * element_size();
296 size_t element_size()
const noexcept
override = 0;
302 void allocate() noexcept
override = 0;
308 bool deallocate() noexcept
override = 0;
329 const std::shared_ptr<IAllocator>& getAllocator()
const noexcept
override = 0;
335 void* getHandle()
const noexcept
override = 0;
338 friend class TBlobProxy;
344 using BlobMap = std::map<std::string, Blob::Ptr>;
349 template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
351 template <
typename,
typename>
358 using Ptr = std::shared_ptr<TBlob<T>>;
376 if (data_size == 0) {
380 if (data_size != 0 && ptr ==
nullptr) {
384 _allocator = details::make_pre_allocator(ptr, data_size);
398 :
MemoryBlob(tensorDesc), _allocator(alloc) {}
450 return std::move(lockme<T>());
458 return std::move(lockme<const T>());
465 if (_handle !=
nullptr) {
466 getAllocator()->free(_handle);
468 _handle = getAllocator()->alloc(size() *
sizeof(T));
483 return std::move(lockme<void>());
491 return std::move(lockme<const void>());
500 return details::BlobIterator<T>(data());
508 details::BlobIterator<T>
end() {
509 return details::BlobIterator<T>(data(), size());
517 details::BlobIterator<const T>
begin()
const {
518 return details::BlobIterator<const T>(readOnly());
526 details::BlobIterator<const T>
end()
const {
527 return details::BlobIterator<const T>(readOnly(), size());
539 void* _handle =
nullptr;
548 auto memptr = data();
549 memcpy(memptr, blob.
readOnly(), byteSize());
560 this->_allocator = std::move(blob.
_allocator);
561 std::swap(this->_handle, blob.
_handle);
568 bool bCanRelease = getAllocator()->free(_handle);
587 const std::shared_ptr<IAllocator>&
getAllocator() const noexcept
override {
622 template <
typename Type>
626 <<
"The blob type cannot be used to store objects of current precision";
627 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
638 template <
typename Type>
643 <<
"The blob type cannot be used to store objects of current precision";
644 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
654 template <
typename Type>
656 const TensorDesc& tensorDesc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
659 <<
"The blob type cannot be used to store objects of current precision";
660 return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
669 template <
typename TypeTo>
671 return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
679 template <
typename T,
typename... Args,
typename std::enable_if<std::is_base_of<Blob, T>::value,
int>::type = 0>
681 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:24
Blob(const TensorDesc &tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:113
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:534
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:104
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:558
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:134
This structure describes ROI data.
Definition: ie_blob.h:687
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:344
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:599
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:182
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
MemoryBlob(const TensorDesc &tensorDesc)
Constructor. Creates an empty MemoryBlob object with the specified precision.
Definition: ie_blob.h:261
TBlob(TBlob< T > &&blob)
A move constructor.
Definition: ie_blob.h:412
T * as() noexcept
Casts this Blob object to the type T*. Use InferenceEngine::as() to operate with shared Blob objects ...
Definition: ie_blob.h:92
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:449
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:421
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:199
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
details::BlobIterator< T > begin()
Gets BlobIterator for the data. Enables a ranged loop support for the TBlob object.
Definition: ie_blob.h:499
const T * as() const noexcept
Casts this Blob object to the type const T*. Use InferenceEngine::as() to operate with shared Blob ob...
Definition: ie_blob.h:105
virtual ~TBlob()
Virtual destructor.
Definition: ie_blob.h:432
This class is for read-only segments.
Definition: ie_locked_memory.hpp:287
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:350
virtual TensorDesc & getTensorDesc() noexcept
Returns the tensor description.
Definition: ie_blob.h:125
static size_t product(const SizeVector &dims) noexcept
Multiplies the dimension vector's values.
Definition: ie_blob.h:189
LockedMemory< S > lockme() const
Creates a LockedMemory instance.
Definition: ie_blob.h:579
details::BlobIterator< T > end()
Gets BlobIterator for the end of data. Enables a ranged loop support for the TBlob object...
Definition: ie_blob.h:508
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:457
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:587
size_t size() const noexcept override
Returns the total number of elements, which is a product of all the dimensions.
Definition: ie_blob.h:280
This class defines Tensor description.
Definition: ie_layouts.h:140
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:404
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:45
size_t element_size() const noexcept override
Gets the size of the given type.
Definition: ie_blob.h:441
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:545
std::shared_ptr< TBlob< T >> Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:358
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.
Definition: ie_blob.h:288
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:40
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:51
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:240
TensorDesc & getTensorDesc() noexcept override
Returns the tensor description.
Definition: ie_blob.h:273
const TensorDesc & getTensorDesc() const noexcept override
Returns the tensor description.
Definition: ie_blob.h:266
The header file defines utility PreAllocator class.
details::BlobIterator< const T > end() const
Gets a const BlobIterator for the end of read-only data. Enables a ranged loop support for the TBlob ...
Definition: ie_blob.h:526
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:35
details::BlobIterator< const T > begin() const
Gets a const BlobIterator for the read-only data. Enables a ranged loop support for the TBlob object...
Definition: ie_blob.h:517
bool is() noexcept
Checks if the Blob object can be cast to the type T*.
Definition: ie_blob.h:67
LockedMemory< void > buffer() noexcept override
Creates a new LockedMemory instance holding void pointer.
Definition: ie_blob.h:482
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:464
virtual const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:118
void * _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:539
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:375
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:397
A header file for the BlobIterator class.
TBlob(const TensorDesc &tensorDesc)
Creates a TBlob object with the specified dimensions and layout but does not allocate the memory...
Definition: ie_blob.h:365
std::vector< size_t > SizeVector
Represents tensor size. The order is opposite to the order in Caffe*: (w,h,n,b) where the most freque...
Definition: ie_common.h:27
virtual size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:142
bool is() const noexcept
Checks if the Blob object can be cast to the type const T*.
Definition: ie_blob.h:79
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:623
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:474
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:260
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:490
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:218
A header file for the main Inference Engine exception.
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:567
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:89