Compute Library
 23.11
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...
 
void set_num_threads_with_affinity (unsigned int num_threads, BindFunc func) override
 Sets the number of threads the scheduler will use to run the kernels but also using a binding function to pin the threads to given logical cores. More...
 
unsigned int num_threads () const override
 Returns the number of threads that the SingleThreadScheduler has in its pool. More...
 
void schedule (ICPPKernel *kernel, const Hints &hints) override
 Runs the kernel in the same thread as the caller synchronously. More...
 
void schedule_op (ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors) override
 Runs the kernel in the same thread as the caller synchronously. 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 BindFunc = std::function< int(int, int)>
 Function to be used and map a given thread id to a logical core id. More...
 
using Workload = std::function< void(const ThreadInfo &)>
 Signature for the workloads to execute. More...
 
- Static Public Attributes inherited from IScheduler
static constexpr unsigned int split_dimensions_all = std::numeric_limits<unsigned>::max()
 When arm_compute::ISchedular::Hints::_split_dimension is initialized with this value then the schedular is free to break down the problem space over as many dimensions as it wishes. More...
 

Detailed Description

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

It has 2 scheduling modes: Linear or Fanout (please refer to the implementation for details) The mode is selected automatically based on the runtime environment. However it can be forced via an environment variable ARM_COMPUTE_CPP_SCHEDULER_MODE. e.g.: ARM_COMPUTE_CPP_SCHEDULER_MODE=linear # Force select the linear scheduling mode ARM_COMPUTE_CPP_SCHEDULER_MODE=fanout # Force select the fanout scheduling mode

Definition at line 42 of file CPPScheduler.h.

Constructor & Destructor Documentation

◆ CPPScheduler()

Constructor: create a pool of threads.

Definition at line 435 of file CPPScheduler.cpp.

435  : _impl(std::make_unique<Impl>(num_threads_hint()))
436 {
437 }

◆ ~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 future releases
Returns
The scheduler

Definition at line 429 of file CPPScheduler.cpp.

430 {
431  static CPPScheduler scheduler;
432  return scheduler;
433 }

◆ num_threads()

unsigned int num_threads ( ) const
overridevirtual

Returns the number of threads that the SingleThreadScheduler has in its pool.

Returns
Number of threads available in SingleThreadScheduler.

Implements IScheduler.

Definition at line 455 of file CPPScheduler.cpp.

456 {
457  return _impl->num_threads();
458 }

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

◆ schedule()

void schedule ( ICPPKernel kernel,
const Hints hints 
)
overridevirtual

Runs the kernel in the same thread as the caller synchronously.

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

Implements IScheduler.

Definition at line 552 of file CPPScheduler.cpp.

553 {
554  ITensorPack tensors;
555  schedule_common(kernel, hints, kernel->window(), tensors);
556 }

References IKernel::window().

Referenced by TEST_CASE().

◆ schedule_op()

void schedule_op ( ICPPKernel kernel,
const Hints hints,
const Window window,
ITensorPack tensors 
)
overridevirtual

Runs the kernel in the same thread as the caller synchronously.

Parameters
[in]kernelKernel to execute.
[in]hintsHints for the scheduler.
[in]windowWindow to use for kernel execution.
[in]tensorsVector containing the tensors to operate on.

Implements IScheduler.

Definition at line 547 of file CPPScheduler.cpp.

548 {
549  schedule_common(kernel, hints, window, tensors);
550 }

◆ 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 one thread per CPU core available on the system will be used, otherwise the number of threads specified.

Implements IScheduler.

Definition at line 441 of file CPPScheduler.cpp.

442 {
443  // No changes in the number of threads while current workloads are running
444  arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
445  _impl->set_num_threads(num_threads, num_threads_hint());
446 }

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

Referenced by TEST_CASE().

◆ set_num_threads_with_affinity()

void set_num_threads_with_affinity ( unsigned int  num_threads,
BindFunc  func 
)
overridevirtual

Sets the number of threads the scheduler will use to run the kernels but also using a binding function to pin the threads to given logical cores.

Parameters
[in]num_threadsIf set to 0, then one thread per CPU core available on the system will be used, otherwise the number of threads specified.
[in]funcBinding function to use.

Reimplemented from IScheduler.

Definition at line 448 of file CPPScheduler.cpp.

449 {
450  // No changes in the number of threads while current workloads are running
451  arm_compute::lock_guard<std::mutex> lock(_impl->_run_workloads_mutex);
452  _impl->set_num_threads_with_affinity(num_threads, num_threads_hint(), func);
453 }

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


The documentation for this class was generated from the following files:
arm_compute::lock_guard
std::lock_guard< Mutex > lock_guard
Wrapper of lock_guard data-object.
Definition: Mutex.h:37
arm_compute::CPPScheduler::num_threads
unsigned int num_threads() const override
Returns the number of threads that the SingleThreadScheduler has in its pool.
Definition: CPPScheduler.cpp:455
arm_compute::IScheduler::num_threads_hint
unsigned int num_threads_hint() const
Get a hint for the best possible number of execution threads.
Definition: IScheduler.cpp:53
arm_compute::CPPScheduler::CPPScheduler
CPPScheduler()
Constructor: create a pool of threads.
Definition: CPPScheduler.cpp:435