Compute Library
 21.05
Scheduler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
26 #include "arm_compute/core/Error.h"
27 
28 #if ARM_COMPUTE_CPP_SCHEDULER
30 #endif /* ARM_COMPUTE_CPP_SCHEDULER */
31 
33 
34 #if ARM_COMPUTE_OPENMP_SCHEDULER
36 #endif /* ARM_COMPUTE_OPENMP_SCHEDULER */
37 
38 using namespace arm_compute;
39 
40 #if !ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
41 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::OMP;
42 #elif ARM_COMPUTE_CPP_SCHEDULER && !ARM_COMPUTE_OPENMP_SCHEDULER
43 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
44 #elif ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
45 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
46 #else /* ARM_COMPUTE_*_SCHEDULER */
47 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::ST;
48 #endif /* ARM_COMPUTE_*_SCHEDULER */
49 
50 std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr;
51 
52 namespace
53 {
54 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init()
55 {
56  std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m;
57  m[Scheduler::Type::ST] = std::make_unique<SingleThreadScheduler>();
58 #if defined(ARM_COMPUTE_CPP_SCHEDULER)
59  m[Scheduler::Type::CPP] = std::make_unique<CPPScheduler>();
60 #endif // defined(ARM_COMPUTE_CPP_SCHEDULER)
61 #if defined(ARM_COMPUTE_OPENMP_SCHEDULER)
62  m[Scheduler::Type::OMP] = std::make_unique<OMPScheduler>();
63 #endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER)
64 
65  return m;
66 }
67 } // namespace
68 
69 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{};
70 
72 {
74  _scheduler_type = t;
75 }
76 
78 {
79  if(t == Type::CUSTOM)
80  {
81  return _custom_scheduler != nullptr;
82  }
83  else
84  {
85  return _schedulers.find(t) != _schedulers.end();
86  }
87 }
88 
90 {
91  return _scheduler_type;
92 }
93 
95 {
96  if(_scheduler_type == Type::CUSTOM)
97  {
98  if(_custom_scheduler == nullptr)
99  {
100  ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) before Scheduler::get()");
101  }
102  else
103  {
104  return *_custom_scheduler;
105  }
106  }
107  else
108  {
109  if(_schedulers.empty())
110  {
111  _schedulers = init();
112  }
113 
114  auto it = _schedulers.find(_scheduler_type);
115  if(it != _schedulers.end())
116  {
117  return *it->second;
118  }
119  else
120  {
121  ARM_COMPUTE_ERROR("Invalid Scheduler type");
122  }
123  }
124 }
125 
126 void Scheduler::set(std::shared_ptr<IScheduler> scheduler)
127 {
128  _custom_scheduler = std::move(scheduler);
129  set(Type::CUSTOM);
130 }
Type
Scheduler type.
Definition: Scheduler.h:39
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Scheduler interface to run kernels.
Definition: IScheduler.h:41
static void set(std::shared_ptr< IScheduler > scheduler)
Sets the user defined scheduler and makes it the active scheduler.
Definition: Scheduler.cpp:126
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
OpenMP scheduler (Alternative to the CPPScheduler).
Copyright (c) 2017-2021 Arm Limited.
static bool is_available(Type t)
Returns true if the given scheduler type is supported.
Definition: Scheduler.cpp:77
Basic pool of threads to execute CPP/Neon code on several cores in parallel.
static Type get_type()
Returns the type of the active scheduler.
Definition: Scheduler.cpp:89
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94