Compute Library
 20.02.1
CPPScheduler Class Referencefinal

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

 CPPScheduler ()
 Constructor: create a pool of threads. More...
 
 ~CPPScheduler ()
 Default destructor. More...
 
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.

Constructor & Destructor Documentation

◆ CPPScheduler()

Constructor: create a pool of threads.

Definition at line 248 of file CPPScheduler.cpp.

249  : _impl(support::cpp14::make_unique<Impl>(num_threads_hint()))
250 {
251 }
unsigned int num_threads_hint() const
Get a hint for the best possible number of execution threads.
Definition: IScheduler.cpp:44

◆ ~CPPScheduler()

~CPPScheduler ( )
default

Default destructor.

Member Function Documentation

◆ get()

CPPScheduler & get ( )
static

Access the scheduler singleton.

Note
this method has been deprecated and will be remover in the upcoming releases
Returns
The scheduler

Definition at line 242 of file CPPScheduler.cpp.

243 {
244  static CPPScheduler scheduler;
245  return scheduler;
246 }
CPPScheduler()
Constructor: create a pool of threads.

◆ 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 262 of file CPPScheduler.cpp.

263 {
264  return _impl->num_threads();
265 }

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 312 of file CPPScheduler.cpp.

313 {
314  ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
315 
316  const Window &max_window = kernel->window();
317  const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
318  const unsigned int num_threads = std::min(num_iterations, _impl->_num_threads);
319 
320  if(num_iterations == 0)
321  {
322  return;
323  }
324 
325  if(!kernel->is_parallelisable() || num_threads == 1)
326  {
327  ThreadInfo info;
328  info.cpu_info = &_cpu_info;
329  kernel->run(max_window, info);
330  }
331  else
332  {
333  unsigned int num_windows = 0;
334  switch(hints.strategy())
335  {
337  num_windows = num_threads;
338  break;
340  {
341  const unsigned int granule_threshold = (hints.threshold() <= 0) ? num_threads : static_cast<unsigned int>(hints.threshold());
342  // Make sure we don't use some windows which are too small as this might create some contention on the ThreadFeeder
343  num_windows = num_iterations > granule_threshold ? granule_threshold : num_iterations;
344  break;
345  }
346  default:
347  ARM_COMPUTE_ERROR("Unknown strategy");
348  }
349  std::vector<IScheduler::Workload> workloads(num_windows);
350  for(unsigned int t = 0; t < num_windows; t++)
351  {
352  //Capture 't' by copy, all the other variables by reference:
353  workloads[t] = [t, &hints, &max_window, &num_windows, &kernel](const ThreadInfo & info)
354  {
355  Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
356  win.validate();
357  kernel->run(win, info);
358  };
359  }
360  run_workloads(workloads);
361  }
362 }
unsigned int num_threads() const override
Returns the number of threads that the CPPScheduler has in his pool.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Split the workload evenly among the threads.
Split the workload dynamically using a bucket system.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456

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(), IScheduler::Hints::threshold(), 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 255 of file CPPScheduler.cpp.

256 {
257  // No changes in the number of threads while current workloads are running
258  arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
259  _impl->set_num_threads(num_threads, num_threads_hint());
260 }
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
std::lock_guard< Mutex > lock_guard
Wrapper of lock_guard data-object.
Definition: Mutex.h:37

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


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