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