Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / hetero_plugin / hetero_device_loader.cpp
1 //
2 // Copyright (C) 2018-2019 Intel Corporation.
3 //
4 // This software and the related documents are Intel copyrighted materials,
5 // and your use of them is governed by the express license under which they
6 // were provided to you (End User License Agreement for the Intel(R) Software
7 // Development Products (Version May 2017)). Unless the License provides
8 // otherwise, you may not use, modify, copy, publish, distribute, disclose or
9 // transmit this software or the related documents without Intel's prior
10 // written permission.
11 //
12 // This software and the related documents are provided as is, with no
13 // express or implied warranties, other than those that are expressly
14 // stated in the License.
15 //
16
17 #include "hetero_device_loader.h"
18 #include "ie_plugin_dispatcher.hpp"
19 #include <string>
20 #include <stdio.h>
21 #include <map>
22 #include <vector>
23 #include <ie_plugin_config.hpp>
24
25 using namespace InferenceEngine;
26
27 StatusCode HeteroDeviceLoader::LoadNetwork(
28     const std::string &device,
29     IExecutableNetwork::Ptr &ret,
30     ICNNNetwork &network,
31     const std::map<std::string, std::string> &config,
32     ResponseDesc *resp) noexcept {
33     if (device != _deviceId) {
34         if (resp) {
35             std::string msg("Current HeteroDeviceLoader doesn't support device passed to LoadNetwork");
36             snprintf(resp->msg, msg.size(), "%s", msg.c_str());
37         }
38         return NETWORK_NOT_LOADED;
39     }
40     if (!_plugin) {
41         return NETWORK_NOT_LOADED;
42     }
43     // preparing local version of configs which are supported by plugins
44     std::map<std::string, std::string> tconfig;
45
46     for (auto c : config) {
47         std::map<std::string, std::string> oneconfig;
48         oneconfig[c.first] = c.second;
49         InferenceEngine::ResponseDesc response;
50         if (_plugin->SetConfig(oneconfig, &response) == OK) {
51             tconfig[c.first] = c.second;
52         }
53     }
54
55     return _plugin->LoadNetwork(ret, network, tconfig, resp);
56 }
57
58 void HeteroDeviceLoader::QueryNetwork(const std::string &device,
59                                       const ICNNNetwork &network,
60                                       QueryNetworkResult &res) noexcept {
61     QueryNetwork(device, network, {}, res);
62 }
63
64 void HeteroDeviceLoader::QueryNetwork(const std::string &device,
65                                       const ICNNNetwork &network,
66                                       const std::map<std::string, std::string>& config,
67                                       QueryNetworkResult &res) noexcept {
68     if (device != _deviceId) {
69         res.rc = GENERAL_ERROR;
70         std::string msg("Current HeteroDeviceLoader doesn't support device passed to QueryNetwork");
71         snprintf(res.resp.msg, msg.size(), "%s", msg.c_str());
72         return;
73     }
74     if (!_plugin) {
75         res.rc = GENERAL_ERROR;
76         std::string msg("No plugin, cannot execute QueryNetwork");
77         snprintf(res.resp.msg, msg.size(), "%s", msg.c_str());
78         return;
79     }
80     _plugin->QueryNetwork(network, config, res);
81 }
82
83 HeteroDeviceLoader::HeteroDeviceLoader(const std::string& deviceId) {
84     _deviceId = deviceId;
85     // try to create plugin
86     PluginDispatcher dispatcher({ "" });
87     _plugin = dispatcher.getPluginByDevice(_deviceId);
88 }
89
90 void HeteroDeviceLoader::initConfigs(const std::map<std::string, std::string> &config,
91                  const std::vector<InferenceEngine::IExtensionPtr> &extensions) {
92     if (_plugin) {
93         if (_deviceId == "CPU") {
94             for (auto &&ext : extensions) {
95                 _plugin->AddExtension(ext, nullptr);
96             }
97         }
98         auto copyConfig = config;
99         // preparing local version of configs which are supported by plugins
100         for (auto c : copyConfig) {
101             std::map<std::string, std::string> oneconfig;
102             oneconfig[c.first] = c.second;
103             try {
104                 _plugin->SetConfig(oneconfig, nullptr);
105             } catch (InferenceEngine::details::InferenceEngineException &e) {
106             }
107         }
108     }
109 }
110
111 void HeteroDeviceLoader::SetLogCallback(IErrorListener &listener) {
112     _plugin->SetLogCallback(listener);
113 }