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" |
| 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace test |
| 32 | { |
| 33 | namespace framework |
| 34 | { |
| 35 | std::string SchedulerTimer::id() const |
| 36 | { |
| 37 | return "SchedulerTimer"; |
| 38 | } |
| 39 | |
| 40 | class Interceptor final : public IScheduler |
| 41 | { |
| 42 | public: |
| 43 | /** Default constructor. */ |
| 44 | Interceptor(std::list<SchedulerTimer::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor) |
| 45 | : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void set_num_threads(unsigned int num_threads) override |
| 50 | { |
| 51 | _real_scheduler.set_num_threads(num_threads); |
| 52 | } |
| 53 | |
| 54 | unsigned int num_threads() const override |
| 55 | { |
| 56 | return _real_scheduler.num_threads(); |
| 57 | } |
| 58 | |
| 59 | void schedule(ICPPKernel *kernel, unsigned int split_dimension) override |
| 60 | { |
| 61 | _timer.start(); |
| 62 | _real_scheduler.schedule(kernel, split_dimension); |
| 63 | _timer.stop(); |
| 64 | |
| 65 | SchedulerTimer::kernel_info info; |
| 66 | info.name = kernel->name(); |
| 67 | info.measurements = _timer.measurements(); |
| 68 | _kernels.push_back(std::move(info)); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | std::list<SchedulerTimer::kernel_info> &_kernels; |
| 73 | IScheduler &_real_scheduler; |
| 74 | WallClockTimer _timer; |
| 75 | }; |
| 76 | |
| 77 | SchedulerTimer::SchedulerTimer(ScaleFactor scale_factor) |
| 78 | : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _scale_factor(scale_factor) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | void SchedulerTimer::start() |
| 83 | { |
| 84 | ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr); |
| 85 | _real_scheduler_type = Scheduler::get_type(); |
| 86 | //Note: We can't currently replace a custom scheduler |
| 87 | if(_real_scheduler_type != Scheduler::Type::CUSTOM) |
| 88 | { |
| 89 | _real_scheduler = &Scheduler::get(); |
| 90 | auto interceptor = std::make_shared<Interceptor>(_kernels, *_real_scheduler, _scale_factor); |
| 91 | Scheduler::set(std::static_pointer_cast<IScheduler>(interceptor)); |
| 92 | } |
| 93 | _kernels.clear(); |
| 94 | } |
| 95 | |
| 96 | void SchedulerTimer::stop() |
| 97 | { |
| 98 | // Restore real scheduler |
| 99 | Scheduler::set(_real_scheduler_type); |
| 100 | _real_scheduler = nullptr; |
| 101 | } |
| 102 | |
| 103 | Instrument::MeasurementsMap SchedulerTimer::measurements() const |
| 104 | { |
| 105 | MeasurementsMap measurements; |
| 106 | unsigned int kernel_number = 0; |
| 107 | for(auto kernel : _kernels) |
| 108 | { |
| 109 | measurements.emplace(kernel.name + " #" + support::cpp11::to_string(kernel_number++), kernel.measurements.begin()->second); |
| 110 | } |
| 111 | |
| 112 | return measurements; |
| 113 | } |
| 114 | } // namespace framework |
| 115 | } // namespace test |
| 116 | } // namespace arm_compute |