Compute Library
 19.11
OMPScheduler Class Referencefinal

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

 OMPScheduler ()
 Constructor. 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 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...
 

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.

Constructor & Destructor Documentation

◆ OMPScheduler()

Constructor.

Definition at line 36 of file OMPScheduler.cpp.

37  : _num_threads(omp_get_max_threads())
38 {
39 }

Member Function Documentation

◆ 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 41 of file OMPScheduler.cpp.

42 {
43  return _num_threads;
44 }

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 52 of file OMPScheduler.cpp.

53 {
54  ARM_COMPUTE_ERROR_ON_MSG(!kernel, "The child class didn't set the kernel");
56  "Dynamic scheduling is not supported in OMPScheduler");
57 
58  const Window &max_window = kernel->window();
59  const unsigned int num_iterations = max_window.num_iterations(hints.split_dimension());
60  const unsigned int num_threads = std::min(num_iterations, _num_threads);
61 
62  if(!kernel->is_parallelisable() || num_threads == 1)
63  {
64  ThreadInfo info;
65  info.cpu_info = &_cpu_info;
66  kernel->run(max_window, info);
67  }
68  else
69  {
70  const unsigned int num_windows = num_threads;
71  std::vector<IScheduler::Workload> workloads(num_windows);
72  for(unsigned int t = 0; t < num_windows; t++)
73  {
74  //Capture 't' by copy, all the other variables by reference:
75  workloads[t] = [t, &hints, &max_window, &num_windows, &kernel](const ThreadInfo & info)
76  {
77  Window win = max_window.split_window(hints.split_dimension(), t, num_windows);
78  win.validate();
79  kernel->run(win, info);
80  };
81  }
82  run_workloads(workloads);
83  }
84 }
Split the workload dynamically using a bucket system.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
unsigned int num_threads() const override
Returns the number of threads that the OMPScheduler has in its pool.

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 46 of file OMPScheduler.cpp.

47 {
48  const unsigned int num_cores = omp_get_max_threads();
49  _num_threads = (num_threads == 0) ? num_cores : num_threads;
50 }
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: