Compute Library
 19.08
CPPScheduler Class Reference

C++11 implementation of a pool of threads to automatically split a kernel's execution among several threads. More...

#include <CPPScheduler.h>

Collaboration diagram for CPPScheduler:
[legend]

Public Member Functions

void set_num_threads (unsigned int num_threads) override
 Sets the number of threads the scheduler will use to run the kernels. More...
 
unsigned int num_threads () const override
 Returns the number of threads that the CPPScheduler has in his pool. More...
 
void schedule (ICPPKernel *kernel, const Hints &hints) override
 Multithread the execution of the passed kernel if possible. More...
 
- Public Member Functions inherited from IScheduler
 IScheduler ()
 Default constructor. More...
 
virtual ~IScheduler ()=default
 Destructor. More...
 
virtual void run_tagged_workloads (std::vector< Workload > &workloads, const char *tag)
 Execute all the passed workloads. More...
 
CPUInfocpu_info ()
 Get CPU info. More...
 
unsigned int num_threads_hint () const
 Get a hint for the best possible number of execution threads. More...
 

Static Public Member Functions

static CPPSchedulerget ()
 Access the scheduler singleton. More...
 

Additional Inherited Members

- Public Types inherited from IScheduler
enum  StrategyHint { STATIC, DYNAMIC }
 Strategies available to split a workload. More...
 
using Workload = std::function< void(const ThreadInfo &)>
 Signature for the workloads to execute. More...
 

Detailed Description

C++11 implementation of a pool of threads to automatically split a kernel's execution among several threads.

Definition at line 34 of file CPPScheduler.h.

Member Function Documentation

◆ get()

CPPScheduler & get ( )
static

Access the scheduler singleton.

Returns
The scheduler

Definition at line 212 of file CPPScheduler.cpp.

213 {
214  static CPPScheduler scheduler;
215  return scheduler;
216 }

Referenced by Scheduler::get().

◆ num_threads()

unsigned int num_threads ( ) const
overridevirtual

Returns the number of threads that the CPPScheduler has in his pool.

Returns
Number of threads available in CPPScheduler.

Implements IScheduler.

Definition at line 230 of file CPPScheduler.cpp.

231 {
232  return _num_threads;
233 }

Referenced by CPPScheduler::schedule(), and CPPScheduler::set_num_threads().

◆ schedule()

void schedule ( ICPPKernel kernel,
const Hints hints 
)
overridevirtual

Multithread the execution of the passed kernel if possible.

The kernel will run on a single thread if any of these conditions is true:

Parameters
[in]kernelKernel to execute.
[in]hintsHints for the scheduler.

Implements IScheduler.

Definition at line 275 of file CPPScheduler.cpp.

276 {
277  ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
278 
279  const Window &max_window = kernel->window();
280  const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
281  const unsigned int num_threads = std::min(num_iterations, _num_threads);
282 
283  if(num_iterations == 0)
284  {
285  return;
286  }
287 
288  if(!kernel->is_parallelisable() || num_threads == 1)
289  {
290  ThreadInfo info;
291  info.cpu_info = &_cpu_info;
292  kernel->run(max_window, info);
293  }
294  else
295  {
296  unsigned int num_windows = 0;
297  switch(hints.strategy())
298  {
300  num_windows = num_threads;
301  break;
303  {
304  // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder
305  const unsigned int max_iterations = static_cast<unsigned int>(_num_threads) * 3;
306  num_windows = num_iterations > max_iterations ? max_iterations : num_iterations;
307  break;
308  }
309  default:
310  ARM_COMPUTE_ERROR("Unknown strategy");
311  }
312  std::vector<IScheduler::Workload> workloads(num_windows);
313  for(unsigned int t = 0; t < num_windows; t++)
314  {
315  //Capture 't' by copy, all the other variables by reference:
316  workloads[t] = [t, &hints, &max_window, &num_windows, &kernel](const ThreadInfo & info)
317  {
318  Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
319  win.validate();
320  kernel->run(win, info);
321  };
322  }
323  run_workloads(workloads);
324  }
325 }
#define ARM_COMPUTE_ERROR(...)
Print the given message then throw an std::runtime_error.
Definition: Error.h:261
unsigned int num_threads() const override
Returns the number of threads that the CPPScheduler has in his pool.
Split the workload evenly among the threads.
Split the workload dynamically using a bucket system.
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON_MSG, IScheduler::DYNAMIC, arm_compute::test::validation::info, IKernel::is_parallelisable(), Window::num_iterations(), CPPScheduler::num_threads(), ICPPKernel::run(), IScheduler::Hints::split_dimension(), Window::split_window(), IScheduler::STATIC, IScheduler::Hints::strategy(), Window::validate(), and IKernel::window().

◆ set_num_threads()

void set_num_threads ( unsigned int  num_threads)
overridevirtual

Sets the number of threads the scheduler will use to run the kernels.

Parameters
[in]num_threadsIf set to 0, then the maximum number of threads supported by C++11 will be used, otherwise the number of threads specified.

Implements IScheduler.

Definition at line 224 of file CPPScheduler.cpp.

225 {
226  _num_threads = num_threads == 0 ? num_threads_hint() : num_threads;
227  _threads.resize(_num_threads - 1);
228 }
unsigned int num_threads() const override
Returns the number of threads that the CPPScheduler has in his pool.
unsigned int num_threads_hint() const
Get a hint for the best possible number of execution threads.
Definition: IScheduler.cpp:44

References CPPScheduler::num_threads(), and IScheduler::num_threads_hint().


The documentation for this class was generated from the following files: