blob: e52fb59940b49a56cadee9edd6e26ce1d6cbd04f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
Georgios Pinitas12833d02019-07-25 13:31:10 +010027
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#if ARM_COMPUTE_CPP_SCHEDULER
29#include "arm_compute/runtime/CPP/CPPScheduler.h"
Anthony Barbierac69aa12017-07-03 17:39:37 +010030#endif /* ARM_COMPUTE_CPP_SCHEDULER */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
32#include "arm_compute/runtime/SingleThreadScheduler.h"
33
34#if ARM_COMPUTE_OPENMP_SCHEDULER
35#include "arm_compute/runtime/OMP/OMPScheduler.h"
Anthony Barbierac69aa12017-07-03 17:39:37 +010036#endif /* ARM_COMPUTE_OPENMP_SCHEDULER */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
38using namespace arm_compute;
39
40#if !ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
41Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::OMP;
42#elif ARM_COMPUTE_CPP_SCHEDULER && !ARM_COMPUTE_OPENMP_SCHEDULER
43Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
44#elif ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER
45Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
Anthony Barbierac69aa12017-07-03 17:39:37 +010046#else /* ARM_COMPUTE_*_SCHEDULER */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::ST;
Anthony Barbierac69aa12017-07-03 17:39:37 +010048#endif /* ARM_COMPUTE_*_SCHEDULER */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
Georgios Pinitas12833d02019-07-25 13:31:10 +010050std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr;
51
52namespace
53{
54std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init()
55{
56 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m;
Georgios Pinitas40f51a62020-11-21 03:04:18 +000057 m[Scheduler::Type::ST] = std::make_unique<SingleThreadScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010058#if defined(ARM_COMPUTE_CPP_SCHEDULER)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000059 m[Scheduler::Type::CPP] = std::make_unique<CPPScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010060#endif // defined(ARM_COMPUTE_CPP_SCHEDULER)
61#if defined(ARM_COMPUTE_OPENMP_SCHEDULER)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000062 m[Scheduler::Type::OMP] = std::make_unique<OMPScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010063#endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER)
64
65 return m;
66}
67} // namespace
68
Sang-Hoon Parkde2e747d2020-09-24 12:55:29 +010069std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{};
Georgios Pinitas12833d02019-07-25 13:31:10 +010070
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071void Scheduler::set(Type t)
72{
73 ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t));
74 _scheduler_type = t;
75}
76
77bool Scheduler::is_available(Type t)
78{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 if (t == Type::CUSTOM)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080 {
Georgios Pinitas12833d02019-07-25 13:31:10 +010081 return _custom_scheduler != nullptr;
82 }
83 else
84 {
85 return _schedulers.find(t) != _schedulers.end();
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 }
87}
88
89Scheduler::Type Scheduler::get_type()
90{
91 return _scheduler_type;
92}
93
94IScheduler &Scheduler::get()
95{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (_scheduler_type == Type::CUSTOM)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010098 if (_custom_scheduler == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) "
101 "before Scheduler::get()");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 }
Georgios Pinitas12833d02019-07-25 13:31:10 +0100103 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 {
Georgios Pinitas12833d02019-07-25 13:31:10 +0100105 return *_custom_scheduler;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 }
107 }
Georgios Pinitas12833d02019-07-25 13:31:10 +0100108 else
109 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 if (_schedulers.empty())
Sang-Hoon Parkde2e747d2020-09-24 12:55:29 +0100111 {
112 _schedulers = init();
113 }
114
Georgios Pinitas12833d02019-07-25 13:31:10 +0100115 auto it = _schedulers.find(_scheduler_type);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100116 if (it != _schedulers.end())
Georgios Pinitas12833d02019-07-25 13:31:10 +0100117 {
118 return *it->second;
119 }
120 else
121 {
122 ARM_COMPUTE_ERROR("Invalid Scheduler type");
123 }
124 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125}
126
Anthony Barbiere8a49832018-01-18 10:04:05 +0000127void Scheduler::set(std::shared_ptr<IScheduler> scheduler)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000129 _custom_scheduler = std::move(scheduler);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 set(Type::CUSTOM);
131}