Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
David Svantesson | ded5b18 | 2023-08-02 14:23:00 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2017-2020, 2023 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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" |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 27 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #if ARM_COMPUTE_CPP_SCHEDULER |
| 29 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 30 | #endif /* ARM_COMPUTE_CPP_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
| 32 | #include "arm_compute/runtime/SingleThreadScheduler.h" |
| 33 | |
| 34 | #if ARM_COMPUTE_OPENMP_SCHEDULER |
| 35 | #include "arm_compute/runtime/OMP/OMPScheduler.h" |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 36 | #endif /* ARM_COMPUTE_OPENMP_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 |
David Svantesson | ded5b18 | 2023-08-02 14:23:00 +0000 | [diff] [blame^] | 43 | Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | #elif ARM_COMPUTE_CPP_SCHEDULER && ARM_COMPUTE_OPENMP_SCHEDULER |
| 45 | Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::CPP; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 46 | #else /* ARM_COMPUTE_*_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::ST; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 48 | #endif /* ARM_COMPUTE_*_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
David Svantesson | ded5b18 | 2023-08-02 14:23:00 +0000 | [diff] [blame^] | 50 | #ifndef ARM_COMPUTE_THREAD_LOCAL_SCHEDULER |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 51 | std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr; |
David Svantesson | ded5b18 | 2023-08-02 14:23:00 +0000 | [diff] [blame^] | 52 | #else // ARM_COMPUTE_THREAD_LOCAL_SCHEDULER |
| 53 | std::shared_ptr<IScheduler> thread_local Scheduler::_custom_scheduler = nullptr; |
| 54 | #endif // ARM_COMPUTE_THREAD_LOCAL_SCHEDULER |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 55 | |
| 56 | namespace |
| 57 | { |
| 58 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init() |
| 59 | { |
| 60 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m; |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 61 | m[Scheduler::Type::ST] = std::make_unique<SingleThreadScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 62 | #if defined(ARM_COMPUTE_CPP_SCHEDULER) |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 63 | m[Scheduler::Type::CPP] = std::make_unique<CPPScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 64 | #endif // defined(ARM_COMPUTE_CPP_SCHEDULER) |
| 65 | #if defined(ARM_COMPUTE_OPENMP_SCHEDULER) |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 66 | m[Scheduler::Type::OMP] = std::make_unique<OMPScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 67 | #endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER) |
| 68 | |
| 69 | return m; |
| 70 | } |
| 71 | } // namespace |
| 72 | |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame] | 73 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{}; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 74 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | void Scheduler::set(Type t) |
| 76 | { |
| 77 | ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t)); |
| 78 | _scheduler_type = t; |
| 79 | } |
| 80 | |
David Svantesson | ded5b18 | 2023-08-02 14:23:00 +0000 | [diff] [blame^] | 81 | bool 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 | |
| 93 | unsigned 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | bool Scheduler::is_available(Type t) |
| 106 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | if (t == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 109 | return _custom_scheduler != nullptr; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | return _schedulers.find(t) != _schedulers.end(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | Scheduler::Type Scheduler::get_type() |
| 118 | { |
| 119 | return _scheduler_type; |
| 120 | } |
| 121 | |
| 122 | IScheduler &Scheduler::get() |
| 123 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 124 | if (_scheduler_type == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (_custom_scheduler == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 128 | ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) " |
| 129 | "before Scheduler::get()"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 131 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 133 | return *_custom_scheduler; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | } |
| 135 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 136 | else |
| 137 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | if (_schedulers.empty()) |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame] | 139 | { |
| 140 | _schedulers = init(); |
| 141 | } |
| 142 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 143 | auto it = _schedulers.find(_scheduler_type); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 144 | if (it != _schedulers.end()) |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 145 | { |
| 146 | return *it->second; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | ARM_COMPUTE_ERROR("Invalid Scheduler type"); |
| 151 | } |
| 152 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 155 | void Scheduler::set(std::shared_ptr<IScheduler> scheduler) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 156 | { |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 157 | _custom_scheduler = std::move(scheduler); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 158 | set(Type::CUSTOM); |
| 159 | } |