Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 ARM Limited. |
| 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 "SchedulerTimer.h" |
| 25 | |
| 26 | #include "WallClockTimer.h" |
| 27 | #include "arm_compute/core/CPP/ICPPKernel.h" |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/misc/Cast.h" |
| 29 | #include "arm_compute/graph/INode.h" |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace framework |
| 36 | { |
| 37 | std::string SchedulerTimer::id() const |
| 38 | { |
| 39 | return "SchedulerTimer"; |
| 40 | } |
| 41 | |
| 42 | class Interceptor final : public IScheduler |
| 43 | { |
| 44 | public: |
| 45 | /** Default constructor. */ |
| 46 | Interceptor(std::list<SchedulerTimer::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 47 | : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor), _prefix() |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | void set_num_threads(unsigned int num_threads) override |
| 52 | { |
| 53 | _real_scheduler.set_num_threads(num_threads); |
| 54 | } |
| 55 | |
| 56 | unsigned int num_threads() const override |
| 57 | { |
| 58 | return _real_scheduler.num_threads(); |
| 59 | } |
| 60 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 61 | void set_prefix(std::string prefix) |
| 62 | { |
| 63 | _prefix = std::move(prefix); |
| 64 | } |
| 65 | |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 66 | void schedule(ICPPKernel *kernel, const Hints &hints) override |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 67 | { |
| 68 | _timer.start(); |
Anthony Barbier | 376c85f | 2018-05-25 14:17:21 +0100 | [diff] [blame] | 69 | _real_scheduler.schedule(kernel, hints.split_dimension()); |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 70 | _timer.stop(); |
| 71 | |
| 72 | SchedulerTimer::kernel_info info; |
| 73 | info.name = kernel->name(); |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 74 | info.prefix = _prefix; |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 75 | info.measurements = _timer.measurements(); |
| 76 | _kernels.push_back(std::move(info)); |
| 77 | } |
| 78 | |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 79 | void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 80 | { |
| 81 | _timer.start(); |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 82 | _real_scheduler.run_tagged_workloads(workloads, tag); |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 83 | _timer.stop(); |
| 84 | |
| 85 | SchedulerTimer::kernel_info info; |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 86 | info.name = tag != nullptr ? tag : "Unknown"; |
Anthony Barbier | 52ecb06 | 2018-05-25 13:32:10 +0100 | [diff] [blame] | 87 | info.prefix = _prefix; |
| 88 | info.measurements = _timer.measurements(); |
| 89 | _kernels.push_back(std::move(info)); |
| 90 | } |
| 91 | |
Anthony Barbier | 148b075 | 2018-09-11 14:19:39 +0100 | [diff] [blame] | 92 | protected: |
| 93 | void run_workloads(std::vector<Workload> &workloads) override |
| 94 | { |
| 95 | ARM_COMPUTE_UNUSED(workloads); |
| 96 | ARM_COMPUTE_ERROR("Can't be reached"); |
| 97 | } |
| 98 | |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 99 | private: |
| 100 | std::list<SchedulerTimer::kernel_info> &_kernels; |
| 101 | IScheduler &_real_scheduler; |
| 102 | WallClockTimer _timer; |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 103 | std::string _prefix; |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | SchedulerTimer::SchedulerTimer(ScaleFactor scale_factor) |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 107 | : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _real_graph_function(nullptr), _scale_factor(scale_factor), _interceptor(nullptr) |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 108 | { |
| 109 | } |
| 110 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 111 | void SchedulerTimer::test_start() |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 112 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 113 | // Start intercepting tasks: |
| 114 | ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr); |
| 115 | _real_graph_function = graph::TaskExecutor::get().execute_function; |
| 116 | auto task_interceptor = [this](graph::ExecutionTask & task) |
| 117 | { |
| 118 | Interceptor *scheduler = nullptr; |
| 119 | if(dynamic_cast<Interceptor *>(this->_interceptor.get()) != nullptr) |
| 120 | { |
| 121 | scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor *>(_interceptor.get()); |
| 122 | if(task.node != nullptr && !task.node->name().empty()) |
| 123 | { |
| 124 | scheduler->set_prefix(task.node->name() + "/"); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | scheduler->set_prefix(""); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | this->_real_graph_function(task); |
| 133 | |
| 134 | if(scheduler != nullptr) |
| 135 | { |
| 136 | scheduler->set_prefix(""); |
| 137 | } |
| 138 | }; |
| 139 | |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr); |
| 141 | _real_scheduler_type = Scheduler::get_type(); |
| 142 | //Note: We can't currently replace a custom scheduler |
| 143 | if(_real_scheduler_type != Scheduler::Type::CUSTOM) |
| 144 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 145 | _real_scheduler = &Scheduler::get(); |
| 146 | _interceptor = std::make_shared<Interceptor>(_kernels, *_real_scheduler, _scale_factor); |
| 147 | Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor)); |
| 148 | graph::TaskExecutor::get().execute_function = task_interceptor; |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 149 | } |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void SchedulerTimer::start() |
| 153 | { |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 154 | _kernels.clear(); |
| 155 | } |
| 156 | |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 157 | void SchedulerTimer::test_stop() |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 158 | { |
| 159 | // Restore real scheduler |
| 160 | Scheduler::set(_real_scheduler_type); |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 161 | _real_scheduler = nullptr; |
| 162 | _interceptor = nullptr; |
| 163 | graph::TaskExecutor::get().execute_function = _real_graph_function; |
| 164 | _real_graph_function = nullptr; |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | Instrument::MeasurementsMap SchedulerTimer::measurements() const |
| 168 | { |
| 169 | MeasurementsMap measurements; |
| 170 | unsigned int kernel_number = 0; |
| 171 | for(auto kernel : _kernels) |
| 172 | { |
Georgios Pinitas | 5c2fb3f | 2018-05-01 15:26:20 +0100 | [diff] [blame] | 173 | measurements.emplace(kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++), kernel.measurements.begin()->second); |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return measurements; |
| 177 | } |
| 178 | } // namespace framework |
| 179 | } // namespace test |
| 180 | } // namespace arm_compute |