blob: ab1dbbfb4cf1b0b5b2365982cba20f7432c5d307 [file] [log] [blame]
Anthony Barbiere8a49832018-01-18 10:04:05 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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
Georgios Pinitas12833d02019-07-25 13:31:10 +010026#include "Instruments.h"
Anthony Barbiere8a49832018-01-18 10:04:05 +000027#include "WallClockTimer.h"
28#include "arm_compute/core/CPP/ICPPKernel.h"
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010029#include "arm_compute/core/utils/misc/Cast.h"
30#include "arm_compute/graph/INode.h"
Anthony Barbiere8a49832018-01-18 10:04:05 +000031
32namespace arm_compute
33{
34namespace test
35{
36namespace framework
37{
Anthony Barbier72f4ae52018-11-07 17:33:54 +000038template <bool output_timestamps>
39std::string SchedulerClock<output_timestamps>::id() const
Anthony Barbiere8a49832018-01-18 10:04:05 +000040{
Anthony Barbier72f4ae52018-11-07 17:33:54 +000041 if(output_timestamps)
42 {
43 return "SchedulerTimestamps";
44 }
45 else
46 {
47 return "SchedulerTimer";
48 }
Anthony Barbiere8a49832018-01-18 10:04:05 +000049}
50
Anthony Barbier72f4ae52018-11-07 17:33:54 +000051template <bool output_timestamps>
Anthony Barbiere8a49832018-01-18 10:04:05 +000052class Interceptor final : public IScheduler
53{
54public:
55 /** Default constructor. */
Anthony Barbier72f4ae52018-11-07 17:33:54 +000056 Interceptor(std::list<struct SchedulerClock<output_timestamps>::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010057 : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor), _prefix()
Anthony Barbiere8a49832018-01-18 10:04:05 +000058 {
59 }
60
61 void set_num_threads(unsigned int num_threads) override
62 {
63 _real_scheduler.set_num_threads(num_threads);
64 }
65
66 unsigned int num_threads() const override
67 {
68 return _real_scheduler.num_threads();
69 }
70
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010071 void set_prefix(const std::string &prefix)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010072 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010073 _prefix = prefix;
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010074 }
75
Anthony Barbier376c85f2018-05-25 14:17:21 +010076 void schedule(ICPPKernel *kernel, const Hints &hints) override
Anthony Barbiere8a49832018-01-18 10:04:05 +000077 {
78 _timer.start();
Georgios Pinitas77d42522019-11-05 13:35:47 +000079 _real_scheduler.schedule(kernel, hints);
Anthony Barbiere8a49832018-01-18 10:04:05 +000080 _timer.stop();
81
Anthony Barbier72f4ae52018-11-07 17:33:54 +000082 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbiere8a49832018-01-18 10:04:05 +000083 info.name = kernel->name();
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010084 info.prefix = _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +000085 info.measurements = _timer.measurements();
86 _kernels.push_back(std::move(info));
87 }
88
Michalis Spyrouce0c6752020-06-18 10:14:57 +010089 void schedule_op(ICPPKernel *kernel, const Hints &hints, const InputTensorMap &inputs, const OutputTensorMap &outputs) override
Michalis Spyroubcd23522020-05-21 15:02:36 +010090 {
91 _timer.start();
92 _real_scheduler.schedule_op(kernel, hints, inputs, outputs);
93 _timer.stop();
94
95 typename SchedulerClock<output_timestamps>::kernel_info info;
96 info.name = kernel->name();
97 info.prefix = _prefix;
98 info.measurements = _timer.measurements();
99 _kernels.push_back(std::move(info));
100 }
101
Anthony Barbier148b0752018-09-11 14:19:39 +0100102 void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
Anthony Barbier52ecb062018-05-25 13:32:10 +0100103 {
104 _timer.start();
Anthony Barbier148b0752018-09-11 14:19:39 +0100105 _real_scheduler.run_tagged_workloads(workloads, tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +0100106 _timer.stop();
107
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000108 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbier148b0752018-09-11 14:19:39 +0100109 info.name = tag != nullptr ? tag : "Unknown";
Anthony Barbier52ecb062018-05-25 13:32:10 +0100110 info.prefix = _prefix;
111 info.measurements = _timer.measurements();
112 _kernels.push_back(std::move(info));
113 }
114
Anthony Barbier148b0752018-09-11 14:19:39 +0100115protected:
116 void run_workloads(std::vector<Workload> &workloads) override
117 {
118 ARM_COMPUTE_UNUSED(workloads);
119 ARM_COMPUTE_ERROR("Can't be reached");
120 }
121
Anthony Barbiere8a49832018-01-18 10:04:05 +0000122private:
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000123 std::list<struct SchedulerClock<output_timestamps>::kernel_info> &_kernels;
124 IScheduler &_real_scheduler;
125 WallClock<output_timestamps> _timer;
126 std::string _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000127};
128
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000129template <bool output_timestamps>
130SchedulerClock<output_timestamps>::SchedulerClock(ScaleFactor scale_factor)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100131 : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _real_graph_function(nullptr), _scale_factor(scale_factor), _interceptor(nullptr), _scheduler_users()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000132{
Georgios Pinitas12833d02019-07-25 13:31:10 +0100133 if(instruments_info != nullptr)
134 {
135 _scheduler_users = instruments_info->_scheduler_users;
136 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000137}
138
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000139template <bool output_timestamps>
140void SchedulerClock<output_timestamps>::test_start()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000141{
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100142 // Start intercepting tasks:
143 ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
144 _real_graph_function = graph::TaskExecutor::get().execute_function;
145 auto task_interceptor = [this](graph::ExecutionTask & task)
146 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000147 Interceptor<output_timestamps> *scheduler = nullptr;
148 if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100149 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000150 scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100151 if(task.node != nullptr && !task.node->name().empty())
152 {
153 scheduler->set_prefix(task.node->name() + "/");
154 }
155 else
156 {
157 scheduler->set_prefix("");
158 }
159 }
160
161 this->_real_graph_function(task);
162
163 if(scheduler != nullptr)
164 {
165 scheduler->set_prefix("");
166 }
167 };
168
Anthony Barbiere8a49832018-01-18 10:04:05 +0000169 ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
170 _real_scheduler_type = Scheduler::get_type();
171 //Note: We can't currently replace a custom scheduler
172 if(_real_scheduler_type != Scheduler::Type::CUSTOM)
173 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100174 _real_scheduler = &Scheduler::get();
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000175 _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, *_real_scheduler, _scale_factor);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100176 Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
177 graph::TaskExecutor::get().execute_function = task_interceptor;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100178
179 // Create an interceptor for each scheduler
180 // TODO(COMPID-2638) : Allow multiple schedulers, now it assumes the same scheduler is used.
181 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
182 [&](ISchedulerUser * user)
183 {
184 if(user != nullptr && user->scheduler() != nullptr)
185 {
186 user->intercept_scheduler(support::cpp14::make_unique<Interceptor<output_timestamps>>(_kernels, *user->scheduler(), _scale_factor));
187 }
188 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000189 }
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100190}
191
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000192template <bool output_timestamps>
193void SchedulerClock<output_timestamps>::start()
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100194{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000195 _kernels.clear();
196}
197
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000198template <bool output_timestamps>
199void SchedulerClock<output_timestamps>::test_stop()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000200{
201 // Restore real scheduler
202 Scheduler::set(_real_scheduler_type);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100203 _real_scheduler = nullptr;
204 _interceptor = nullptr;
205 graph::TaskExecutor::get().execute_function = _real_graph_function;
206 _real_graph_function = nullptr;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100207
208 // Restore schedulers
209 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
210 [&](ISchedulerUser * user)
211 {
212 if(user != nullptr)
213 {
214 user->restore_scheduler();
215 }
216 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000217}
218
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000219template <bool output_timestamps>
220Instrument::MeasurementsMap SchedulerClock<output_timestamps>::measurements() const
Anthony Barbiere8a49832018-01-18 10:04:05 +0000221{
222 MeasurementsMap measurements;
223 unsigned int kernel_number = 0;
224 for(auto kernel : _kernels)
225 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000226 std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
227 if(output_timestamps)
228 {
229 ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100230 for(auto const &m : kernel.measurements)
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000231 {
232 if(m.first.find("[start]") != std::string::npos)
233 {
234 measurements.emplace("[start]" + name, m.second);
235 }
236 else if(m.first.find("[end]") != std::string::npos)
237 {
238 measurements.emplace("[end]" + name, m.second);
239 }
240 else
241 {
242 ARM_COMPUTE_ERROR("Measurement not handled");
243 }
244 }
245 }
246 else
247 {
248 measurements.emplace(name, kernel.measurements.begin()->second);
249 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000250 }
251
252 return measurements;
253}
Anthony Barbiercc225be2018-11-09 15:35:20 +0000254
Anthony Barbiere8a49832018-01-18 10:04:05 +0000255} // namespace framework
256} // namespace test
257} // namespace arm_compute
Anthony Barbiercc225be2018-11-09 15:35:20 +0000258
259template class arm_compute::test::framework::SchedulerClock<true>;
260template class arm_compute::test::framework::SchedulerClock<false>;