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  if (_allocator == nullptr) THROW_IE_EXCEPTION << "TBlob allocator was not initialized.";
524  }
525 
526  /**
527  * @brief The copy constructor data is reallocated and copied from the source to the target blob.
528  *
529  * @param blob Source blob
530  */
531  TBlob(const TBlob<T>& blob): MemoryBlob(blob.getTensorDesc()) {
532  copyFrom(blob);
533  }
534 
535  /**
536  * @brief A move constructor.
537  *
538  * @param blob rvalue to make a move from
539  */
540  TBlob(TBlob<T>&& blob): MemoryBlob(blob.getTensorDesc()) {
541  moveFrom(blob);
542  }
543 
544  /**
545  * @brief Copy operator for the TBlob object.
546  *
547  * @param blob object reference to copy from
548  * @return Newly copied object
549  */
550  TBlob& operator=(const TBlob& blob) {
551  copyFrom(blob);
552  return *this;
553  }
554 
555  /**
556  *@brief Virtual destructor.
557  */
558 #ifdef __clang__
559  virtual ~TBlob();
560 #else
561  virtual ~TBlob() {
562  free();
563  }
564 #endif // __clang__
565 
566  /**
567  * @brief Gets the size of the given type.
568  *
569  * @return Size of the type
570  */
571  size_t element_size() const noexcept override {
572  return sizeof(T);
573  }
574 
575  /**
576  * @brief Creates an new empty rvalue LockedMemory object.
577  *
578  * @return rvalue for the empty locked object of type T
579  */
580  virtual LockedMemory<T> data() noexcept {
581  return std::move(lockme<T>());
582  }
583 
584  /**
585  * @brief Creates a new empty rvalue read-only LockedMemory object.
586  *
587  * @return rvalue for the empty locked const object of type T.
588  */
589  virtual LockedMemory<const T> readOnly() const noexcept {
590  return std::move(lockme<const T>());
591  }
592 
593  /**
594  * @brief Allocates or reallocates memory
595  */
596  void allocate() noexcept override {
597  if (_handle != nullptr) {
598  getAllocator()->free(_handle);
599  }
600  _handle = getAllocator()->alloc(size() * sizeof(T));
601  }
602 
603  /**
604  * @brief Frees all allocated data
605  */
606  bool deallocate() noexcept override {
607  return free();
608  }
609 
610  /**
611  * @brief Creates a new LockedMemory instance holding void pointer.
612  *
613  * @return LockedMemory instance holding void pointer
614  */
615  LockedMemory<void> buffer() noexcept override {
616  return std::move(lockme<void>());
617  }
618 
619  /**
620  * @brief Creates a new LockedMemory instance holding constant void pointer.
621  *
622  * @return LockedMemory instance holding constant void pointer
623  */
624  LockedMemory<const void> cbuffer() const noexcept override {
625  return std::move(lockme<const void>());
626  }
627 
628  LockedMemory<void> rwmap()noexcept override {
629  return std::move(lockme<void>());
630  }
631 
632  LockedMemory<const void> rmap() const noexcept override {
633  return std::move(lockme<const void>());
634  }
635  LockedMemory<void> wmap()noexcept override {
636  return std::move(lockme<void>());
637  }
638 
639  /**
640  * @brief Gets BlobIterator for the data.
641  *
642  * Enables a ranged loop support for the TBlob object.
643  *
644  * @return BlobIterator object of type T
645  */
646  details::BlobIterator<T> begin() {
647  return details::BlobIterator<T>(data());
648  }
649 
650  /**
651  * @brief Gets BlobIterator for the end of data.
652  *
653  * Enables a ranged loop support for the TBlob object.
654  *
655  * @return BlobIterator object of type T representing end of the data
656  */
657  details::BlobIterator<T> end() {
658  return details::BlobIterator<T>(data(), size());
659  }
660 
661  /**
662  * @brief Gets a const BlobIterator for the read-only data.
663  *
664  * Enables a ranged loop support for the TBlob object.
665  *
666  * @return BlobIterator object of type const T
667  */
668  details::BlobIterator<const T> begin() const {
669  return details::BlobIterator<const T>(readOnly());
670  }
671 
672  /**
673  * @brief Gets a const BlobIterator for the end of read-only data.
674  *
675  * Enables a ranged loop support for the TBlob object.
676  *
677  * @return BlobIterator object of type const T representing end of data
678  */
679  details::BlobIterator<const T> end() const {
680  return details::BlobIterator<const T>(readOnly(), size());
681  }
682 
683 protected:
684  /**
685  * @brief Local instance of IAllocator to manipulate memory.
686  */
687  mutable std::shared_ptr<IAllocator> _allocator;
688 
689  /**
690  * @brief A handle for the stored memory returned from _allocator.alloc().
691  */
692  void* _handle = nullptr;
693 
694  /**
695  * @brief Copies dimensions and data from the TBlob object.
696  *
697  * @param blob object reference to copy from
698  */
699  void copyFrom(const TBlob<T>& blob) {
700  tensorDesc = blob.tensorDesc;
701  this->allocate();
702  auto memptr = data();
703  memcpy(memptr, blob.readOnly(), byteSize());
704  }
705 
706  /**
707  * @brief Swaps memory handlers between the current blob and the given one.
708  *
709  * @tparam U Type of the blob to move from
710  * @param blob TBlob instance to move from
711  */
712  template <class U>
713  void moveFrom(TBlob<U>& blob) {
714  tensorDesc = blob.tensorDesc;
715  this->_allocator = std::move(blob._allocator);
716  std::swap(this->_handle, blob._handle);
717  }
718 
719  /**
720  * @brief Frees handler and cleans up the stored data.
721  */
722  virtual bool free() {
723  bool bCanRelease = getAllocator()->free(_handle);
724  _handle = nullptr;
725  return bCanRelease;
726  }
727 
728  /**
729  * @brief Creates a LockedMemory instance.
730  *
731  * @tparam S Type of the LockedMemory to be created
732  * @return A created instance of LockedMemory
733  */
734  template <class S>
736  return LockedMemory<S>(_allocator.get(), _handle, 0);
737  }
738 
739  /**
740  * @brief Gets an allocator or creates a default one.
741  *
742  * @return IAllocator instance
743  */
744  const std::shared_ptr<IAllocator>& getAllocator() const noexcept override {
745  // in case when constructor without allocator was used
746  if (!_allocator) {
747  _allocator = shared_from_irelease(CreateDefaultAllocator());
748  }
749 
750  return _allocator;
751  }
752 
753  /**
754  * @brief Returns handle to the stored data.
755  */
756  void* getHandle() const noexcept override {
757  return _handle;
758  }
759 };
760 
761 #ifdef __clang__
762 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<float>);
763 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<double>);
764 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int16_t>);
765 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint16_t>);
766 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int8_t>);
767 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint8_t>);
768 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<int>);
769 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long>);
770 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<long long>);
771 extern template class INFERENCE_ENGINE_API_CLASS(InferenceEngine::TBlob<uint64_t>);
772 #endif // __clang__
773 
774 /**
775  * @brief Creates a blob with the given tensor descriptor.
776  *
777  * @tparam Type Type of the shared pointer to be created
778  * @param tensorDesc Tensor descriptor for Blob creation
779  * @return A shared pointer to the newly created blob of the given type
780  */
781 template <typename Type>
783  if (!tensorDesc.getPrecision().hasStorageType<Type>())
784  THROW_IE_EXCEPTION << "Cannot make shared blob! "
785  << "The blob type cannot be used to store objects of current precision";
786  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
787 }
788 
789 /**
790  * @brief Creates a blob with the given tensor descriptor from the pointer to the pre-allocated memory.
791  *
792  * @tparam Type Type of the shared pointer to be created
793  * @param tensorDesc TensorDesc for Blob creation
794  * @param ptr Pointer to the pre-allocated memory
795  * @param size Length of the pre-allocated array
796  * @return A shared pointer to the newly created blob of the given type
797  */
798 template <typename Type>
799 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, Type* ptr,
800  size_t size = 0) {
801  if (!tensorDesc.getPrecision().hasStorageType<Type>())
802  THROW_IE_EXCEPTION << "Cannot make shared blob! "
803  << "The blob type cannot be used to store objects of current precision";
804  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
805 }
806 
807 /**
808  * @brief Creates a blob with the given tensor descriptor and allocator.
809  *
810  * @tparam Type Type of the shared pointer to be created
811  * @param tensorDesc Tensor descriptor for Blob creation
812  * @param alloc Shared pointer to IAllocator to use in the blob
813  * @return A shared pointer to the newly created blob of the given type
814  */
815 template <typename Type>
817  const TensorDesc& tensorDesc, const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
818  if (!tensorDesc.getPrecision().hasStorageType<Type>())
819  THROW_IE_EXCEPTION << "Cannot make shared blob! "
820  << "The blob type cannot be used to store objects of current precision";
821  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
822 }
823 
824 /**
825  * @brief Creates a copy of given TBlob instance.
826  *
827  * @tparam TypeTo Type of the shared pointer to be created
828  * @param arg given pointer to blob
829  * @return A shared pointer to the newly created blob of the given type
830  */
831 template <typename TypeTo>
833  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
834 }
835 
836 /**
837  * @brief Creates a Blob object of the specified type
838  *
839  * @param args Constructor arguments for the Blob object
840  * @return A shared pointer to the newly created Blob object
841  */
842 template <typename T, typename... Args, typename std::enable_if<std::is_base_of<Blob, T>::value, int>::type = 0>
843 std::shared_ptr<T> make_shared_blob(Args&&... args) {
844  return std::make_shared<T>(std::forward<Args>(args)...);
845 }
846 
847 /**
848  * @brief This structure describes ROI data.
849  */
850 struct ROI {
851  size_t id; //!< ID of a ROI
852  size_t posX; //!< W upper left coordinate of ROI
853  size_t posY; //!< H upper left coordinate of ROI
854  size_t sizeX; //!< W size of ROI
855  size_t sizeY; //!< H size of ROI
856 };
857 
858 /**
859  * @brief Creates a blob describing given ROI object based on the given blob with pre-allocated memory.
860  *
861  * @param inputBlob original blob with pre-allocated memory.
862  * @param roi A ROI object inside of the original blob.
863  * @return A shared pointer to the newly created blob.
864  */
865 INFERENCE_ENGINE_API_CPP(Blob::Ptr) make_shared_blob(const Blob::Ptr& inputBlob, const ROI& roi);
866 
867 } // 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:687
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:852
void moveFrom(TBlob< U > &blob)
Swaps memory handlers between the current blob and the given one.
Definition: ie_blob.h:713
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:850
size_t sizeX
W size of ROI.
Definition: ie_blob.h:854
void * getHandle() const noexcept override
Returns handle to the stored data.
Definition: ie_blob.h:756
std::vector< size_t > SizeVector
Represents tensor size.
Definition: ie_common.h:29
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:206
Definition: cldnn_config.hpp:16
size_t sizeY
H size of ROI.
Definition: ie_blob.h:855
std::shared_ptr< T > as(const Blob::Ptr &blob) noexcept
Helper cast function to work with shared Blob objects.
Definition: ie_blob.h:247
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:540
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:580
TBlob & operator=(const TBlob &blob)
Copy operator for the TBlob object.
Definition: ie_blob.h:550
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:218
details::BlobIterator< T > begin()
Gets BlobIterator for the data.
Definition: ie_blob.h:646
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:561
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:735
details::BlobIterator< T > end()
Gets BlobIterator for the end of data.
Definition: ie_blob.h:657
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:589
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
const std::shared_ptr< IAllocator > & getAllocator() const noexcept override
Gets an allocator or creates a default one.
Definition: ie_blob.h:744
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:531
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:628
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:571
void copyFrom(const TBlob< T > &blob)
Copies dimensions and data from the TBlob object.
Definition: ie_blob.h:699
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
InferenceEngine::TBlob< Type >::Ptr make_shared_blob(const TensorDesc &tensorDesc)
Creates a blob with the given tensor descriptor.
Definition: ie_blob.h:782
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:42
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:851
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:679
A header file for generic LockedMemory<> and different variations of locks.
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
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:668
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:615
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:632
void allocate() noexcept override
Allocates or reallocates memory.
Definition: ie_blob.h:596
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:635
void * _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:692
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
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:53
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:487
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:853
bool deallocate() noexcept override
Frees all allocated data.
Definition: ie_blob.h:606
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:624
A header file for the main Inference Engine exception.
virtual bool free()
Frees handler and cleans up the stored data.
Definition: ie_blob.h:722
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:93