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 #include "support/MemorySupport.h"
28
29 #if ARM_COMPUTE_CPP_SCHEDULER
30 #include "arm_compute/runtime/CPP/CPPScheduler.h"
31 #endif /* ARM_COMPUTE_CPP_SCHEDULER */
32
33 #include "arm_compute/runtime/SingleThreadScheduler.h"
34
35 #if ARM_COMPUTE_OPENMP_SCHEDULER
36 #include "arm_compute/runtime/OMP/OMPScheduler.h"
37 #endif /* ARM_COMPUTE_OPENMP_SCHEDULER */
38
39 using namespace arm_compute;
40
41 #if !ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
42 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::OMP;
43 #elif ARM_COMPUTE_CPP_SCHEDULER && !ARM_COMPUTE_OPENMP_SCHEDULER
44 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
45 #elif ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
46 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
47 #else /* ARM_COMPUTE_*_SCHEDULER */
48 Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::ST;
49 #endif /* ARM_COMPUTE_*_SCHEDULER */
50
51 std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr;
52
53 namespace
54 {
init()55 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init()
56 {
57 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m;
58 m[Scheduler::Type::ST] = support::cpp14::make_unique<SingleThreadScheduler>();
59 #if defined(ARM_COMPUTE_CPP_SCHEDULER)
60 m[Scheduler::Type::CPP] = support::cpp14::make_unique<CPPScheduler>();
61 #endif // defined(ARM_COMPUTE_CPP_SCHEDULER)
62 #if defined(ARM_COMPUTE_OPENMP_SCHEDULER)
63 m[Scheduler::Type::OMP] = support::cpp14::make_unique<OMPScheduler>();
64 #endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER)
65
66 return m;
67 }
68 } // namespace
69
70 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{};
71
set(Type t)72 void Scheduler::set(Type t)
73 {
74 ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t));
75 _scheduler_type = t;
76 }
77
is_available(Type t)78 bool Scheduler::is_available(Type t)
79 {
80 if(t == Type::CUSTOM)
81 {
82 return _custom_scheduler != nullptr;
83 }
84 else
85 {
86 return _schedulers.find(t) != _schedulers.end();
87 }
88 }
89
get_type()90 Scheduler::Type Scheduler::get_type()
91 {
92 return _scheduler_type;
93 }
94
get()95 IScheduler &Scheduler::get()
96 {
97 if(_scheduler_type == Type::CUSTOM)
98 {
99 if(_custom_scheduler == nullptr)
100 {
101 ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) before Scheduler::get()");
102 }
103 else
104 {
105 return *_custom_scheduler;
106 }
107 }
108 else
109 {
110 if(_schedulers.empty())
111 {
112 _schedulers = init();
113 }
114
115 auto it = _schedulers.find(_scheduler_type);
116 if(it != _schedulers.end())
117 {
118 return *it->second;
119 }
120 else
121 {
122 ARM_COMPUTE_ERROR("Invalid Scheduler type");
123 }
124 }
125 }
126
set(std::shared_ptr<IScheduler> scheduler)127 void Scheduler::set(std::shared_ptr<IScheduler> scheduler)
128 {
129 _custom_scheduler = std::move(scheduler);
130 set(Type::CUSTOM);
131 }
132