Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 |
| 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; |
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 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 50 | std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr; |
| 51 | |
| 52 | namespace |
| 53 | { |
| 54 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> init() |
| 55 | { |
| 56 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> m; |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 57 | m[Scheduler::Type::ST] = std::make_unique<SingleThreadScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 58 | #if defined(ARM_COMPUTE_CPP_SCHEDULER) |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 59 | m[Scheduler::Type::CPP] = std::make_unique<CPPScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 60 | #endif // defined(ARM_COMPUTE_CPP_SCHEDULER) |
| 61 | #if defined(ARM_COMPUTE_OPENMP_SCHEDULER) |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 62 | m[Scheduler::Type::OMP] = std::make_unique<OMPScheduler>(); |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 63 | #endif // defined(ARM_COMPUTE_OPENMP_SCHEDULER) |
| 64 | |
| 65 | return m; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame] | 69 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{}; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 70 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | void Scheduler::set(Type t) |
| 72 | { |
| 73 | ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t)); |
| 74 | _scheduler_type = t; |
| 75 | } |
| 76 | |
| 77 | bool Scheduler::is_available(Type t) |
| 78 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 79 | if(t == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 81 | return _custom_scheduler != nullptr; |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | return _schedulers.find(t) != _schedulers.end(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | Scheduler::Type Scheduler::get_type() |
| 90 | { |
| 91 | return _scheduler_type; |
| 92 | } |
| 93 | |
| 94 | IScheduler &Scheduler::get() |
| 95 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 96 | if(_scheduler_type == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 98 | if(_custom_scheduler == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_ERROR("No custom scheduler has been setup. Call set(std::shared_ptr<IScheduler> &scheduler) before Scheduler::get()"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 102 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 104 | return *_custom_scheduler; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 107 | else |
| 108 | { |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame] | 109 | if(_schedulers.empty()) |
| 110 | { |
| 111 | _schedulers = init(); |
| 112 | } |
| 113 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 126 | void Scheduler::set(std::shared_ptr<IScheduler> scheduler) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 127 | { |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 128 | _custom_scheduler = std::move(scheduler); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 129 | set(Type::CUSTOM); |
| 130 | } |