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