63960599bffa5644720f4dad8b8186a63408340f
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / ie_blob_proxy.hpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #pragma once
7
8 /**
9  * @brief A header file for the TBlobProxy class definition
10  * @file ie_blob_proxy.hpp
11  */
12
13 #include "ie_blob.h"
14 #include <utility>
15 #include <memory>
16
17 namespace InferenceEngine {
18
19 /**
20  * @class TBlobProxy
21  * @brief This class enables creation of several blobs based on a single allocation but using different offsets for read/write
22  */
23 template<class T>
24 class TBlobProxy : public TBlob<T> {
25     using base = TBlob<T>;
26
27 public:
28     /**
29      * @brief A shared pointer to the TBlobProxy object
30      */
31     using Ptr = std::shared_ptr<TBlobProxy<T>>;
32
33     /**
34      * @brief A move constructor
35      * @param p Precision type
36      * @param l Layout
37      * @param blob Source TBlob object to move from. It is deleted after a constructor call, since the ownership is transferred to the proxy
38      * @param offset Offset in memory
39      * @param dims Dimensions of the given blob
40      */
41     TBlobProxy(Precision p, Layout l, TBlob <T> &&blob, size_t offset, const SizeVector &dims)
42             : base(p, l, dims), realObject(make_shared_blob<T>(std::move(blob))),
43               offset(offset * blob.element_size()) {
44         checkWindow();
45     }
46
47     /**
48      * @brief A move constructor
49      * @param p Precision type
50      * @param l Layout
51      * @param blob Source Blob object to move from. It is deleted after a constructor call, since the ownership is transferred to the proxy
52      * @param offset Offset in memory
53      * @param dims Dimensions of the given blob
54      */
55     TBlobProxy(Precision p, Layout l, const Blob::Ptr &blob, size_t offset, const SizeVector &dims)
56             : base(p, l, dims), realObject(blob), offset(offset * blob->element_size()) {
57         checkWindow();
58     }
59
60     /**
61      * A copy constructor
62      * @param p Precision type
63      * @param l Layout
64      * @param blobProxy Source TBlobProxy object to copy from
65      * @param offset Offset in memory
66      * @param dims Dimensions of the given blob
67      */
68     TBlobProxy(Precision p, Layout l, const TBlobProxy<T> &blobProxy, size_t offset, const SizeVector &dims)
69             : TBlob<T>(p, l, dims), realObject(blobProxy.realObject), offset(offset * sizeof(T)) {
70         checkWindow();
71     }
72
73     /**
74      * @brief Creates a new empty rvalue LockedMemory instance of type void
75      * @return LockedMemory instance of type void
76      */
77     LockedMemory<void> buffer() override {
78         return {getAllocator().get(), getHandle(), offset};
79     }
80
81     /**
82      * @brief Creates a new empty rvalue LockedMemory instance of type const void
83      * @return LockedMemory instance of type const void
84      */
85     LockedMemory<const void> cbuffer() const override {
86         return {getAllocator().get(), getHandle(), offset};
87     }
88
89     /**
90      * @brief Creates a LockedMemory instance of the given type
91      * @return LockedMemory instance of the given type
92      */
93     LockedMemory <T> data() override {
94         return {getAllocator().get(), getHandle(), offset};
95     }
96
97     /**
98     * @brief Creates a readOnly LockedMemory instance of the given type
99     * @return Read-only LockedMemory instance of the given type
100     */
101     LockedMemory<const T> readOnly() const override {
102         return {getAllocator().get(), getHandle(), offset};
103     }
104
105 protected:
106     /**
107      * @brief Gets an allocator
108      * @return An allocator instance
109      */
110     const std::shared_ptr<IAllocator> &getAllocator() const noexcept override {
111         return realObject->getAllocator();
112     }
113
114     /**
115      * @brief Gets a handle pointer
116      * @return A handle pointer
117      */
118     void *getHandle() const noexcept override {
119         return realObject->getHandle();
120     }
121
122     /**
123      * @brief Checks whether proxy can be created with the requested offset and size parameters
124      */
125     void checkWindow() {
126         if (realObject->size() * realObject->element_size() < base::size() * base::element_size() + offset) {
127             THROW_IE_EXCEPTION << "cannot create proxy, offsetInBytes=" << offset << ", sizeInBytes="
128                                << base::size() * base::element_size() << ", out of original object size="
129                                << realObject->size() * realObject->element_size();
130         }
131     }
132
133     /**
134      * @brief Allocates TBlobProxy data
135      * Always throws exception. Not intended to be used
136      */
137     void allocate() override {
138         THROW_IE_EXCEPTION
139                 << "Proxy blob are not to use allocate, it should rely on actual blob allocation it proxies";
140     }
141
142     /**
143      * @brief Deallocates TBlobProxy data
144      * Always throws exception. Not intended to be used
145      */
146     bool deallocate() override {
147         THROW_IE_EXCEPTION
148                 << "Proxy blob are not to use deallocate(), it should rely on actual blob allocation it proxies";
149     }
150
151 private:
152     typename Blob::Ptr realObject;
153     size_t offset;
154 };
155 }  // namespace InferenceEngine