Compute Library
 21.02
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...
 
void schedule_op (ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors) 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 set_num_threads_with_affinity (unsigned int num_threads, BindFunc func)
 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...
 
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 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

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

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

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

Referenced by OMPScheduler::schedule_op().

41 {
42  return _num_threads;
43 }

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

References IKernel::window().

52 {
53  ITensorPack tensors;
54  schedule_common(kernel, hints, kernel->window(), tensors);
55 }

◆ schedule_op()

void schedule_op ( ICPPKernel kernel,
const Hints hints,
const Window window,
ITensorPack tensors 
)
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.
[in]windowWindow to use for kernel execution.
[in]tensorsVector containing the tensors to operate on.

Implements IScheduler.

Definition at line 57 of file OMPScheduler.cpp.

References ARM_COMPUTE_ERROR_ON_MSG, ThreadInfo::cpu_info, IScheduler::DYNAMIC, arm_compute::test::validation::info, IKernel::is_parallelisable(), Window::num_iterations(), OMPScheduler::num_threads(), ThreadInfo::num_threads, ICPPKernel::run_op(), IScheduler::Hints::split_dimension(), Window::split_window(), IScheduler::Hints::strategy(), tf_frozen_model_extractor::t, ThreadInfo::thread_id, and Window::validate().

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

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

46 {
47  const unsigned int num_cores = omp_get_max_threads();
48  _num_threads = (num_threads == 0) ? num_cores : num_threads;
49 }
unsigned int num_threads() const override
Returns the number of threads that the OMPScheduler has in its pool.

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