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" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 27 | #include "support/MemorySupport.h" |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 28 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #if ARM_COMPUTE_CPP_SCHEDULER |
| 30 | #include "arm_compute/runtime/CPP/CPPScheduler.h" |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 31 | #endif /* ARM_COMPUTE_CPP_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | #include "arm_compute/runtime/SingleThreadScheduler.h" |
| 34 | |
| 35 | #if ARM_COMPUTE_OPENMP_SCHEDULER |
| 36 | #include "arm_compute/runtime/OMP/OMPScheduler.h" |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 37 | #endif /* ARM_COMPUTE_OPENMP_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 47 | #else /* ARM_COMPUTE_*_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | Scheduler::Type Scheduler::_scheduler_type = Scheduler::Type::ST; |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 49 | #endif /* ARM_COMPUTE_*_SCHEDULER */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 51 | std::shared_ptr<IScheduler> Scheduler::_custom_scheduler = nullptr; |
| 52 | |
| 53 | namespace |
| 54 | { |
| 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 | |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame^] | 70 | std::map<Scheduler::Type, std::unique_ptr<IScheduler>> Scheduler::_schedulers{}; |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 71 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | void Scheduler::set(Type t) |
| 73 | { |
| 74 | ARM_COMPUTE_ERROR_ON(!Scheduler::is_available(t)); |
| 75 | _scheduler_type = t; |
| 76 | } |
| 77 | |
| 78 | bool Scheduler::is_available(Type t) |
| 79 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 80 | if(t == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 81 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 82 | return _custom_scheduler != nullptr; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | return _schedulers.find(t) != _schedulers.end(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | Scheduler::Type Scheduler::get_type() |
| 91 | { |
| 92 | return _scheduler_type; |
| 93 | } |
| 94 | |
| 95 | IScheduler &Scheduler::get() |
| 96 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 97 | if(_scheduler_type == Type::CUSTOM) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 99 | if(_custom_scheduler == nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 101 | 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] | 102 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 103 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | { |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 105 | return *_custom_scheduler; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | } |
| 107 | } |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 108 | else |
| 109 | { |
Sang-Hoon Park | de2e747d | 2020-09-24 12:55:29 +0100 | [diff] [blame^] | 110 | if(_schedulers.empty()) |
| 111 | { |
| 112 | _schedulers = init(); |
| 113 | } |
| 114 | |
Georgios Pinitas | 12833d0 | 2019-07-25 13:31:10 +0100 | [diff] [blame] | 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 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 127 | void Scheduler::set(std::shared_ptr<IScheduler> scheduler) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 128 | { |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 129 | _custom_scheduler = std::move(scheduler); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 130 | set(Type::CUSTOM); |
| 131 | } |