ie_blob.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for Blob and generic TBlob<>
7  *
8  * @file ie_blob.h
9  */
10 #pragma once
11 
12 #include <cstring>
13 #include <functional>
14 #include <map>
15 #include <memory>
16 #include <numeric>
17 #include <string>
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21 
22 #include "ie_allocator.hpp"
23 #include "ie_common.h"
24 #include "ie_layouts.h"
25 #include "ie_locked_memory.hpp"
26 #include "ie_precision.hpp"
28 #include "details/ie_exception.hpp"
30 
31 namespace InferenceEngine {
32 
33 /**
34  * @brief This class represents a universal container in the Inference Engine
35  *
36  * @note Each Blob implementation must be derived from this Blob class directly or indirectly
37  */
38 class INFERENCE_ENGINE_API_CLASS(Blob) {
39 public:
40  /**
41  * @brief A smart pointer containing Blob object
42  */
43  using Ptr = std::shared_ptr<Blob>;
44 
45  /**
46  * @brief A smart pointer to the const Blob object
47  */
48  using CPtr = std::shared_ptr<const Blob>;
49 
50  /**
51  * @brief Creates a TBlob<> object from a Data node
52  *
53  * @param data A reference to a smart pointer of the Data node
54  * @return Smart pointer to TBlob<> with the relevant C type to the precision of the data node
55  */
56  static Ptr CreateFromData(const DataPtr& data);
57 
58  /**
59  * @brief Blob virtual destructor
60  */
61  virtual ~Blob();
62 
63  /**
64  * @brief Checks if the Blob object can be cast to the type T*
65  *
66  * @tparam T Type to be checked. Must represent a class derived from the Blob
67  * @return true if this object can be dynamically cast to the type T*. Otherwise, false
68  */
69  template <typename T,
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>
72  bool is() noexcept {
73  return dynamic_cast<T*>(this) != nullptr;
74  }
75 
76  /**
77  * @brief Checks if the Blob object can be cast to the type const T*
78  *
79  * @tparam T Type to be checked. Must represent a class derived from the Blob
80  * @return true if this object can be dynamically cast to the type const T*. Otherwise, false
81  */
82  template <typename T,
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;
87  }
88 
89  /**
90  * @brief Casts this Blob object to the type T*.
91  *
92  * Use InferenceEngine::as() to operate with shared Blob objects instead of raw pointers
93  *
94  * @tparam T Type to cast to. Must represent a class derived from the Blob
95  * @return Raw pointer to the object of the type T or nullptr on error
96  */
97  template <typename T,
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>
100  T* as() noexcept {
101  return dynamic_cast<T*>(this);
102  }
103 
104  /**
105  * @brief Casts this Blob object to the type const T*.
106  *
107  * Use InferenceEngine::as() to operate with shared Blob objects instead of raw pointers
108  *
109  * @tparam T Type to cast to. Must represent a class derived from the Blob
110  * @return Raw pointer to the object of the type const T or nullptr on error
111  */
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);
117  }
118 
119  /**
120  * @brief Constructor. Creates an empty Blob object with the specified precision.
121  *
122  * @param tensorDesc Defines the layout and dims of the blob
123  */
124  explicit Blob(const TensorDesc& tensorDesc): tensorDesc(tensorDesc) {}
125 
126  /**
127  * @brief Returns the tensor description
128  */
129  virtual const TensorDesc& getTensorDesc() const noexcept {
130  return tensorDesc;
131  }
132 
133  /**
134  * @brief Returns the tensor description
135  */
136  virtual TensorDesc& getTensorDesc() noexcept {
137  return tensorDesc;
138  }
139 
140  /**
141  * @brief By default, returns the total number of elements (a product of all the dims or 1 for scalar)
142  *
143  * Return value and its interpretation heavily depend on the blob type
144  */
145  virtual size_t size() const noexcept {
146  if (tensorDesc.getLayout() == Layout::SCALAR) return 1;
147  return product(tensorDesc.getDims());
148  }
149 
150  /**
151  * @brief Returns the size of the current Blob in bytes.
152  */
153  virtual size_t byteSize() const noexcept {
154  return size() * element_size();
155  }
156 
157  /**
158  * @deprecated Cast to MemoryBlob and use its API instead.
159  * Blob class can represent compound blob, which do not refer to the only solid memory.
160  *
161  * @brief Returns the number of bytes per element.
162  *
163  * The overall Blob capacity is size() * element_size(). Abstract method.
164  */
165  virtual size_t element_size() const noexcept = 0;
166 
167  /**
168  * @brief Allocates memory to store the data.
169  *
170  * Abstract method.
171  */
172  virtual void allocate() noexcept = 0;
173 
174  /**
175  * @brief Releases previously allocated data.
176  *
177  * Abstract method.
178  */
179  virtual bool deallocate() noexcept = 0;
180 
181  /**
182  * @deprecated Cast to MemoryBlob and use new wlock/rwlock API instead.
183  * Blob class can represent compound blob, which do not refer to the only solid memory.
184  * @brief Gets access to the allocated memory.
185  *
186  * Abstract method.
187  *
188  * @return A LockedMemory object
189  */
190  virtual LockedMemory<void> buffer() noexcept = 0;
191 
192  /**
193  * @deprecated Cast to MemoryBlob and use new MemoryBlob::rmap() function instead.
194  * Blob class can represent compound blob, which do not refer to the only solid memory.
195  * @brief Gets read-only access to the allocated memory.
196  *
197  * Abstract method.
198  *
199  * @return A LockedMemory object
200  */
201  virtual LockedMemory<const void> cbuffer() const noexcept = 0;
202 
203  /**
204  * @brief Creates a blob describing given ROI object based on the current blob with memory sharing.
205  *
206  * Note: default implementation throws "not implemented" exception.
207  *
208  * @param roi A ROI object inside of the current blob.
209  *
210  * @return A shared pointer to the newly created ROI blob.
211  */
212  virtual Blob::Ptr createROI(const ROI& roi) const;
213 
214 protected:
215  /**
216  * @brief The tensor descriptor of the given blob.
217  */
218  TensorDesc tensorDesc;
219 
220  /**
221  * @deprecated Cast to MemoryBlob and use its API instead.
222  * @brief Multiplies the dimension vector values.
223  *
224  * @param dims Reference to a vector with dimension values of type size_t
225  * @return Result of multiplication
226  */
227  static size_t product(const SizeVector& dims) noexcept {
228  if (dims.empty()) return 0;
229  return std::accumulate(std::begin(dims), std::end(dims), (size_t)1, std::multiplies<size_t>());
230  }
231 
232  /**
233  * @brief Gets an allocator for allocator-based blobs
234  *
235  * @return The allocator for allocator-based blobs or nullptr if there is none
236  */
237  virtual const std::shared_ptr<IAllocator>& getAllocator() const noexcept = 0;
238 
239  /**
240  * @brief Gets a handle to allocated memory
241  *
242  * @return The handle to allocated memory for allocator-based blobs or nullptr if there is none
243  */
244  virtual void* getHandle() const noexcept = 0;
245 
246  template <typename>
247  friend class TBlobProxy;
248 };
249 
250 /**
251  * @brief Helper cast function to work with shared Blob objects
252  *
253  * @return shared_ptr to the type T. Returned shared_ptr shares ownership of the object with the
254  * input Blob::Ptr
255  */
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);
261 }
262 
263 /**
264  * @brief Helper cast function to work with shared Blob objects
265  *
266  * @return shared_ptr to the type const T. Returned shared_ptr shares ownership of the object with
267  * the input Blob::Ptr
268  */
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>
272 std::shared_ptr<const T> as(const Blob::CPtr& blob) noexcept {
273  return std::dynamic_pointer_cast<const T>(blob);
274 }
275 
276 /**
277  * @brief This class implements a container object that represents a tensor in memory (host and
278  * remote/accelerated)
279  *
280  * @note Any Blob implementation that represents a concept of a tensor in memory (for example,
281  * TBlob) must be a subclass of MemoryBlob instead of Blob
282  */
283 class INFERENCE_ENGINE_API_CLASS(MemoryBlob): public Blob {
284 public:
285  /**
286  * @brief A smart pointer to the MemoryBlob object
287  */
288  using Ptr = std::shared_ptr<MemoryBlob>;
289 
290  /**
291  * @brief A smart pointer to the const MemoryBlob object
292  */
293  using CPtr = std::shared_ptr<const MemoryBlob>;
294 
295  /**
296  * @brief MemoryBlob virtual destructor
297  */
298  virtual ~MemoryBlob();
299 
300  /**
301  * @brief Constructor. Creates an empty MemoryBlob object with the specified precision.
302  *
303  * @param tensorDesc Defines the layout and dims of the blob
304  */
305  explicit MemoryBlob(const TensorDesc& tensorDesc): Blob(tensorDesc) {}
306 
307  /**
308  * @brief Returns the tensor description
309  */
310  const TensorDesc& getTensorDesc() const noexcept override {
311  return tensorDesc;
312  }
313 
314  /**
315  * @brief Returns the tensor description
316  */
317  TensorDesc& getTensorDesc() noexcept override {
318  return tensorDesc;
319  }
320 
321  /**
322  * @brief Returns the total number of elements, which is a product of all the dimensions
323  */
324  size_t size() const noexcept override {
325  if (tensorDesc.getLayout() == Layout::SCALAR) return 1;
326  return product(tensorDesc.getDims());
327  }
328 
329  /**
330  * @brief Returns the size of the current Blob in bytes calculated as `size() * element_size()`.
331  * @return Blob's size in bytes
332  */
333  size_t byteSize() const noexcept override {
334  return size() * element_size();
335  }
336 
337  /**
338  * @brief Provides the number of bytes per element.
339  * Abstract method.
340  * @return The number of bytes per element.
341  */
342  size_t element_size() const noexcept override = 0;
343 
344  /**
345  * @brief Allocates memory to store the data.
346  *
347  * Abstract method.
348  */
349  void allocate() noexcept override = 0;
350 
351  /**
352  * @brief Releases previously allocated data.
353  *
354  * Abstract method.
355  * @return `True` if deallocation happens successfully, `false` otherwise.
356  */
357  bool deallocate() noexcept override = 0;
358 
359  /**
360  * @deprecated Use wmap() or rwmap() API instead.
361  * @brief Gets access to the allocated memory.
362  *
363  * Abstract method.
364  *
365  * @return A LockedMemory object
366  */
367  LockedMemory<void> buffer() noexcept override = 0;
368 
369  /**
370  * @deprecated Use rmap() function instead.
371  * @brief Gets read-only access to the allocated memory.
372  *
373  * Abstract method.
374  *
375  * @return A LockedMemory object
376  */
377  LockedMemory<const void> cbuffer() const noexcept override = 0;
378 
379  /**
380  * @brief Gets read/write access to the memory in virtual space of the process.
381  * The function returns object which retains mapped memory.
382  * The memory been addressed in the MemoryBlob in general case can be allocated on remote device.
383  * This function maps remote memory to the memory in the virtual process space and after destruction
384  * of the LockedMemory will upload changed content to the accelerator.
385  *
386  * To avoid extra copy of data, you can use rmap() and wmap() functions.
387  *
388  * In case of memory originally allocated on the host, this function returns LockedMemory which will
389  * transparently refer to original memory address. No extra copy will happen
390  *
391  * In general case, pointer received from that LockedMemory becomes invalid just after
392  * destruction of LockedMemory instance. Keep Locked memory alive while you need to address memory
393  * in the process on the host.
394  *
395  * Abstract method.
396  *
397  * @return A LockedMemory object
398  */
399  virtual LockedMemory<void> rwmap()noexcept = 0;
400 
401  /**
402  * @brief Gets read only access to the memory in virtual space of the process.
403  * The function returns object which retains mapped memory.
404  *
405  * The memory been addressed in the MemoryBlob in general case can be allocated on remote device.
406  * This function copies remote memory to the memory in the virtual process space and after
407  * destruction of the LockedMemory it will not upload host memory back, bacause it is expected that
408  * content is not changed.
409  *
410  * To have an ability change content, you can use rwmap() and wmap() functions.
411  *
412  * In case of memory originally allocated on the host, this function returns LockedMemory which will
413  * transparently refer to original memory address. No extra copy will happen
414  *
415  * In general case, pointer received from that LockedMemory becomes invalid just after destruction
416  * of LockedMemory instance. Keep Locked memory alive while you need to address memory in the
417  * process on the host.
418  *
419  * Abstract method.
420  *
421  * @return A LockedMemory object
422  */
423  virtual LockedMemory<const void> rmap()const noexcept = 0;
424 
425  /**
426  * @brief Gets "write only direction" access to the memory in virtual space of the process.
427  * The function returns object which retains memory to be uploaded on device.
428  *
429  * The memory been addressed in the MemoryBlob in general case can be allocated on remote device.
430  * This function does not copy of the content from the device to the memory in the virtual process
431  * space, the content of the memory just after calling of this functin is not specified. After
432  * destruction of the LockedMemory, content will be upload host memory.
433  * In the same time there is no abilities to restrict reading from the memory, you need to care of
434  * reading from memory got by wmap(), it might have sence in some cases like filling of content and
435  * before uploading to device
436  *
437  * To access data stored in the blob, you can use rwmap() and rmap() functions.
438  *
439  * In case of memory originally allocated on the host, this function returns LockedMemory which will
440  * transparently refer to original memory address. No extra copy will happen
441  *
442  * In general case, pointer received from that LockedMemory becomes invalid just after destruction
443  * of LockedMemory instance. Keep Locked memory alive while you need to address memory in the
444  * process on the host.
445  *
446  * Abstract method.
447  *
448  * @return A LockedMemory object
449  */
450  virtual LockedMemory<void> wmap()noexcept = 0;
451 
452 protected:
453  /**
454  * @brief Gets the allocator for allocator-based blobs.
455  *
456  * @return The allocator for allocator-based blobs or if there is none then a nullptr.
457  */
458  const std::shared_ptr<IAllocator>& getAllocator() const noexcept override = 0;
459 
460  /**
461  * @brief Gets the handle to allocated memory.
462  *
463  * @return The handle to allocated memory for allocator-based blobs or if there is none then a nullptr.
464  */
465  void* getHandle() const noexcept override = 0;
466 
467  template <typename>
468  friend class TBlobProxy;
469 };
470 
471 /**
472  * @brief This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance).
473  */
474 using BlobMap = std::map<std::string, Blob::Ptr>;
475 
476 /**
477  * @brief Represents real host memory allocated for a Tensor/Blob per C type.
478  */
479 template <typename T, typename = std::enable_if<std::is_pod<T>::value>>
480 class TBlob : public MemoryBlob {
481  template <typename, typename>
482  friend class TBlob;
483 
484 public:
485  /**
486  * @brief Smart Pointer to this TBlob object.
487  */
488  using Ptr = std::shared_ptr<TBlob<T>>;
489 
490  /**
491  * @brief Creates a TBlob object with the specified dimensions and layout but does not allocate the memory.
492  *
493  * Use the allocate() method to allocate memory.
494  *
495  * @param tensorDesc Tensor description
496  */
497  explicit TBlob(const TensorDesc& tensorDesc): MemoryBlob(tensorDesc) {}
498 
499  /**
500  * @brief The constructor creates a TBlob object with the specified dimensions and layout
501  * on the pre-allocated memory.
502  *
503  * The allocate() call is not required.
504  *
505  * @param tensorDesc Tensor description
506  * @param ptr Pointer to the pre-allocated memory
507  * @param data_size Length of the pre-allocated array. If not set, size is assumed equal
508  * to the dot product of dims.
509  */
510  TBlob(const TensorDesc& tensorDesc, T* ptr, size_t data_size = 0): MemoryBlob(tensorDesc) {
511  if (data_size == 0) {
512  data_size = size();
513  }
514 
515  if (data_size != 0 && ptr == nullptr) {
516  THROW_IE_EXCEPTION << "Using Blob on external nullptr memory";
517  }
518 
519  _allocator = details::make_pre_allocator(ptr, data_size);
520  // blob on attached memory is always allocated, so we are not forcing the user to call allocate()
521  allocate();
522  }
523 
524  /**
525  * @brief Creates a TBlob object with the specified dimensions, layout and custom memory allocator but does not
526  * allocate the memory.
527  *
528  * @param tensorDesc Tensor description
529  * @param alloc An allocator
530  */
531  TBlob(const TensorDesc& tensorDesc, const std::shared_ptr<IAllocator>& alloc)
532  : MemoryBlob(tensorDesc), _allocator(alloc) {
533  if (_allocator == nullptr) THROW_IE_EXCEPTION << "TBlob allocator was not initialized.";
534  }
535 
536  /**
537  * @brief The copy constructor data is reallocated and copied from the source to the target blob.
538  *
539  * @param blob Source blob
540  */
541  TBlob(const TBlob<T>& blob): MemoryBlob(blob.getTensorDesc()) {
542  copyFrom(blob);
543  }
544 
545  /**
546  * @brief A move constructor.
547  *
548  * @param blob rvalue to make a move from
549  */
550  TBlob(TBlob<T>&& blob): MemoryBlob(blob.getTensorDesc()) {
551  moveFrom(blob);
552  }
553 
554  /**
555  * @brief Copy operator for the TBlob object.
556  *
557  * @param blob object reference to copy from
558  * @return Newly copied object
559  */
560  TBlob& operator=(const TBlob& blob) {
561  copyFrom(blob);
562  return *this;
563  }
564 
565  /**
566  *@brief Virtual destructor.
567  */
568 #ifdef __clang__
569  virtual ~TBlob();
570 #else
571  virtual ~TBlob() {
572  free();
573  }
574 #endif // __clang__
575 
576  /**
577  * @brief Gets the size of the given type.
578  *
579  * @return Size of the type
580  */
581  size_t element_size() const noexcept override {
582  return sizeof(T);
583  }
584 
585  /**
586  * @brief Creates an new empty rvalue LockedMemory object.
587  *
588  * @return rvalue for the empty locked object of type T
589  */
590  virtual LockedMemory<T> data() noexcept {
591  return std::move(lockme<T>());
592  }
593 
594  /**
595  * @brief Creates a new empty rvalue read-only LockedMemory object.
596  *
597  * @return rvalue for the empty locked const object of type T.
598  */
599  virtual LockedMemory<const T> readOnly() const noexcept {
600  return std::move(lockme<const T>());
601  }
602 
603  /**
604  * @brief Allocates or reallocates memory
605  */
606  void allocate() noexcept override {
607  const auto allocator = getAllocator();
608  const auto rawHandle = allocator->alloc(size() * sizeof(T));
609 
610  if (rawHandle == nullptr) {
611  return;
612  }
613 
614  _handle.reset(
615  rawHandle,
616  [allocator](void* rawHandle) {
617  allocator->free(rawHandle);
618  });
619  }
620 
621  /**
622  * @brief Frees all allocated data
623  */
624  bool deallocate() noexcept override {
625  return free();
626  }
627 
628  /**
629  * @brief Creates a new LockedMemory instance holding void pointer.
630  *
631  * @return LockedMemory instance holding void pointer
632  */
633  LockedMemory<void> buffer() noexcept override {
634  return std::move(lockme<void>());
635  }
636 
637  /**
638  * @brief Creates a new LockedMemory instance holding constant void pointer.
639  *
640  * @return LockedMemory instance holding constant void pointer
641  */
642  LockedMemory<const void> cbuffer() const noexcept override {
643  return std::move(lockme<const void>());
644  }
645 
646  LockedMemory<void> rwmap()noexcept override {
647  return std::move(lockme<void>());
648  }
649 
650  LockedMemory<const void> rmap() const noexcept override {
651  return std::move(lockme<const void>());
652  }
653  LockedMemory<void> wmap()noexcept override {
654  return std::move(lockme<void>());
655  }
656 
657  Blob::Ptr createROI(const ROI& roi) const override {
658  return Blob::Ptr(new TBlob<T>(*this, roi));
659  }
660 
661  /**
662  * @brief Gets BlobIterator for the data.
663  *
664  * Enables a ranged loop support for the TBlob object.
665  *
666  * @return BlobIterator object of type T
667  */
668  details::BlobIterator<T> begin() {
669  return details::BlobIterator<T>(data());
670  }
671 
672  /**
673  * @brief Gets BlobIterator for the end of data.
674  *
675  * Enables a ranged loop support for the TBlob object.
676  *
677  * @return BlobIterator object of type T representing end of the data
678  */
679  details::BlobIterator<T> end() {
680  return details::BlobIterator<T>(data(), size());
681  }
682 
683  /**
684  * @brief Gets a const BlobIterator for the read-only data.
685  *
686  * Enables a ranged loop support for the TBlob object.
687  *
688  * @return BlobIterator object of type const T
689  */
690  details::BlobIterator<const T> begin() const {
691  return details::BlobIterator<const T>(readOnly());
692  }
693 
694  /**
695  * @brief Gets a const BlobIterator for the end of read-only data.
696  *
697  * Enables a ranged loop support for the TBlob object.
698  *
699  * @return BlobIterator object of type const T representing end of data
700  */
701  details::BlobIterator<const T> end() const {
702  return details::BlobIterator<const T>(readOnly(), size());
703  }
704 
705 protected:
706  /**
707  * @brief Local instance of IAllocator to manipulate memory.
708  */
709  mutable std::shared_ptr<IAllocator> _allocator;
710 
711  /**
712  * @brief A handle for the stored memory returned from _allocator.alloc().
713  */
714  std::shared_ptr<void> _handle;
715 
716  /**
717  * @brief Copies dimensions and data from the TBlob object.
718  *
719  * @param blob object reference to copy from
720  */
721  void copyFrom(const TBlob<T>& blob) {
722  tensorDesc = blob.tensorDesc;
723  this->allocate();
724  auto memptr = data();
725  memcpy(memptr, blob.readOnly(), byteSize());
726  }
727 
728  /**
729  * @brief Swaps memory handlers between the current blob and the given one.
730  *
731  * @tparam U Type of the blob to move from
732  * @param blob TBlob instance to move from
733  */
734  template <class U>
735  void moveFrom(TBlob<U>& blob) {
736  tensorDesc = blob.tensorDesc;
737  this->_allocator = std::move(blob._allocator);
738  std::swap(this->_handle, blob._handle);
739  }
740 
741  /**
742  * @brief Frees handler and cleans up the stored data.
743  */
744  virtual bool free() {
745  bool bCanRelease = _handle != nullptr;
746  _handle.reset();
747  return bCanRelease;
748  }
749 
750  /**
751  * @brief Creates a LockedMemory instance.
752  *
753  * @tparam S Type of the LockedMemory to be created
754  * @return A created instance of LockedMemory
755  */
756  template <class S>
758  return LockedMemory<S>(_allocator.get(), getHandle(), 0);
759  }
760 
761  /**
762  * @brief Gets an allocator or creates a default one.
763  *
764  * @return IAllocator instance
765  */
766  const std::shared_ptr<IAllocator>& getAllocator() const noexcept override {
767  // in case when constructor without allocator was used
768  if (!_allocator) {
769  _allocator = shared_from_irelease(CreateDefaultAllocator());
770  }
771 
772  return _allocator;
773  }
774 
775  /**
776  * @brief Returns handle to the stored data.
777  */
778  void* getHandle() const noexcept override {
779  return _handle.get();
780  }
781 
782  TBlob(const TBlob& origBlob, const ROI& roi) :
783  MemoryBlob(make_roi_desc(origBlob.getTensorDesc(), roi, true)),
784  _allocator(origBlob._allocator) {
785  IE_ASSERT(origBlob._handle != nullptr)
786  << "Original Blob must be allocated before ROI creation";
787 
788  _handle = origBlob._handle;
789  }
790 };
791 
792 #ifdef __clang__
793 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<float>);
794 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<double>);
795 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int8_t>);
796 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint8_t>);
797 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int16_t>);
798 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint16_t>);
799 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int32_t>);
800 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint32_t>);
801 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long>);
802 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long long>);
803 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<unsigned long>);
804 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<unsigned long long>);
805 #endif // __clang__
806 
807 /**
808  * @brief Creates a blob with the given tensor descriptor.
809  *
810  * @tparam Type Type of the shared pointer to be created
811  * @param tensorDesc Tensor descriptor for Blob creation
812  * @return A shared pointer to the newly created blob of the given type
813  */
814 template <typename Type>
816  if (!tensorDesc.getPrecision().hasStorageType<Type>())
817  THROW_IE_EXCEPTION << "Cannot make shared blob! "
818  << "The blob type cannot be used to store objects of current precision";
819  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
820 }
821 
822 /**
823  * @brief Creates a blob with the given tensor descriptor from the pointer to the pre-allocated memory.
824  *
825  * @tparam Type Type of the shared pointer to be created
826  * @param tensorDesc TensorDesc for Blob creation
827  * @param ptr Pointer to the pre-allocated memory
828  * @param size Length of the pre-allocated array
829  * @return A shared pointer to the newly created blob of the given type
830  */
831 template <typename Type>
832 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, Type* ptr,
833  size_t size = 0) {
834  if (!tensorDesc.getPrecision().hasStorageType<Type>())
835  THROW_IE_EXCEPTION << "Cannot make shared blob! "
836  << "The blob type cannot be used to store objects of current precision";
837  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
838 }
839 
840 /**
841  * @brief Creates a blob with the given tensor descriptor and allocator.
842  *
843  * @tparam Type Type of the shared pointer to be created
844  * @param tensorDesc Tensor descriptor for Blob creation
845  * @param alloc Shared pointer to IAllocator to use in the blob
846  * @return A shared pointer to the newly created blob of the given type
847  */
848 template <typename Type>
850  const TensorDesc& tensorDesc, const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
851  if (!tensorDesc.getPrecision().hasStorageType<Type>())
852  THROW_IE_EXCEPTION << "Cannot make shared blob! "
853  << "The blob type cannot be used to store objects of current precision";
854  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
855 }
856 
857 /**
858  * @brief Creates a copy of given TBlob instance.
859  *
860  * @tparam TypeTo Type of the shared pointer to be created
861  * @param arg given pointer to blob
862  * @return A shared pointer to the newly created blob of the given type
863  */
864 template <typename TypeTo>
866  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
867 }
868 
869 /**
870  * @brief Creates a Blob object of the specified type
871  *
872  * @param args Constructor arguments for the Blob object
873  * @return A shared pointer to the newly created Blob object
874  */
875 template <typename T, typename... Args, typename std::enable_if<std::is_base_of<Blob, T>::value, int>::type = 0>
876 std::shared_ptr<T> make_shared_blob(Args&&... args) {
877  return std::make_shared<T>(std::forward<Args>(args)...);
878 }
879 
880 /**
881  * @brief Creates a blob describing given ROI object based on the given blob with pre-allocated memory.
882  *
883  * @param inputBlob original blob with pre-allocated memory.
884  * @param roi A ROI object inside of the original blob.
885  * @return A shared pointer to the newly created blob.
886  */
887 INFERENCE_ENGINE_API_CPP(Blob::Ptr) make_shared_blob(const Blob::Ptr& inputBlob, const ROI& roi);
888 
889 } // namespace InferenceEngine
InferenceEngine::Blob::as
T * as() noexcept
Casts this Blob object to the type T*.
Definition: ie_blob.h:100
InferenceEngine::CreateDefaultAllocator
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
ie_precision.hpp
A header file that provides class for describing precision of data.
InferenceEngine::TBlob::begin
details::BlobIterator< T > begin()
Gets BlobIterator for the data.
Definition: ie_blob.h:668
ie_allocator.hpp
A header file that provides Allocator interface.
InferenceEngine::make_roi_desc
TensorDesc make_roi_desc(const TensorDesc &origDesc, const ROI &roi, bool useOrigMemDesc)
Creates a TensorDesc object for ROI.
InferenceEngine::TBlob::~TBlob
virtual ~TBlob()
Virtual destructor.
Definition: ie_blob.h:571
InferenceEngine::BlobMap
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
roi
Definition: ie_c_api.h:251
InferenceEngine::Blob::getTensorDesc
virtual TensorDesc & getTensorDesc() noexcept
Returns the tensor description.
Definition: ie_blob.h:136
InferenceEngine::Blob::size
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
InferenceEngine::Blob::~Blob
virtual ~Blob()
Blob virtual destructor.
InferenceEngine::TBlob::free
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:744
InferenceEngine::TBlob
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:480
InferenceEngine::TBlob::lockme
LockedMemory< S > lockme() const
Creates a LockedMemory instance.
Definition: ie_blob.h:757
InferenceEngine::LockedMemory
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:111
InferenceEngine::TBlob::TBlob
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
InferenceEngine::MemoryBlob
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:283
InferenceEngine::TBlob::element_size
size_t element_size() const noexcept override
Gets the size of the given type.
Definition: ie_blob.h:581
InferenceEngine::Blob::CPtr
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:48
InferenceEngine::TensorDesc
This class defines Tensor description.
Definition: ie_layouts.h:153
InferenceEngine::TBlob::copyFrom
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:721
InferenceEngine::Blob::CreateFromData
static Ptr CreateFromData(const DataPtr &data)
Creates a TBlob<> object from a Data node.
ie_locked_memory.hpp
A header file for generic LockedMemory<> and different variations of locks.
InferenceEngine::Blob::as
const T * as() const noexcept
Casts this Blob object to the type const T*.
Definition: ie_blob.h:115
InferenceEngine::TBlob::getHandle
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:778
InferenceEngine::MemoryBlob::size
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
InferenceEngine::TBlob::operator=
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:560
InferenceEngine::make_shared_blob
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:815
InferenceEngine::TBlob::TBlob
TBlob(TBlob< T > &&blob)
A move constructor.
Definition: ie_blob.h:550
InferenceEngine::TBlob::data
virtual LockedMemory< T > data() noexcept
Creates an new empty rvalue LockedMemory object.
Definition: ie_blob.h:590
InferenceEngine::TBlob::end
details::BlobIterator< const T > end() const
Gets a const BlobIterator for the end of read-only data.
Definition: ie_blob.h:701
InferenceEngine::TBlob::end
details::BlobIterator< T > end()
Gets BlobIterator for the end of data.
Definition: ie_blob.h:679
InferenceEngine::Blob::is
bool is() noexcept
Checks if the Blob object can be cast to the type T*.
Definition: ie_blob.h:72
InferenceEngine::TBlob::Ptr
std::shared_ptr< TBlob< T > > Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:488
ie_exception.hpp
A header file for the main Inference Engine exception.
InferenceEngine::IAllocator
Allocator concept to be used for memory management and is used as part of the Blob.
Definition: ie_allocator.hpp:29
InferenceEngine::TBlob::allocate
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:606
InferenceEngine::TBlob::readOnly
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:599
InferenceEngine::Precision::hasStorageType
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
InferenceEngine::TBlob::wmap
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
ie_common.h
This is a header file with common inference engine definitions.
InferenceEngine::TBlob::getAllocator
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:766
InferenceEngine::MemoryBlob::~MemoryBlob
virtual ~MemoryBlob()
MemoryBlob virtual destructor.
InferenceEngine::MemoryBlob::byteSize
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
InferenceEngine::SizeVector
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:27
InferenceEngine::TBlob::rwmap
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
InferenceEngine::LockedMemory< const T >
This class is for read-only segments.
Definition: ie_locked_memory.hpp:317
InferenceEngine::MemoryBlob::getTensorDesc
const TensorDesc & getTensorDesc() const noexcept override
Returns the tensor description.
Definition: ie_blob.h:310
InferenceEngine::Blob::Ptr
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:43
InferenceEngine::TBlob::TBlob
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
ie_layouts.h
A header file for data layouts and conversion between them.
InferenceEngine::Blob::getTensorDesc
virtual const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:129
THROW_IE_EXCEPTION
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
ie_pre_allocator.hpp
The header file defines utility PreAllocator class.
InferenceEngine::MemoryBlob::getTensorDesc
TensorDesc & getTensorDesc() noexcept override
Returns the tensor description.
Definition: ie_blob.h:317
ie_blob_iterator.hpp
A header file for the BlobIterator class.
InferenceEngine::DataPtr
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:37
InferenceEngine::Blob
This class represents a universal container in the Inference Engine.
Definition: ie_blob.h:38
InferenceEngine::TBlob::cbuffer
LockedMemory< const void > cbuffer() const noexcept override
Creates a new LockedMemory instance holding constant void pointer.
Definition: ie_blob.h:642
InferenceEngine::TBlob::deallocate
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:624
InferenceEngine::Blob::byteSize
virtual size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:153
InferenceEngine::Blob::element_size
virtual size_t element_size() const noexcept=0
Returns the number of bytes per element.
InferenceEngine::LockedMemory< void >
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:218
InferenceEngine::TBlob::rmap
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
InferenceEngine::TBlob::begin
details::BlobIterator< const T > begin() const
Gets a const BlobIterator for the read-only data.
Definition: ie_blob.h:690
InferenceEngine::TBlob::createROI
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
InferenceEngine::ROI
This structure describes ROI data for image-like tensors.
Definition: ie_layouts.h:324
InferenceEngine::Blob::Blob
Blob(const TensorDesc &tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:124
InferenceEngine::TBlob::_allocator
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:709
InferenceEngine::TBlob::buffer
LockedMemory< void > buffer() noexcept override
Creates a new LockedMemory instance holding void pointer.
Definition: ie_blob.h:633
InferenceEngine::TensorDesc::getPrecision
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:242
InferenceEngine::Blob::getAllocator
virtual const std::shared_ptr< IAllocator > & getAllocator() const noexcept=0
Gets an allocator for allocator-based blobs.
InferenceEngine::as
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:259
InferenceEngine::TBlob::moveFrom
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:735
InferenceEngine::Blob::is
bool is() const noexcept
Checks if the Blob object can be cast to the type const T*.
Definition: ie_blob.h:85
InferenceEngine::TBlob::TBlob
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
IE_ASSERT
#define IE_ASSERT(EXPRESSION)
Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise.
Definition: ie_exception.hpp:54
InferenceEngine::MemoryBlob::MemoryBlob
MemoryBlob(const TensorDesc &tensorDesc)
Constructor. Creates an empty MemoryBlob object with the specified precision.
Definition: ie_blob.h:305
InferenceEngine::TBlob::TBlob
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
InferenceEngine::TBlob::_handle
std::shared_ptr< void > _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:714
InferenceEngine::MemoryBlob::element_size
size_t element_size() const noexcept override=0
Provides the number of bytes per element. Abstract method.