ie_plugin_dispatcher.hpp
Go to the documentation of this file.
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 */
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 {
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 {
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) {
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  */
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
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
InferenceEngine::details::SOPointer< IInferencePlugin > InferenceEnginePluginPtr
A C++ helper to work with objects created by the plugin. Implements different interfaces.
Definition: ie_plugin_ptr.hpp:50
TargetDevice
Describes known device types.
Definition: ie_device.hpp:23
Defines a message that contains a list of appropriate plugin names.
Definition: ie_device.hpp:116
file_name_t make_plugin_name(const file_name_t &path, const file_name_t &input) const
Creates path to the plugin.
Definition: ie_plugin_dispatcher.hpp:95
Definition: ie_argmax_layer.hpp:11
This class is a C++ API wrapper for IInferencePlugin. It can throw exceptions safely for the applicat...
Definition: ie_plugin_cpp.hpp:28
A header file contains a wrapper class for handling plugin instantiation and releasing resources...
InferenceEnginePluginPtr getSuitablePlugin(TargetDevice device) const
Loads a plugin from directories that is suitable for the device.
Definition: ie_plugin_dispatcher.hpp:69
Represents detailed information for an error.
Definition: ie_common.h:198
This is a class to load a suitable plugin.
Definition: ie_plugin_dispatcher.hpp:20
FindPluginResponse findPlugin(const FindPluginRequest &req)
Finds an appropriate plugin for requested target device.
InferencePlugin getPluginByDevice(const std::string &deviceName) const
Loads a plugin from directories that is suitable for the device string.
Definition: ie_plugin_dispatcher.hpp:50
PluginDispatcher(const std::vector< file_name_t > &pp={file_name_t()})
A constructor.
Definition: ie_plugin_dispatcher.hpp:26
char msg[256]
A character buffer that holds the detailed information for an error.
Definition: ie_common.h:202
This is a header file for the Inference Engine plugin C++ API.
virtual InferencePlugin getPluginByName(const file_name_t &name) const
Loads a plugin from plugin directories.
Definition: ie_plugin_dispatcher.hpp:33