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 */
24 #include "arm_compute/runtime/Scheduler.h"
25
26 #include "arm_compute/core/Error.h"
27
28 #if ARM_COMPUTE_CPP_SCHEDULER
29 #include "arm_compute/runtime/CPP/CPPScheduler.h"
30 #endif /* ARM_COMPUTE_CPP_SCHEDULER */
31
32 #include "arm_compute/runtime/SingleThreadScheduler.h"
33
34 #if ARM_COMPUTE_OPENMP_SCHEDULER
35 #include "arm_compute/runtime/OMP/OMPScheduler.h"
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 {
init()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
set(Type t)71 void Scheduler::set(Type t)
72 {
73 ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t));
74 _scheduler_type = t;
75 }
76
is_available(Type t)77 bool Scheduler::is_available(Type t)
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
get_type()89 Scheduler::Type Scheduler::get_type()
90 {
91 return _scheduler_type;
92 }
93
get()94 IScheduler &Scheduler::get()
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
set(std::shared_ptr<IScheduler> scheduler)126 void Scheduler::set(std::shared_ptr<IScheduler> scheduler)
127 {
128 _custom_scheduler = std::move(scheduler);
129 set(Type::CUSTOM);
130 }
131