blob: c114dfbd9ddd088c01ea8ea0ca8e89ddc2f4df5f [file] [log] [blame]
Anthony Barbiere8a49832018-01-18 10:04:05 +00001/*
Michalis Spyroubcfd09a2019-05-01 13:03:59 +01002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbiere8a49832018-01-18 10:04:05 +00003 *
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 Pinitas5c2fb3f2018-05-01 15:26:20 +010028#include "arm_compute/core/utils/misc/Cast.h"
29#include "arm_compute/graph/INode.h"
Anthony Barbiere8a49832018-01-18 10:04:05 +000030
31namespace arm_compute
32{
33namespace test
34{
35namespace framework
36{
Anthony Barbier72f4ae52018-11-07 17:33:54 +000037template <bool output_timestamps>
38std::string SchedulerClock<output_timestamps>::id() const
Anthony Barbiere8a49832018-01-18 10:04:05 +000039{
Anthony Barbier72f4ae52018-11-07 17:33:54 +000040 if(output_timestamps)
41 {
42 return "SchedulerTimestamps";
43 }
44 else
45 {
46 return "SchedulerTimer";
47 }
Anthony Barbiere8a49832018-01-18 10:04:05 +000048}
49
Anthony Barbier72f4ae52018-11-07 17:33:54 +000050template <bool output_timestamps>
Anthony Barbiere8a49832018-01-18 10:04:05 +000051class Interceptor final : public IScheduler
52{
53public:
54 /** Default constructor. */
Anthony Barbier72f4ae52018-11-07 17:33:54 +000055 Interceptor(std::list<struct SchedulerClock<output_timestamps>::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010056 : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor), _prefix()
Anthony Barbiere8a49832018-01-18 10:04:05 +000057 {
58 }
59
60 void set_num_threads(unsigned int num_threads) override
61 {
62 _real_scheduler.set_num_threads(num_threads);
63 }
64
65 unsigned int num_threads() const override
66 {
67 return _real_scheduler.num_threads();
68 }
69
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010070 void set_prefix(const std::string &prefix)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010071 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010072 _prefix = prefix;
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010073 }
74
Anthony Barbier376c85f2018-05-25 14:17:21 +010075 void schedule(ICPPKernel *kernel, const Hints &hints) override
Anthony Barbiere8a49832018-01-18 10:04:05 +000076 {
77 _timer.start();
Anthony Barbier376c85f2018-05-25 14:17:21 +010078 _real_scheduler.schedule(kernel, hints.split_dimension());
Anthony Barbiere8a49832018-01-18 10:04:05 +000079 _timer.stop();
80
Anthony Barbier72f4ae52018-11-07 17:33:54 +000081 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbiere8a49832018-01-18 10:04:05 +000082 info.name = kernel->name();
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010083 info.prefix = _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +000084 info.measurements = _timer.measurements();
85 _kernels.push_back(std::move(info));
86 }
87
Anthony Barbier148b0752018-09-11 14:19:39 +010088 void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
Anthony Barbier52ecb062018-05-25 13:32:10 +010089 {
90 _timer.start();
Anthony Barbier148b0752018-09-11 14:19:39 +010091 _real_scheduler.run_tagged_workloads(workloads, tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +010092 _timer.stop();
93
Anthony Barbier72f4ae52018-11-07 17:33:54 +000094 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbier148b0752018-09-11 14:19:39 +010095 info.name = tag != nullptr ? tag : "Unknown";
Anthony Barbier52ecb062018-05-25 13:32:10 +010096 info.prefix = _prefix;
97 info.measurements = _timer.measurements();
98 _kernels.push_back(std::move(info));
99 }
100
Anthony Barbier148b0752018-09-11 14:19:39 +0100101protected:
102 void run_workloads(std::vector<Workload> &workloads) override
103 {
104 ARM_COMPUTE_UNUSED(workloads);
105 ARM_COMPUTE_ERROR("Can't be reached");
106 }
107
Anthony Barbiere8a49832018-01-18 10:04:05 +0000108private:
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000109 std::list<struct SchedulerClock<output_timestamps>::kernel_info> &_kernels;
110 IScheduler &_real_scheduler;
111 WallClock<output_timestamps> _timer;
112 std::string _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000113};
114
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000115template <bool output_timestamps>
116SchedulerClock<output_timestamps>::SchedulerClock(ScaleFactor scale_factor)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100117 : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _real_graph_function(nullptr), _scale_factor(scale_factor), _interceptor(nullptr)
Anthony Barbiere8a49832018-01-18 10:04:05 +0000118{
119}
120
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000121template <bool output_timestamps>
122void SchedulerClock<output_timestamps>::test_start()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000123{
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100124 // Start intercepting tasks:
125 ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
126 _real_graph_function = graph::TaskExecutor::get().execute_function;
127 auto task_interceptor = [this](graph::ExecutionTask & task)
128 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000129 Interceptor<output_timestamps> *scheduler = nullptr;
130 if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100131 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000132 scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100133 if(task.node != nullptr && !task.node->name().empty())
134 {
135 scheduler->set_prefix(task.node->name() + "/");
136 }
137 else
138 {
139 scheduler->set_prefix("");
140 }
141 }
142
143 this->_real_graph_function(task);
144
145 if(scheduler != nullptr)
146 {
147 scheduler->set_prefix("");
148 }
149 };
150
Anthony Barbiere8a49832018-01-18 10:04:05 +0000151 ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
152 _real_scheduler_type = Scheduler::get_type();
153 //Note: We can't currently replace a custom scheduler
154 if(_real_scheduler_type != Scheduler::Type::CUSTOM)
155 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100156 _real_scheduler = &Scheduler::get();
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000157 _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, *_real_scheduler, _scale_factor);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100158 Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
159 graph::TaskExecutor::get().execute_function = task_interceptor;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000160 }
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100161}
162
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000163template <bool output_timestamps>
164void SchedulerClock<output_timestamps>::start()
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100165{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000166 _kernels.clear();
167}
168
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000169template <bool output_timestamps>
170void SchedulerClock<output_timestamps>::test_stop()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000171{
172 // Restore real scheduler
173 Scheduler::set(_real_scheduler_type);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100174 _real_scheduler = nullptr;
175 _interceptor = nullptr;
176 graph::TaskExecutor::get().execute_function = _real_graph_function;
177 _real_graph_function = nullptr;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000178}
179
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000180template <bool output_timestamps>
181Instrument::MeasurementsMap SchedulerClock<output_timestamps>::measurements() const
Anthony Barbiere8a49832018-01-18 10:04:05 +0000182{
183 MeasurementsMap measurements;
184 unsigned int kernel_number = 0;
185 for(auto kernel : _kernels)
186 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000187 std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
188 if(output_timestamps)
189 {
190 ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100191 for(auto const &m : kernel.measurements)
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000192 {
193 if(m.first.find("[start]") != std::string::npos)
194 {
195 measurements.emplace("[start]" + name, m.second);
196 }
197 else if(m.first.find("[end]") != std::string::npos)
198 {
199 measurements.emplace("[end]" + name, m.second);
200 }
201 else
202 {
203 ARM_COMPUTE_ERROR("Measurement not handled");
204 }
205 }
206 }
207 else
208 {
209 measurements.emplace(name, kernel.measurements.begin()->second);
210 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000211 }
212
213 return measurements;
214}
Anthony Barbiercc225be2018-11-09 15:35:20 +0000215
Anthony Barbiere8a49832018-01-18 10:04:05 +0000216} // namespace framework
217} // namespace test
218} // namespace arm_compute
Anthony Barbiercc225be2018-11-09 15:35:20 +0000219
220template class arm_compute::test::framework::SchedulerClock<true>;
221template class arm_compute::test::framework::SchedulerClock<false>;