Publishing R3
[platform/upstream/dldt.git] / inference-engine / include / ie_plugin_dispatcher.hpp
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 /**
7 * @brief A header for a class to handle plugin loading.
8 * @file ie_plugin_dispatcher.hpp
9 */
10 #pragma once
11
12 #include "ie_plugin_ptr.hpp"
13 #include <string>
14 #include <vector>
15 #include <cpp/ie_plugin_cpp.hpp>
16
17 namespace InferenceEngine {
18 /**
19 * @brief This is a class to load a suitable plugin
20 */
21 class PluginDispatcher {
22 public:
23     /**
24      * @brief A constructor
25      * @param pp Vector of paths to plugin directories
26      */
27     explicit PluginDispatcher(const std::vector<std::string> &pp) : pluginDirs(pp) {}
28
29     /**
30     * @brief Loads a plugin from plugin directories
31     * @param name Plugin name
32     * @return A pointer to the loaded plugin
33     */
34     virtual InferencePlugin getPluginByName(const std::string& name) const {
35         std::stringstream err;
36         for (auto &pluginPath : pluginDirs) {
37             try {
38                 return InferencePlugin(InferenceEnginePluginPtr(make_plugin_name(pluginPath, name)));
39             }
40             catch (const std::exception &ex) {
41                 err << "cannot load plugin: " << name << " from " << pluginPath << ": " << ex.what() << ", skipping\n";
42             }
43         }
44         THROW_IE_EXCEPTION << "Plugin " << name << " cannot be loaded: " << err.str() << "\n";
45     }
46
47     /**
48     * @brief Loads a plugin from directories that is suitable for the device string
49     * @return A pointer to the plugin
50     */
51     InferencePlugin getPluginByDevice(const std::string& deviceName) const {
52         InferenceEnginePluginPtr ptr;
53         // looking for HETERO: if can find, add everything after ':' to the options of hetero plugin
54         if (deviceName.find("HETERO:") == 0) {
55             ptr = getSuitablePlugin(InferenceEngine::TargetDeviceInfo::fromStr("HETERO"));
56             if (ptr) {
57                 InferenceEngine::ResponseDesc response;
58                 ptr->SetConfig({ { "TARGET_FALLBACK", deviceName.substr(7, deviceName.length() - 7) } }, &response);
59             }
60         } else {
61             ptr = getSuitablePlugin(InferenceEngine::TargetDeviceInfo::fromStr(deviceName));
62         }
63         return InferencePlugin(ptr);
64     }
65
66     /**
67     * @brief Loads a plugin from directories that is suitable for the device
68     * @return A pointer to the plugin
69     */
70     InferenceEnginePluginPtr getSuitablePlugin(TargetDevice device) const {
71         FindPluginResponse result;
72         ResponseDesc desc;
73         if (InferenceEngine::OK != findPlugin({ device }, result, &desc)) {
74             THROW_IE_EXCEPTION << desc.msg;
75         }
76
77         std::stringstream err;
78         for (std::string& name : result.names) {
79             try {
80                 return getPluginByName(name);
81             }
82             catch (const std::exception &ex) {
83                 err << "Tried load plugin : " << name << ",  error: " << ex.what() << "\n";
84             }
85         }
86         THROW_IE_EXCEPTION << "Cannot find plugin to use :" << err.str() << "\n";
87     }
88
89 protected:
90     /**
91     * @brief Creates path to the plugin
92     * @param path Path to the plugin
93     * @param input Plugin name
94     * @return The path to the plugin
95     */
96     std::string make_plugin_name(const std::string &path, const std::string &input) const {
97         std::string separator =
98 #if defined _WIN32 || defined __CYGWIN__
99         "\\";
100 #else
101         "/";
102 #endif
103         if (path.empty())
104             separator = "";
105 #ifdef _WIN32
106         return path + separator + input + ".dll";
107 #elif __APPLE__
108         return path + separator + "lib" + input + ".dylib";
109 #else
110         return path + separator + "lib" + input + ".so";
111 #endif
112     }
113
114 private:
115     std::vector<std::string> pluginDirs;
116 };
117 }  // namespace InferenceEngine