1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
7 * @brief A header for a class to handle plugin loading.
8 * @file ie_plugin_dispatcher.hpp
12 #include "ie_plugin_ptr.hpp"
15 #include <cpp/ie_plugin_cpp.hpp>
17 namespace InferenceEngine {
19 * @brief This is a class to load a suitable plugin
21 class PluginDispatcher {
24 * @brief A constructor
25 * @param pp Vector of paths to plugin directories
27 explicit PluginDispatcher(const std::vector<std::string> &pp) : pluginDirs(pp) {}
30 * @brief Loads a plugin from plugin directories
31 * @param name Plugin name
32 * @return A pointer to the loaded plugin
34 virtual InferencePlugin getPluginByName(const std::string& name) const {
35 std::stringstream err;
36 for (auto &pluginPath : pluginDirs) {
38 return InferencePlugin(InferenceEnginePluginPtr(make_plugin_name(pluginPath, name)));
40 catch (const std::exception &ex) {
41 err << "cannot load plugin: " << name << " from " << pluginPath << ": " << ex.what() << ", skipping\n";
44 THROW_IE_EXCEPTION << "Plugin " << name << " cannot be loaded: " << err.str() << "\n";
48 * @brief Loads a plugin from directories that is suitable for the device string
49 * @return A pointer to the plugin
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"));
57 InferenceEngine::ResponseDesc response;
58 ptr->SetConfig({ { "TARGET_FALLBACK", deviceName.substr(7, deviceName.length() - 7) } }, &response);
61 ptr = getSuitablePlugin(InferenceEngine::TargetDeviceInfo::fromStr(deviceName));
63 return InferencePlugin(ptr);
67 * @brief Loads a plugin from directories that is suitable for the device
68 * @return A pointer to the plugin
70 InferenceEnginePluginPtr getSuitablePlugin(TargetDevice device) const {
71 FindPluginResponse result;
73 if (InferenceEngine::OK != findPlugin({ device }, result, &desc)) {
74 THROW_IE_EXCEPTION << desc.msg;
77 std::stringstream err;
78 for (std::string& name : result.names) {
80 return getPluginByName(name);
82 catch (const std::exception &ex) {
83 err << "Tried load plugin : " << name << ", error: " << ex.what() << "\n";
86 THROW_IE_EXCEPTION << "Cannot find plugin to use :" << err.str() << "\n";
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
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__
106 return path + separator + input + ".dll";
108 return path + separator + "lib" + input + ".dylib";
110 return path + separator + "lib" + input + ".so";
115 std::vector<std::string> pluginDirs;
117 } // namespace InferenceEngine