Compute Library
 19.08
OMPScheduler Class Reference

Pool of threads to automatically split a kernel's execution among several threads. More...

#include <OMPScheduler.h>

Collaboration diagram for OMPScheduler:
[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 OMPScheduler has in its 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 OMPSchedulerget ()
 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

Pool of threads to automatically split a kernel's execution among several threads.

Definition at line 32 of file OMPScheduler.h.

Member Function Documentation

◆ get()

OMPScheduler & get ( )
static

Access the scheduler singleton.

Returns
The scheduler

Definition at line 36 of file OMPScheduler.cpp.

37 {
38  static OMPScheduler scheduler;
39  return scheduler;
40 }
Pool of threads to automatically split a kernel's execution among several threads.
Definition: OMPScheduler.h:32

Referenced by Scheduler::get().

◆ num_threads()

unsigned int num_threads ( ) const
overridevirtual

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

Returns
Number of threads available in OMPScheduler.

Implements IScheduler.

Definition at line 47 of file OMPScheduler.cpp.

48 {
49  return _num_threads;
50 }

Referenced by OMPScheduler::schedule(), and OMPScheduler::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 58 of file OMPScheduler.cpp.

59 {
60  ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
62  "Dynamic scheduling is not supported in OMPScheduler");
63 
64  const Window &max_window = kernel->window();
65  const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
66  const unsigned int num_threads = std::min(num_iterations, _num_threads);
67 
68  if(!kernel->is_parallelisable() || num_threads == 1)
69  {
71  info.cpu_info = &_cpu_info;
72  kernel->run(max_window, info);
73  }
74  else
75  {
76  const unsigned int num_windows = num_threads;
77  std::vector<IScheduler::Workload> workloads(num_windows);
78  for(unsigned int t = 0; t < num_windows; t++)
79  {
80  //Capture 't' by copy, all the other variables by reference:
81  workloads[t] = [t, &hints, &max_window, &num_windows, &kernel](const ThreadInfo & info)
82  {
83  Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
84  win.validate();
85  kernel->run(win, info);
86  };
87  }
88  run_workloads(workloads);
89  }
90 }
virtual void run(const Window &window, const ThreadInfo &info)=0
Execute the kernel on the passed window.
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
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:175
void validate() const
Will validate all the window's dimensions' values when asserts are enabled.
Definition: Window.inl:159
constexpr size_t num_iterations(size_t dimension) const
Return the number of iterations needed to iterate through a given dimension.
Definition: Window.inl:168
Split the workload dynamically using a bucket system.
virtual bool is_parallelisable() const
Indicates whether or not the kernel is parallelisable.
Definition: IKernel.cpp:41
Information about executing thread and CPU.
Definition: CPPTypes.h:225
unsigned int num_threads() const override
Returns the number of threads that the OMPScheduler has in its pool.
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON_MSG, IScheduler::DYNAMIC, arm_compute::test::validation::info, IKernel::is_parallelisable(), Window::num_iterations(), OMPScheduler::num_threads(), ICPPKernel::run(), IScheduler::Hints::split_dimension(), Window::split_window(), 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 number returned by omp_get_max_threads() will be used, otherwise the number of threads specified.

Implements IScheduler.

Definition at line 52 of file OMPScheduler.cpp.

53 {
54  const unsigned int num_cores = omp_get_max_threads();
55  _num_threads = (num_threads == 0) ? num_cores : num_threads;
56 }
unsigned int num_threads() const override
Returns the number of threads that the OMPScheduler has in its pool.

References OMPScheduler::num_threads().


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