2eb235af55748ec49e3fe6aa82becd28a50c08d9
[platform/upstream/dldt.git] / inference-engine / include / cpp / ie_executable_network.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief A header file that provides wrapper classes for IExecutableNetwork
7  * @file ie_executable_network.hpp
8  */
9 #pragma once
10
11 #include <map>
12 #include <string>
13 #include <vector>
14 #include <memory>
15 #include <algorithm>
16 #include "ie_iexecutable_network.hpp"
17 #include "ie_plugin_ptr.hpp"
18 #include "cpp/ie_infer_request.hpp"
19 #include "cpp/ie_memory_state.hpp"
20 #include "cpp/ie_cnn_network.h"
21 #include "details/ie_exception_conversion.hpp"
22
23 namespace InferenceEngine {
24
25 /**
26  * @brief wrapper over IExecutableNetwork
27  */
28 class ExecutableNetwork {
29     IExecutableNetwork::Ptr actual;
30     InferenceEnginePluginPtr plg;
31
32 public:
33     ExecutableNetwork() = default;
34     ~ExecutableNetwork() {
35         actual = nullptr;
36     }
37
38     explicit ExecutableNetwork(IExecutableNetwork::Ptr actual, InferenceEnginePluginPtr plg = {})
39     : actual(actual), plg(plg) {}
40
41     /**
42      * @brief Wraps original method
43      * IExecutableNetwork::getOutputsInfo
44      */
45     ConstOutputsDataMap GetOutputsInfo() const {
46         ConstOutputsDataMap data;
47         CALL_STATUS_FNC(GetOutputsInfo, data);
48         return data;
49     }
50
51     /**
52      * @brief Wraps original method
53      * IExecutableNetwork::getInputsInfo
54      */
55     ConstInputsDataMap GetInputsInfo() const {
56         ConstInputsDataMap info;
57         CALL_STATUS_FNC(GetInputsInfo, info);
58         return info;
59     }
60
61     /**
62      * @brief reset owned object to new pointer, essential for cases when simultaneously loaded networks not expected
63      * @param actual actual pointed object
64      */
65     void reset(IExecutableNetwork::Ptr newActual) {
66         this->actual.swap(newActual);
67     }
68
69     /**
70      * @brief Wraps original method
71      * IExecutableNetwork::CreateInferRequest
72      */
73     InferRequest CreateInferRequest() {
74         IInferRequest::Ptr req;
75         CALL_STATUS_FNC(CreateInferRequest, req);
76         if (req.get() == nullptr) THROW_IE_EXCEPTION << "Internal error: pointer to infer request is null";
77         return InferRequest(req, plg);
78     }
79
80     /**
81      * @brief Wraps original method
82      * IExecutableNetwork::CreateInferRequestPtr
83      * @return shared pointer on InferRequest object
84      */
85     InferRequest::Ptr CreateInferRequestPtr() {
86         IInferRequest::Ptr req;
87         CALL_STATUS_FNC(CreateInferRequest, req);
88         return std::make_shared<InferRequest>(req, plg);
89     }
90
91     /**
92     * @brief Exports the current executable network so it can be used later in the Import() main API
93     * @param modelFileName Full path to the location of the exported file
94     * @param resp Optional: pointer to an already allocated object to contain information in case of failure
95     */
96     void Export(const std::string &modelFileName) {
97         CALL_STATUS_FNC(Export, modelFileName);
98     }
99
100     /**
101     * @brief Gets the mapping of IR layer names to implemented kernels
102     * @param deployedTopology Map of PrimitiveInfo objects that represent the deployed topology
103     * @param resp Optional: pointer to an already allocated object to contain information in case of failure
104     */
105     void GetMappedTopology(std::map<std::string, std::vector<PrimitiveInfo::Ptr>> &deployedTopology) {
106         CALL_STATUS_FNC(GetMappedTopology, deployedTopology);
107     }
108
109     /**
110     * cast operator is used when this wrapper initialized by LoadNetwork
111     * @return
112     */
113     operator IExecutableNetwork::Ptr &() {
114         return actual;
115     }
116
117     /**
118     * @brief Get executable graph information from a plugin represented as CNNNetwork
119     * @return CNNetwork containing Executable Graph Info
120     */
121     CNNNetwork GetExecGraphInfo() {
122         ICNNNetwork::Ptr ptr = nullptr;
123         CALL_STATUS_FNC(GetExecGraphInfo, ptr);
124         return CNNNetwork(ptr);
125     }
126
127     /**
128      *@brief see original function InferenceEngine::IExecutableNetwork::QueryState
129      */
130     std::vector<MemoryState> QueryState() {
131         IMemoryState::Ptr pState = nullptr;
132         auto res = OK;
133         std::vector<MemoryState> controller;
134         for (size_t idx = 0; res == OK; ++idx) {
135             ResponseDesc resp;
136             res = actual->QueryState(pState, idx, &resp);
137             if (res != OK && res != OUT_OF_BOUNDS) {
138                 THROW_IE_EXCEPTION << resp.msg;
139             }
140             if (res != OUT_OF_BOUNDS) {
141                 controller.push_back(MemoryState(pState));
142             }
143         }
144
145         return controller;
146     }
147
148     /**
149      * @brief Sets configuration for current executable network
150      * @param config Map of pairs: (config parameter name, config parameter value)
151      * @param resp Pointer to the response message that holds a description of an error if any occurred
152      */
153     void SetConfig(const std::map<std::string, Parameter> &config) {
154         CALL_STATUS_FNC(SetConfig, config);
155     }
156
157     /** @brief Gets configuration dedicated to plugin behaviour
158         * @param name - config key, can be found in ie_plugin_config.hpp
159         * @param options - configuration details for coonfig value
160         * @param result - value of config corresponding to config key
161         * @param resp Pointer to the response message that holds a description of an error if any occurred
162     */
163     Parameter GetConfig(const std::string &name) const {
164         Parameter configValue;
165         CALL_STATUS_FNC(GetConfig, name, configValue);
166         return configValue;
167     }
168
169     /**
170      * @brief Gets general runtime metric for dedicated hardware
171      * @param name  - metric name to request
172      * @param options - configuration details for metric
173      * @param result - metric value corresponding to metric key
174      * @param resp - Pointer to the response message that holds a description of an error if any
175      *             occurred
176      * @return code of the operation. OK if succeeded
177      */
178     Parameter GetMetric(const std::string &name) const {
179         Parameter metricValue;
180         CALL_STATUS_FNC(GetMetric, name, metricValue);
181         return metricValue;
182     }
183
184     using Ptr = std::shared_ptr<ExecutableNetwork>;
185 };
186
187 }  // namespace InferenceEngine