1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header for a class to handle plugin loading.
7 * @file ie_plugin_dispatcher.hpp
11 #include "ie_plugin_ptr.hpp"
14 #include <cpp/ie_plugin_cpp.hpp>
16 namespace InferenceEngine {
18 * @brief This is a class to load a suitable plugin
20 class PluginDispatcher {
23 * @brief A constructor
24 * @param pp Vector of paths to plugin directories
26 explicit PluginDispatcher(const std::vector<file_name_t> &pp = {file_name_t()}) : pluginDirs(pp) {}
29 * @brief Loads a plugin from plugin directories
30 * @param name Plugin name
31 * @return A pointer to the loaded plugin
33 virtual InferencePlugin getPluginByName(const file_name_t& name) const {
34 std::stringstream err;
35 for (auto &pluginPath : pluginDirs) {
37 return InferencePlugin(InferenceEnginePluginPtr(make_plugin_name(pluginPath, name)));
39 catch (const std::exception &ex) {
40 err << "cannot load plugin: " << fileNameToString(name) << " from " << fileNameToString(pluginPath) << ": " << ex.what() << ", skipping\n";
43 THROW_IE_EXCEPTION << "Plugin " << fileNameToString(name) << " cannot be loaded: " << err.str() << "\n";
47 * @brief Loads a plugin from directories that is suitable for the device string
48 * @return A pointer to the plugin
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"));
56 InferenceEngine::ResponseDesc response;
57 ptr->SetConfig({ { "TARGET_FALLBACK", deviceName.substr(7, deviceName.length() - 7) } }, &response);
60 ptr = getSuitablePlugin(InferenceEngine::TargetDeviceInfo::fromStr(deviceName));
62 return InferencePlugin(ptr);
66 * @brief Loads a plugin from directories that is suitable for the device
67 * @return A pointer to the plugin
69 InferenceEnginePluginPtr getSuitablePlugin(TargetDevice device) const {
70 FindPluginResponse result;
72 if (InferenceEngine::OK != findPlugin({ device }, result, &desc)) {
73 THROW_IE_EXCEPTION << desc.msg;
76 std::stringstream err;
77 for (std::string& name : result.names) {
79 return getPluginByName(stringToFileName(name));
81 catch (const std::exception &ex) {
82 err << "Tried load plugin : " << name << ", error: " << ex.what() << "\n";
85 THROW_IE_EXCEPTION << "Cannot find plugin to use :" << err.str() << "\n";
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
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__
107 separator = file_name_t();
109 return path + separator + input +
116 return path + separator + "lib" + input + ".dylib";
118 return path + separator + "lib" + input + ".so";
124 std::vector<file_name_t> pluginDirs;
126 } // namespace InferenceEngine