Compute Library
 21.02
IScheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
27 #include "arm_compute/core/Error.h"
29 #include "src/runtime/CPUUtils.h"
31 
32 namespace arm_compute
33 {
35  : _cpu_info()
36 {
38  // Work out the best possible number of execution threads
39  _num_threads_hint = utils::cpu::get_threads_hint();
40 }
41 
43 {
44  return _cpu_info;
45 }
46 
48 {
49  ARM_COMPUTE_UNUSED(num_threads, func);
50  ARM_COMPUTE_ERROR("Feature for affinity setting is not implemented");
51 }
52 
53 unsigned int IScheduler::num_threads_hint() const
54 {
55  return _num_threads_hint;
56 }
57 
58 void IScheduler::schedule_common(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors)
59 {
60  ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
61 #ifndef BARE_METAL
62  const Window &max_window = window;
64  {
65  /*
66  * if the split dim is size_t max then this signals we should parallelise over
67  * all dimensions
68  */
69  const std::size_t m = max_window.num_iterations(Window::DimX);
70  const std::size_t n = max_window.num_iterations(Window::DimY);
71 
72  //in c++17 this can be swapped for auto [ m_threads, n_threads ] = split_2d(...
73  unsigned m_threads, n_threads;
74  std::tie(m_threads, n_threads) = scheduler_utils::split_2d(this->num_threads(), m, n);
75 
76  std::vector<IScheduler::Workload> workloads;
77  for(unsigned int ni = 0; ni != n_threads; ++ni)
78  {
79  for(unsigned int mi = 0; mi != m_threads; ++mi)
80  {
81  workloads.push_back(
82  [ni, mi, m_threads, n_threads, &max_window, &kernel](const ThreadInfo & info)
83  {
84  //narrow the window to our mi-ni workload
85  Window win = max_window.split_window(Window::DimX, mi, m_threads)
86  .split_window(Window::DimY, ni, n_threads);
87 
88  win.validate();
89 
90  Window thread_locator;
91  thread_locator.set(Window::DimX, Window::Dimension(mi, m_threads));
92  thread_locator.set(Window::DimY, Window::Dimension(ni, n_threads));
93 
94  thread_locator.validate();
95 
96  kernel->run_nd(win, info, thread_locator);
97  });
98  }
99  }
100  run_workloads(workloads);
101  }
102  else
103  {
104  const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
105  const unsigned int num_threads = std::min(num_iterations, this->num_threads());
106 
107  if(num_iterations == 0)
108  {
109  return;
110  }
111 
112  if(!kernel->is_parallelisable() || num_threads == 1)
113  {
115  info.cpu_info = &_cpu_info;
116  if(tensors.empty())
117  {
118  kernel->run(max_window, info);
119  }
120  else
121  {
122  kernel->run_op(tensors, max_window, info);
123  }
124  }
125  else
126  {
127  unsigned int num_windows = 0;
128  switch(hints.strategy())
129  {
131  num_windows = num_threads;
132  break;
134  {
135  const unsigned int granule_threshold = (hints.threshold() <= 0) ? num_threads : static_cast<unsigned int>(hints.threshold());
136  // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder
137  num_windows = num_iterations > granule_threshold ? granule_threshold : num_iterations;
138  break;
139  }
140  default:
141  ARM_COMPUTE_ERROR("Unknown strategy");
142  }
143  std::vector<IScheduler::Workload> workloads(num_windows);
144  for(unsigned int t = 0; t < num_windows; ++t)
145  {
146  //Capture 't' by copy, all the other variables by reference:
147  workloads[t] = [t, &hints, &max_window, &num_windows, &kernel, &tensors](const ThreadInfo & info)
148  {
149  Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
150  win.validate();
151 
152  if(tensors.empty())
153  {
154  kernel->run(win, info);
155  }
156  else
157  {
158  kernel->run_op(tensors, win, info);
159  }
160  };
161  }
162  run_workloads(workloads);
163  }
164  }
165 #else /* !BARE_METAL */
166  ARM_COMPUTE_UNUSED(kernel, hints, window, tensors);
167 #endif /* !BARE_METAL */
168 }
169 
170 void IScheduler::run_tagged_workloads(std::vector<Workload> &workloads, const char *tag)
171 {
172  ARM_COMPUTE_UNUSED(tag);
173  run_workloads(workloads);
174 }
175 
176 } // namespace arm_compute
std::pair< unsigned, unsigned > split_2d(unsigned max_threads, std::size_t m, std::size_t n)
Given two dimensions and a maximum number of threads to utilise, calculate the best combination of th...
IScheduler()
Default constructor.
Definition: IScheduler.cpp:34
bool empty() const
Checks if pack is empty.
Definition: ITensorPack.cpp:61
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Common interface for all kernels implemented in C++.
Definition: ICPPKernel.h:38
Split the workload evenly among the threads.
static constexpr unsigned int split_dimensions_all
When arm_compute::ISchedular::Hints::_split_dimension is initialized with this value then the schedul...
Definition: IScheduler.h:62
Window split_window(size_t dimension, size_t id, size_t total) const
Split a window into a set of sub windows along a given dimension.
Definition: Window.inl:189
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
virtual void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Execute the kernel on the passed window.
Definition: ICPPKernel.h:86
CPUInfo & cpu_info()
Get CPU info.
Definition: IScheduler.cpp:42
virtual void run_tagged_workloads(std::vector< Workload > &workloads, const char *tag)
Execute all the passed workloads.
Definition: IScheduler.cpp:170
void validate() const
Will validate all the window&#39;s dimensions&#39; values when asserts are enabled.
Definition: Window.inl:173
constexpr size_t num_iterations(size_t dimension) const
Return the number of iterations needed to iterate through a given dimension.
Definition: Window.inl:182
int threshold() const
Return the granule capping threshold to be used by dynamic scheduling.
Definition: IScheduler.h:124
Copyright (c) 2017-2021 Arm Limited.
Split the workload dynamically using a bucket system.
std::function< int(int, int)> BindFunc
Function to be used and map a given thread id to a logical core id.
Definition: IScheduler.h:56
virtual void run_nd(const Window &window, const ThreadInfo &info, const Window &thread_locator)
legacy compatibility layer for implemantions which do not support thread_locator In these cases we si...
Definition: ICPPKernel.h:68
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual void run(const Window &window, const ThreadInfo &info)
Execute the kernel on the passed window.
Definition: ICPPKernel.h:55
StrategyHint strategy() const
Return the prefered strategy to use to split workload.
Definition: IScheduler.h:116
virtual bool is_parallelisable() const
Indicates whether or not the kernel is parallelisable.
Definition: IKernel.cpp:41
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
FloorUKernelPtr func
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
unsigned int num_threads_hint() const
Get a hint for the best possible number of execution threads.
Definition: IScheduler.cpp:53
Tensor packing service.
Definition: ITensorPack.h:37
unsigned int split_dimension() const
Return the prefered split dimension.
Definition: IScheduler.h:96
void get_cpu_configuration(CPUInfo &cpuinfo)
This function will try to detect the CPU configuration on the system and will fill the cpuinfo object...
Definition: CPUUtils.cpp:359
unsigned int get_threads_hint()
Some systems have both big and small cores, this fuction computes the minimum number of cores that ar...
Definition: CPUUtils.cpp:414
virtual unsigned int num_threads() const =0
Returns the number of threads that the SingleThreadScheduler has in his pool.
virtual void set_num_threads_with_affinity(unsigned int num_threads, BindFunc func)
Sets the number of threads the scheduler will use to run the kernels but also using a binding functio...
Definition: IScheduler.cpp:47
const CPUInfo * cpu_info
Definition: CPPTypes.h:239
Describe a multidimensional execution window.
Definition: Window.h:39