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