ie_blob.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for Blob and generic TBlob<>
7  * @file ie_blob.h
8  */
9 #pragma once
10 
11 #include <cstring>
12 #include <functional>
13 #include <map>
14 #include <memory>
15 #include <numeric>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19 #include <vector>
20 
22 #include "details/ie_exception.hpp"
24 #include "ie_allocator.hpp"
25 #include "ie_common.h"
26 #include "ie_layouts.h"
27 #include "ie_locked_memory.hpp"
28 #include "ie_precision.hpp"
29 
30 namespace InferenceEngine {
31 /**
32  * @brief This class represents a universal container in the Inference Engine
33  * @note Each Blob implementation must be derived from this Blob class directly or indirectly
34  */
35 class INFERENCE_ENGINE_API_CLASS(Blob) {
36 public:
37  /**
38  * @brief A smart pointer containing Blob object
39  */
40  using Ptr = std::shared_ptr<Blob>;
41 
42  /**
43  * @brief A smart pointer to the const Blob object
44  */
45  using CPtr = std::shared_ptr<const Blob>;
46 
47  /**
48  * @brief Creates a TBlob<> object from a Data node
49  * @param Data reference to a smart pointer of the Data node
50  * @return Smart pointer to TBlob<> with the relevant C type to the precision of the data node
51  */
52  static Ptr CreateFromData(const DataPtr& data);
53 
54  /**
55  * @brief Blob virtual destructor
56  */
57  virtual ~Blob();
58 
59  /**
60  * @brief Checks if the Blob object can be cast to the type T*
61  * @tparam T Type to be checked. Must represent a class derived from the Blob
62  * @return true if this object can be dynamically cast to the type T*. Otherwise, false
63  */
64  template <typename T,
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>
67  bool is() noexcept {
68  return dynamic_cast<T*>(this) != nullptr;
69  }
70 
71  /**
72  * @brief Checks if the Blob object can be cast to the type const T*
73  * @tparam T Type to be checked. Must represent a class derived from the Blob
74  * @return true if this object can be dynamically cast to the type const T*. Otherwise, false
75  */
76  template <typename T,
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;
81  }
82 
83  /**
84  * @brief Casts this Blob object to the type T*. Use InferenceEngine::as() to operate with
85  * shared Blob objects instead of raw pointers
86  * @tparam T Type to cast to. Must represent a class derived from the Blob
87  * @return Raw pointer to the object of the type T or nullptr on error
88  */
89  template <typename T,
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>
92  T* as() noexcept {
93  return dynamic_cast<T*>(this);
94  }
95 
96  /**
97  * @brief Casts this Blob object to the type const T*. Use InferenceEngine::as() to operate with
98  * shared Blob objects instead of raw pointers
99  * @tparam T Type to cast to. Must represent a class derived from the Blob
100  * @return Raw pointer to the object of the type const T or nullptr on error
101  */
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);
107  }
108 
109  /**
110  * @brief Constructor. Creates an empty Blob object with the specified precision.
111  * @param tensorDesc Defines the layout and dims of the blob
112  */
113  explicit Blob(const TensorDesc& tensorDesc): tensorDesc(tensorDesc) {}
114 
115  /**
116  * @brief Returns the tensor description
117  */
118  virtual const TensorDesc& getTensorDesc() const noexcept {
119  return tensorDesc;
120  }
121 
122  /**
123  * @brief Returns the tensor description
124  */
125  virtual TensorDesc& getTensorDesc() noexcept {
126  return tensorDesc;
127  }
128 
129  /**
130  * @brief By default, returns the total number of elements (a product of all the dims or 1 for scalar)
131  *
132  * Return value and its interpretation heavily depend on the blob type
133  */
134  virtual size_t size() const noexcept {
135  if (tensorDesc.getLayout() == Layout::SCALAR) return 1;
136  return product(tensorDesc.getDims());
137  }
138 
139  /**
140  * @brief Returns the size of the current Blob in bytes.
141  */
142  virtual size_t byteSize() const noexcept {
143  return size() * element_size();
144  }
145 
146  /**
147  * @brief Returns the number of bytes per element. The overall Blob capacity is size() * element_size().
148  * Abstract method.
149  */
150  virtual size_t element_size() const noexcept = 0;
151 
152  /**
153  * @brief Allocates memory to store the data.
154  * Abstract method.
155  */
156  virtual void allocate() noexcept = 0;
157 
158  /**
159  * @brief Releases previously allocated data.
160  * Abstract method.
161  */
162  virtual bool deallocate() noexcept = 0;
163 
164  /**
165  * @brief Gets access to the allocated memory.
166  * Abstract method.
167  * @return A LockedMemory object
168  */
169  virtual LockedMemory<void> buffer() noexcept = 0;
170 
171  /**
172  * @brief Gets read-only access to the allocated memory.
173  * Abstract method.
174  * @return A LockedMemory object
175  */
176  virtual LockedMemory<const void> cbuffer() const noexcept = 0;
177 
178 protected:
179  /**
180  * @brief The tensor descriptor of the given blob.
181  */
183 
184  /**
185  * @brief Multiplies the dimension vector's values.
186  * @param dims Reference to a vector with dimension values of type size_t
187  * @return Result of multiplication
188  */
189  static size_t product(const SizeVector& dims) noexcept {
190  if (dims.empty()) return 0;
191  return std::accumulate(std::begin(dims), std::end(dims), (size_t)1, std::multiplies<size_t>());
192  }
193 
194  /**
195  * @brief Gets an allocator for allocator-based blobs
196  * @return The allocator for allocator-based blobs or nullptr if there is none
197  */
198  virtual const std::shared_ptr<IAllocator>& getAllocator() const noexcept = 0;
199 
200  /**
201  * @brief Gets a handle to allocated memory
202  * @return The handle to allocated memory for allocator-based blobs or nullptr if there is none
203  */
204  virtual void* getHandle() const noexcept = 0;
205 
206  template <typename>
207  friend class TBlobProxy;
208 };
209 
210 /**
211  * @brief Helper cast function to work with shared Blob objects
212  * @return shared_ptr to the type T. Returned shared_ptr shares ownership of the object with the
213  * input Blob::Ptr
214  */
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>
218 std::shared_ptr<T> as(const Blob::Ptr& blob) noexcept {
219  return std::dynamic_pointer_cast<T>(blob);
220 }
221 
222 /**
223  * @brief Helper cast function to work with shared Blob objects
224  * @return shared_ptr to the type const T. Returned shared_ptr shares ownership of the object with
225  * the input Blob::Ptr
226  */
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>
230 std::shared_ptr<const T> as(const Blob::CPtr& blob) noexcept {
231  return std::dynamic_pointer_cast<const T>(blob);
232 }
233 
234 /**
235  * @brief This class implements a container object that represents a tensor in memory (host and
236  * remote/accelerated)
237  * @note Any Blob implementation that represents a concept of a tensor in memory (for example,
238  * TBlob) must be a subclass of MemoryBlob instead of Blob
239  */
240 class INFERENCE_ENGINE_API_CLASS(MemoryBlob): public Blob {
241 public:
242  /**
243  * @brief A smart pointer to the MemoryBlob object
244  */
245  using Ptr = std::shared_ptr<MemoryBlob>;
246 
247  /**
248  * @brief A smart pointer to the const MemoryBlob object
249  */
250  using CPtr = std::shared_ptr<const MemoryBlob>;
251 
252  /**
253  * @brief MemoryBlob virtual destructor
254  */
255  virtual ~MemoryBlob();
256 
257  /**
258  * @brief Constructor. Creates an empty MemoryBlob object with the specified precision.
259  * @param tensorDesc Defines the layout and dims of the blob
260  */
261  explicit MemoryBlob(const TensorDesc& tensorDesc): Blob(tensorDesc) {}
262 
263  /**
264  * @brief Returns the tensor description
265  */
266  const TensorDesc& getTensorDesc() const noexcept override {
267  return tensorDesc;
268  }
269 
270  /**
271  * @brief Returns the tensor description
272  */
273  TensorDesc& getTensorDesc() noexcept override {
274  return tensorDesc;
275  }
276 
277  /**
278  * @brief Returns the total number of elements, which is a product of all the dimensions
279  */
280  size_t size() const noexcept override {
281  if (tensorDesc.getLayout() == Layout::SCALAR) return 1;
282  return product(tensorDesc.getDims());
283  }
284 
285  /**
286  * @brief Returns the size of the current Blob in bytes
287  */
288  size_t byteSize() const noexcept override {
289  return size() * element_size();
290  }
291 
292  /**
293  * @brief Returns the number of bytes per element. The overall MemoryBlob capacity is size() * element_size().
294  * Abstract method.
295  */
296  size_t element_size() const noexcept override = 0;
297 
298  /**
299  * @brief Allocates memory to store the data.
300  * Abstract method.
301  */
302  void allocate() noexcept override = 0;
303 
304  /**
305  * @brief Releases previously allocated data.
306  * Abstract method.
307  */
308  bool deallocate() noexcept override = 0;
309 
310  /**
311  * @brief Gets access to the allocated memory.
312  * Abstract method.
313  * @return A LockedMemory object
314  */
315  LockedMemory<void> buffer() noexcept override = 0;
316 
317  /**
318  * @brief Gets read-only access to the allocated memory.
319  * Abstract method.
320  * @return A LockedMemory object
321  */
322  LockedMemory<const void> cbuffer() const noexcept override = 0;
323 
324 protected:
325  /**
326  * @brief Gets the allocator for allocator-based blobs.
327  * @return The allocator for allocator-based blobs or if there is none then a nullptr.
328  */
329  const std::shared_ptr<IAllocator>& getAllocator() const noexcept override = 0;
330 
331  /**
332  * @brief Gets the handle to allocated memory.
333  * @return The handle to allocated memory for allocator-based blobs or if there is none then a nullptr.
334  */
335  void* getHandle() const noexcept override = 0;
336 
337  template <typename>
338  friend class TBlobProxy;
339 };
340 
341 /**
342  * @brief This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance).
343  */
344 using BlobMap = std::map<std::string, Blob::Ptr>;
345 
346 /**
347  * @brief Represents real host memory allocated for a Tensor/Blob per C type.
348  */
349 template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
350 class TBlob : public MemoryBlob {
351  template <typename, typename>
352  friend class TBlob;
353 
354 public:
355  /**
356  * @brief Smart Pointer to this TBlob object.
357  */
358  using Ptr = std::shared_ptr<TBlob<T>>;
359 
360  /**
361  * @brief Creates a TBlob object with the specified dimensions and layout but does not allocate the memory.
362  * Use the allocate() method to allocate memory.
363  * @param tensorDesc Tensor description
364  */
365  explicit TBlob(const TensorDesc& tensorDesc): MemoryBlob(tensorDesc) {}
366 
367  /**
368  * @brief The constructor creates a TBlob object with the specified dimensions and layout
369  * on the pre-allocated memory. The allocate() call is not required.
370  * @param tensorDesc Tensor description
371  * @param ptr Pointer to the pre-allocated memory
372  * @param data_size Length of the pre-allocated array. If not set, size is assumed equal
373  * to the dot product of dims.
374  */
375  TBlob(const TensorDesc& tensorDesc, T* ptr, size_t data_size = 0): MemoryBlob(tensorDesc) {
376  if (data_size == 0) {
377  data_size = size();
378  }
379 
380  if (data_size != 0 && ptr == nullptr) {
381  THROW_IE_EXCEPTION << "Using Blob on external nullptr memory";
382  }
383 
384  _allocator = details::make_pre_allocator(ptr, data_size);
385  // blob on attached memory is always allocated, so we are not forcing the user to call allocate()
386  allocate();
387  }
388 
389  /**
390  * @brief Creates a TBlob object with the specified dimensions, layout and custom memory allocator but does not
391  * allocate the memory.
392  * @param p Precision
393  * @param l Layout
394  * @param dims Tensor dimensions
395  * @param alloc Allocator to be used
396  */
397  TBlob(const TensorDesc& tensorDesc, const std::shared_ptr<IAllocator>& alloc)
398  : MemoryBlob(tensorDesc), _allocator(alloc) {}
399 
400  /**
401  * @brief The copy constructor data is reallocated and copied from the source to the target blob.
402  * @param blob Source blob
403  */
404  TBlob(const TBlob<T>& blob): MemoryBlob(blob.getTensorDesc()) {
405  copyFrom(blob);
406  }
407 
408  /**
409  * @brief A move constructor.
410  * @param blob rvalue to make a move from
411  */
412  TBlob(TBlob<T>&& blob): MemoryBlob(blob.getTensorDesc()) {
413  moveFrom(blob);
414  }
415 
416  /**
417  * @brief Copy operator for the TBlob object.
418  * @param blob object reference to copy from
419  * @return Newly copied object
420  */
421  TBlob& operator=(const TBlob& blob) {
422  copyFrom(blob);
423  return *this;
424  }
425 
426  /**
427  *@brief Virtual destructor.
428  */
429 #ifdef __clang__
430  virtual ~TBlob();
431 #else
432  virtual ~TBlob() {
433  free();
434  }
435 #endif // __clang__
436 
437  /**
438  * @brief Gets the size of the given type.
439  * @return Size of the type
440  */
441  size_t element_size() const noexcept override {
442  return sizeof(T);
443  }
444 
445  /**
446  * @brief Creates an new empty rvalue LockedMemory object.
447  * @return rvalue for the empty locked object of type T
448  */
449  virtual LockedMemory<T> data() noexcept {
450  return std::move(lockme<T>());
451  }
452 
453  /**
454  * @brief Creates a new empty rvalue read-only LockedMemory object.
455  * @return rvalue for the empty locked const object of type T.
456  */
457  virtual LockedMemory<const T> readOnly() const noexcept {
458  return std::move(lockme<const T>());
459  }
460 
461  /**
462  * @brief Allocates or reallocates memory
463  */
464  void allocate() noexcept override {
465  if (_handle != nullptr) {
466  getAllocator()->free(_handle);
467  }
468  _handle = getAllocator()->alloc(size() * sizeof(T));
469  }
470 
471  /**
472  * @brief Frees all allocated data
473  */
474  bool deallocate() noexcept override {
475  return free();
476  }
477 
478  /**
479  * @brief Creates a new LockedMemory instance holding void pointer.
480  * @return LockedMemory instance holding void pointer
481  */
482  LockedMemory<void> buffer() noexcept override {
483  return std::move(lockme<void>());
484  }
485 
486  /**
487  * @brief Creates a new LockedMemory instance holding constant void pointer.
488  * @return LockedMemory instance holding constant void pointer
489  */
490  LockedMemory<const void> cbuffer() const noexcept override {
491  return std::move(lockme<const void>());
492  }
493 
494  /**
495  * @brief Gets BlobIterator for the data.
496  * Enables a ranged loop support for the TBlob object.
497  * @return BlobIterator object of type T
498  */
499  details::BlobIterator<T> begin() {
500  return details::BlobIterator<T>(data());
501  }
502 
503  /**
504  * @brief Gets BlobIterator for the end of data.
505  * Enables a ranged loop support for the TBlob object.
506  * @return BlobIterator object of type T representing end of the data
507  */
508  details::BlobIterator<T> end() {
509  return details::BlobIterator<T>(data(), size());
510  }
511 
512  /**
513  * @brief Gets a const BlobIterator for the read-only data.
514  * Enables a ranged loop support for the TBlob object.
515  * @return BlobIterator object of type const T
516  */
517  details::BlobIterator<const T> begin() const {
518  return details::BlobIterator<const T>(readOnly());
519  }
520 
521  /**
522  * @brief Gets a const BlobIterator for the end of read-only data.
523  * Enables a ranged loop support for the TBlob object.
524  * @return BlobIterator object of type const T representing end of data
525  */
526  details::BlobIterator<const T> end() const {
527  return details::BlobIterator<const T>(readOnly(), size());
528  }
529 
530 protected:
531  /**
532  * @brief Local instance of IAllocator to manipulate memory.
533  */
534  mutable std::shared_ptr<IAllocator> _allocator;
535 
536  /**
537  * @brief A handle for the stored memory returned from _allocator.alloc().
538  */
539  void* _handle = nullptr;
540 
541  /**
542  * @brief Copies dimensions and data from the TBlob object.
543  * @param blob object reference to copy from
544  */
545  void copyFrom(const TBlob<T>& blob) {
546  tensorDesc = blob.tensorDesc;
547  this->allocate();
548  auto memptr = data();
549  memcpy(memptr, blob.readOnly(), byteSize());
550  }
551 
552  /**
553  * @brief Swaps memory handlers between the current blob and the given one.
554  * @tparam U Type of the blob to move from
555  * @param blob TBlob instance to move from
556  */
557  template <class U>
558  void moveFrom(TBlob<U>& blob) {
559  tensorDesc = blob.tensorDesc;
560  this->_allocator = std::move(blob._allocator);
561  std::swap(this->_handle, blob._handle);
562  }
563 
564  /**
565  * @brief Frees handler and cleans up the stored data.
566  */
567  virtual bool free() {
568  bool bCanRelease = getAllocator()->free(_handle);
569  _handle = nullptr;
570  return bCanRelease;
571  }
572 
573  /**
574  * @brief Creates a LockedMemory instance.
575  * @tparam S Type of the LockedMemory to be created
576  * @return A created instance of LockedMemory
577  */
578  template <class S>
580  return LockedMemory<S>(_allocator.get(), _handle, 0);
581  }
582 
583  /**
584  * @brief Gets an allocator or creates a default one.
585  * @return IAllocator instance
586  */
587  const std::shared_ptr<IAllocator>& getAllocator() const noexcept override {
588  // in case when constructor without allocator was used
589  if (!_allocator) {
590  _allocator = shared_from_irelease(CreateDefaultAllocator());
591  }
592 
593  return _allocator;
594  }
595 
596  /**
597  * @brief Returns handle to the stored data.
598  */
599  void* getHandle() const noexcept override {
600  return _handle;
601  }
602 };
603 
604 #ifdef __clang__
605 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<float>);
606 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<double>);
607 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int16_t>);
608 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint16_t>);
609 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int8_t>);
610 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint8_t>);
611 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int>);
612 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long>);
613 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long long>);
614 #endif // __clang__
615 
616 /**
617  * @brief Creates a blob with the given tensor descriptor.
618  * @tparam Type Type of the shared pointer to be created
619  * @param tensorDesc Tensor descriptor for Blob creation
620  * @return A shared pointer to the newly created blob of the given type
621  */
622 template <typename Type>
624  if (!tensorDesc.getPrecision().hasStorageType<Type>())
625  THROW_IE_EXCEPTION << "Cannot make shared blob! "
626  << "The blob type cannot be used to store objects of current precision";
627  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
628 }
629 
630 /**
631  * @brief Creates a blob with the given tensor descriptor from the pointer to the pre-allocated memory.
632  * @tparam Type Type of the shared pointer to be created
633  * @param tensorDesc TensorDesc for Blob creation
634  * @param ptr Pointer to the pre-allocated memory
635  * @param size Length of the pre-allocated array
636  * @return A shared pointer to the newly created blob of the given type
637  */
638 template <typename Type>
639 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, Type* ptr,
640  size_t size = 0) {
641  if (!tensorDesc.getPrecision().hasStorageType<Type>())
642  THROW_IE_EXCEPTION << "Cannot make shared blob! "
643  << "The blob type cannot be used to store objects of current precision";
644  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
645 }
646 
647 /**
648  * @brief Creates a blob with the given tensor descriptor and allocator.
649  * @tparam Type Type of the shared pointer to be created
650  * @param tensorDesc Tensor descriptor for Blob creation
651  * @param alloc Shared pointer to IAllocator to use in the blob
652  * @return A shared pointer to the newly created blob of the given type
653  */
654 template <typename Type>
656  const TensorDesc& tensorDesc, const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
657  if (!tensorDesc.getPrecision().hasStorageType<Type>())
658  THROW_IE_EXCEPTION << "Cannot make shared blob! "
659  << "The blob type cannot be used to store objects of current precision";
660  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
661 }
662 
663 /**
664  * @brief Creates a copy of given TBlob instance.
665  * @tparam TypeTo Type of the shared pointer to be created
666  * @param arg given pointer to blob
667  * @return A shared pointer to the newly created blob of the given type
668  */
669 template <typename TypeTo>
671  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
672 }
673 
674 /**
675  * @brief Creates a Blob object of the specified type
676  * @param args Constructor arguments for the Blob object
677  * @return A shared pointer to the newly created Blob object
678  */
679 template <typename T, typename... Args, typename std::enable_if<std::is_base_of<Blob, T>::value, int>::type = 0>
680 std::shared_ptr<T> make_shared_blob(Args&&... args) {
681  return std::make_shared<T>(std::forward<Args>(args)...);
682 }
683 
684 /**
685  * @brief This structure describes ROI data.
686  */
687 struct ROI {
688  size_t id; // ID of a ROI
689  size_t posX; // W upper left coordinate of ROI
690  size_t posY; // H upper left coordinate of ROI
691  size_t sizeX; // W size of ROI
692  size_t sizeY; // H size of ROI
693 };
694 
695 /**
696  * @brief Creates a blob describing given ROI object based on the given blob with pre-allocated memory.
697  * @param inputBlob original blob with pre-allocated memory.
698  * @param roi A ROI object inside of the original blob.
699  * @return A shared pointer to the newly created blob.
700  */
701 INFERENCE_ENGINE_API_CPP(Blob::Ptr) make_shared_blob(const Blob::Ptr& inputBlob, const ROI& roi);
702 
703 } // namespace InferenceEngine
#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&#39;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