ie_istreams_executor.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  * @file ie_istreams_executor.hpp
7  * @brief A header file for Inference Engine Streams-based Executor Interface
8  */
9 
10 #pragma once
11 
12 #include <memory>
13 #include <vector>
14 #include <string>
15 
16 #include "ie_parameter.hpp"
18 
19 namespace InferenceEngine {
20 
21 /**
22  * @interface IStreamsExecutor
23  * @ingroup ie_dev_api_threading
24  * @brief Interface for Streams Task Executor. This executor groups worker threads into so-called `streams`.
25  * @par CPU
26  * The executor executes all parallel tasks using threads from one stream.
27  * With proper pinning settings it should reduce cache misses for memory bound workloads.
28  * @par NUMA
29  * On NUMA hosts GetNumaNodeId() method can be used to define the NUMA node of current stream
30  */
31 class INFERENCE_ENGINE_API_CLASS(IStreamsExecutor) : public ITaskExecutor {
32 public:
33  /**
34  * A shared pointer to IStreamsExecutor interface
35  */
36  using Ptr = std::shared_ptr<IStreamsExecutor>;
37 
38  /**
39  * @brief Defines thread binding type
40  */
41  enum ThreadBindingType : std::uint8_t {
42  NONE, //!< Don't bind threads
43  CORES, //!< Bind threads to cores
44  NUMA //!< Bind threads to NUMA nodes
45  };
46 
47  /**
48  * @brief Defines IStreamsExecutor configuration
49  */
50  struct INFERENCE_ENGINE_API_CLASS(Config) {
51  /**
52  * @brief Supported Configuration keys
53  * @return vector of supported configuration keys
54  */
55  std::vector<std::string> SupportedKeys();
56 
57  /**
58  * @brief Parses configuration key/value pair
59  * @param key configuration key
60  * @param value configuration values
61  */
62  void SetConfig(const std::string& key, const std::string& value);
63 
64  /**
65  * @brief Return configuration value
66  * @param key configuration key
67  * @return configuration value wrapped into Parameter
68  */
69  Parameter GetConfig(const std::string& key);
70 
71  /**
72  * @brief Create appropriate multithreaded configuration
73  * filing unconfigured values from initial configuration using hardware properties
74  * @param initial Inital configuration
75  * @return configured values
76  */
77  static Config MakeDefaultMultiThreaded(const Config& initial);
78 
79  std::string _name; //!< Used by `ITT` to name executor threads
80  int _streams = 1; //!< Number of streams.
81  int _threadsPerStream = 0; //!< Number of threads per stream that executes `ie_parallel` calls
82  ThreadBindingType _threadBindingType = ThreadBindingType::NONE; //!< Thread binding to hardware resource type. No binding by default
83  int _threadBindingStep = 1; //!< In case of @ref CORES binding offset type thread binded to cores with defined step
84  int _threadBindingOffset = 0; //!< In case of @ref CORES binding offset type thread binded to cores starting from offset
85  int _threads = 0; //!< Number of threads distributed between streams. Reserved. Should not be used.
86 
87  /**
88  * @brief A constructor with arguments
89  *
90  * @param[in] name The executor name
91  * @param[in] streams @copybrief Config::_streams
92  * @param[in] threadsPerStream @copybrief Config::_threadsPerStream
93  * @param[in] threadBindingType @copybrief Config::_threadBindingType
94  * @param[in] threadBindingStep @copybrief Config::_threadBindingStep
95  * @param[in] threadBindingOffset @copybrief Config::_threadBindingOffset
96  * @param[in] threads @copybrief Config::_threads
97  */
99  std::string name = "StreamsExecutor",
100  int streams = 1,
101  int threadsPerStream = 0,
102  ThreadBindingType threadBindingType = ThreadBindingType::NONE,
103  int threadBindingStep = 1,
104  int threadBindingOffset = 0,
105  int threads = 0) :
106  _name{name},
107  _streams{streams},
108  _threadsPerStream{threadsPerStream},
109  _threadBindingType{threadBindingType},
110  _threadBindingStep{threadBindingStep},
111  _threadBindingOffset{threadBindingOffset},
112  _threads{threads} {
113  }
114  };
115 
116  /**
117  * @brief A virtual destructor
118  */
119  ~IStreamsExecutor() override;
120 
121  /**
122  * @brief Return the index of current stream
123  * @return An index of current stream. Or throw exceptions if called not from stream thread
124  */
125  virtual int GetStreamId() = 0;
126 
127  /**
128  * @brief Return the id of current NUMA Node
129  * @return `ID` of current NUMA Node, or throws exceptions if called not from stream thread
130  */
131  virtual int GetNumaNodeId() = 0;
132 
133  /**
134  * @brief Execute the task in the current thread using streams executor configuration and constraints
135  * @param task A task to start
136  */
137  virtual void Execute(Task task) = 0;
138 };
139 
140 
141 
142 } // namespace InferenceEngine
InferenceEngine
Inference Engine Plugin API namespace.
InferenceEngine::IStreamsExecutor::NONE
@ NONE
Don't bind threads.
Definition: ie_istreams_executor.hpp:42
InferenceEngine::IStreamsExecutor::CORES
@ CORES
Bind threads to cores.
Definition: ie_istreams_executor.hpp:43
ie_itask_executor.hpp
A header file for Inference Engine Task Executor Interface.
ie_parameter.hpp
InferenceEngine::IStreamsExecutor::ThreadBindingType
ThreadBindingType
Defines thread binding type.
Definition: ie_istreams_executor.hpp:41
InferenceEngine::Parameter
InferenceEngine::IStreamsExecutor::Execute
virtual void Execute(Task task)=0
Execute the task in the current thread using streams executor configuration and constraints.
InferenceEngine::IStreamsExecutor::GetStreamId
virtual int GetStreamId()=0
Return the index of current stream.
InferenceEngine::IStreamsExecutor::Config
Defines IStreamsExecutor configuration.
Definition: ie_istreams_executor.hpp:50
InferenceEngine::IStreamsExecutor::Config::GetConfig
Parameter GetConfig(const std::string &key)
Return configuration value.
InferenceEngine::IStreamsExecutor::Config::SetConfig
void SetConfig(const std::string &key, const std::string &value)
Parses configuration key/value pair.
InferenceEngine::IStreamsExecutor::Config::Config
Config(std::string name="StreamsExecutor", int streams=1, int threadsPerStream=0, ThreadBindingType threadBindingType=ThreadBindingType::NONE, int threadBindingStep=1, int threadBindingOffset=0, int threads=0)
A constructor with arguments.
Definition: ie_istreams_executor.hpp:98
InferenceEngine::IStreamsExecutor::Config::SupportedKeys
std::vector< std::string > SupportedKeys()
Supported Configuration keys.
InferenceEngine::IStreamsExecutor::GetNumaNodeId
virtual int GetNumaNodeId()=0
Return the id of current NUMA Node.
InferenceEngine::IStreamsExecutor::Config::MakeDefaultMultiThreaded
static Config MakeDefaultMultiThreaded(const Config &initial)
Create appropriate multithreaded configuration filing unconfigured values from initial configuration ...
InferenceEngine::IStreamsExecutor::Ptr
std::shared_ptr< IStreamsExecutor > Ptr
Definition: ie_istreams_executor.hpp:36
InferenceEngine::IStreamsExecutor::~IStreamsExecutor
~IStreamsExecutor() override
A virtual destructor.
InferenceEngine::ITaskExecutor
Interface for Task Executor. Inference Engine uses InferenceEngine::ITaskExecutor interface to run al...
Definition: ie_itask_executor.hpp:46
InferenceEngine::IStreamsExecutor::Config::_name
std::string _name
Used by ITT to name executor threads.
Definition: ie_istreams_executor.hpp:79
InferenceEngine::Task
std::function< void()> Task
Inference Engine Task Executor can use any copyable callable without parameters and output as a task....
Definition: ie_itask_executor.hpp:25