ie_istreams_executor.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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 inference thread binding type
40  */
41  enum ThreadBindingType : std::uint8_t {
42  NONE, //!< Don't bind the inference threads
43  CORES, //!< Bind inference threads to the CPU cores (round-robin)
44  // the following modes are implemented only for the TBB code-path:
45  NUMA, //!< Bind to the NUMA nodes (default mode for the non-hybrid CPUs on the Win/MacOS, where the 'CORES' is not implemeneted)
46  HYBRID_AWARE //!< Let the runtime bind the inference threads depending on the cores type (default mode for the hybrid CPUs)
47  };
48 
49  /**
50  * @brief Defines IStreamsExecutor configuration
51  */
52  struct INFERENCE_ENGINE_API_CLASS(Config) {
53  /**
54  * @brief Supported Configuration keys
55  * @return vector of supported configuration keys
56  */
57  std::vector<std::string> SupportedKeys();
58 
59  /**
60  * @brief Parses configuration key/value pair
61  * @param key configuration key
62  * @param value configuration values
63  */
64  void SetConfig(const std::string& key, const std::string& value);
65 
66  /**
67  * @brief Return configuration value
68  * @param key configuration key
69  * @return configuration value wrapped into Parameter
70  */
71  Parameter GetConfig(const std::string& key);
72 
73  /**
74  * @brief Create appropriate multithreaded configuration
75  * filing unconfigured values from initial configuration using hardware properties
76  * @param initial Inital configuration
77  * @param fp_intesive additional hint for the the (Hybrid) core-types selection logic
78  * whether the executor should be configured for floating point intensive work (as opposite to int8 intensive)
79  * @return configured values
80  */
81  static Config MakeDefaultMultiThreaded(const Config& initial, const bool fp_intesive = true);
82 
83  std::string _name; //!< Used by `ITT` to name executor threads
84  int _streams = 1; //!< Number of streams.
85  int _threadsPerStream = 0; //!< Number of threads per stream that executes `ie_parallel` calls
86  ThreadBindingType _threadBindingType = ThreadBindingType::NONE; //!< Thread binding to hardware resource type. No binding by default
87  int _threadBindingStep = 1; //!< In case of @ref CORES binding offset type thread binded to cores with defined step
88  int _threadBindingOffset = 0; //!< In case of @ref CORES binding offset type thread binded to cores starting from offset
89  int _threads = 0; //!< Number of threads distributed between streams. Reserved. Should not be used.
90  enum PreferredCoreType {
91  ANY,
92  LITTLE,
93  BIG,
94  ROUND_ROBIN // used w/multiple streams to populate the Big cores first, then the Little, then wrap around (for large #streams)
95  } _threadPreferredCoreType = PreferredCoreType::ANY; //!< In case of @ref HYBRID_AWARE hints the TBB to affinitize
96 
97  /**
98  * @brief A constructor with arguments
99  *
100  * @param[in] name The executor name
101  * @param[in] streams @copybrief Config::_streams
102  * @param[in] threadsPerStream @copybrief Config::_threadsPerStream
103  * @param[in] threadBindingType @copybrief Config::_threadBindingType
104  * @param[in] threadBindingStep @copybrief Config::_threadBindingStep
105  * @param[in] threadBindingOffset @copybrief Config::_threadBindingOffset
106  * @param[in] threads @copybrief Config::_threads
107  * @param[in] threadPreferBigCores @copybrief Config::_threadPreferBigCores
108  */
110  std::string name = "StreamsExecutor",
111  int streams = 1,
112  int threadsPerStream = 0,
113  ThreadBindingType threadBindingType = ThreadBindingType::NONE,
114  int threadBindingStep = 1,
115  int threadBindingOffset = 0,
116  int threads = 0,
117  PreferredCoreType threadPreferredCoreType = PreferredCoreType::ANY) :
118  _name{name},
119  _streams{streams},
120  _threadsPerStream{threadsPerStream},
121  _threadBindingType{threadBindingType},
122  _threadBindingStep{threadBindingStep},
123  _threadBindingOffset{threadBindingOffset},
124  _threads{threads}, _threadPreferredCoreType(threadPreferredCoreType){
125  }
126  };
127 
128  /**
129  * @brief A virtual destructor
130  */
131  ~IStreamsExecutor() override;
132 
133  /**
134  * @brief Return the index of current stream
135  * @return An index of current stream. Or throw exceptions if called not from stream thread
136  */
137  virtual int GetStreamId() = 0;
138 
139  /**
140  * @brief Return the id of current NUMA Node
141  * @return `ID` of current NUMA Node, or throws exceptions if called not from stream thread
142  */
143  virtual int GetNumaNodeId() = 0;
144 
145  /**
146  * @brief Execute the task in the current thread using streams executor configuration and constraints
147  * @param task A task to start
148  */
149  virtual void Execute(Task task) = 0;
150 };
151 
152 
153 
154 } // namespace InferenceEngine
virtual int GetNumaNodeId()=0
Return the id of current NUMA Node.
~IStreamsExecutor() override
A virtual destructor.
virtual void Execute(Task task)=0
Execute the task in the current thread using streams executor configuration and constraints.
virtual int GetStreamId()=0
Return the index of current stream.
ThreadBindingType
Defines inference thread binding type.
Definition: ie_istreams_executor.hpp:41
@ NUMA
Bind to the NUMA nodes (default mode for the non-hybrid CPUs on the Win/MacOS, where the 'CORES' is n...
Definition: ie_istreams_executor.hpp:45
@ NONE
Don't bind the inference threads.
Definition: ie_istreams_executor.hpp:42
@ CORES
Bind inference threads to the CPU cores (round-robin)
Definition: ie_istreams_executor.hpp:43
std::shared_ptr< IStreamsExecutor > Ptr
Definition: ie_istreams_executor.hpp:36
Interface for Task Executor. Inference Engine uses InferenceEngine::ITaskExecutor interface to run al...
Definition: ie_itask_executor.hpp:46
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
A header file for Inference Engine Task Executor Interface.
Inference Engine Plugin API namespace.
Defines IStreamsExecutor configuration.
Definition: ie_istreams_executor.hpp:52
Config(std::string name="StreamsExecutor", int streams=1, int threadsPerStream=0, ThreadBindingType threadBindingType=ThreadBindingType::NONE, int threadBindingStep=1, int threadBindingOffset=0, int threads=0, PreferredCoreType threadPreferredCoreType=PreferredCoreType::ANY)
A constructor with arguments.
Definition: ie_istreams_executor.hpp:109
void SetConfig(const std::string &key, const std::string &value)
Parses configuration key/value pair.
std::vector< std::string > SupportedKeys()
Supported Configuration keys.
static Config MakeDefaultMultiThreaded(const Config &initial, const bool fp_intesive=true)
Create appropriate multithreaded configuration filing unconfigured values from initial configuration ...
Parameter GetConfig(const std::string &key)
Return configuration value.
std::string _name
Used by ITT to name executor threads.
Definition: ie_istreams_executor.hpp:83