blob: 6d961f29a50d3caf9829b682ab7c3ce464176209 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
David Svantessonded5b182023-08-02 14:23:00 +00002* Copyright (c) 2017-2020, 2023 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
David Svantessonded5b182023-08-02 14:23:00 +000043Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044#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
David Svantessonded5b182023-08-02 14:23:00 +000050#ifndef ARM_COMPUTE_THREAD_LOCAL_SCHEDULER
Georgios Pinitas12833d02019-07-25 13:31:10 +010051std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr;
David Svantessonded5b182023-08-02 14:23:00 +000052#else // ARM_COMPUTE_THREAD_LOCAL_SCHEDULER
53std::shared_ptr<IScheduler> thread_local Scheduler::_custom_scheduler = nullptr;
54#endif // ARM_COMPUTE_THREAD_LOCAL_SCHEDULER
Georgios Pinitas12833d02019-07-25 13:31:10 +010055
56namespace
57{
58std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init()
59{
60 std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m;
Georgios Pinitas40f51a62020-11-21 03:04:18 +000061 m[Scheduler::Type::ST] = std::make_unique<SingleThreadScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010062#if defined(ARM_COMPUTE_CPP_SCHEDULER)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000063 m[Scheduler::Type::CPP] = std::make_unique<CPPScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010064#endif // defined(ARM_COMPUTE_CPP_SCHEDULER)
65#if defined(ARM_COMPUTE_OPENMP_SCHEDULER)
Georgios Pinitas40f51a62020-11-21 03:04:18 +000066 m[Scheduler::Type::OMP] = std::make_unique<OMPScheduler>();
Georgios Pinitas12833d02019-07-25 13:31:10 +010067#endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER)
68
69 return m;
70}
71} // namespace
72
Sang-Hoon Parkde2e747d2020-09-24 12:55:29 +010073std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{};
Georgios Pinitas12833d02019-07-25 13:31:10 +010074
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075void Scheduler::set(Type t)
76{
77 ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t));
78 _scheduler_type = t;
79}
80
David Svantessonded5b182023-08-02 14:23:00 +000081bool Scheduler::is_set()
82{
83 if (_scheduler_type == Type::CUSTOM)
84 {
85 return _custom_scheduler != nullptr;
86 }
87 else
88 {
89 return !_schedulers.empty();
90 }
91}
92
93unsigned int Scheduler::num_threads()
94{
95 if (Scheduler::is_set())
96 {
97 return Scheduler::get().num_threads();
98 }
99 else
100 {
101 return CPUInfo::get().get_cpu_num();
102 }
103}
104
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105bool Scheduler::is_available(Type t)
106{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (t == Type::CUSTOM)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 {
Georgios Pinitas12833d02019-07-25 13:31:10 +0100109 return _custom_scheduler != nullptr;
110 }
111 else
112 {
113 return _schedulers.find(t) != _schedulers.end();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 }
115}
116
117Scheduler::Type Scheduler::get_type()
118{
119 return _scheduler_type;
120}
121
122IScheduler &Scheduler::get()
123{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 if (_scheduler_type == Type::CUSTOM)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 if (_custom_scheduler == nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100128 ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) "
129 "before Scheduler::get()");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 }
Georgios Pinitas12833d02019-07-25 13:31:10 +0100131 else
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 {
Georgios Pinitas12833d02019-07-25 13:31:10 +0100133 return *_custom_scheduler;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100134 }
135 }
Georgios Pinitas12833d02019-07-25 13:31:10 +0100136 else
137 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100138 if (_schedulers.empty())
Sang-Hoon Parkde2e747d2020-09-24 12:55:29 +0100139 {
140 _schedulers = init();
141 }
142
Georgios Pinitas12833d02019-07-25 13:31:10 +0100143 auto it = _schedulers.find(_scheduler_type);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 if (it != _schedulers.end())
Georgios Pinitas12833d02019-07-25 13:31:10 +0100145 {
146 return *it->second;
147 }
148 else
149 {
150 ARM_COMPUTE_ERROR("Invalid Scheduler type");
151 }
152 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153}
154
Anthony Barbiere8a49832018-01-18 10:04:05 +0000155void Scheduler::set(std::shared_ptr<IScheduler> scheduler)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000157 _custom_scheduler = std::move(scheduler);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 set(Type::CUSTOM);
159}