blob: aa69bc297dda39b872cc7cb9f13c9f8d9032c2f1 [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
Georgios Pinitas06e890b2020-07-09 18:38:34 +010066 void set_num_threads_with_affinity(unsigned int num_threads, BindFunc func) override
67 {
68 _real_scheduler.set_num_threads_with_affinity(num_threads, func);
69 }
70
Anthony Barbiere8a49832018-01-18 10:04:05 +000071 unsigned int num_threads() const override
72 {
73 return _real_scheduler.num_threads();
74 }
75
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010076 void set_prefix(const std::string &prefix)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010077 {
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010078 _prefix = prefix;
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010079 }
80
Anthony Barbier376c85f2018-05-25 14:17:21 +010081 void schedule(ICPPKernel *kernel, const Hints &hints) override
Anthony Barbiere8a49832018-01-18 10:04:05 +000082 {
83 _timer.start();
Georgios Pinitas77d42522019-11-05 13:35:47 +000084 _real_scheduler.schedule(kernel, hints);
Anthony Barbiere8a49832018-01-18 10:04:05 +000085 _timer.stop();
86
Anthony Barbier72f4ae52018-11-07 17:33:54 +000087 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbiere8a49832018-01-18 10:04:05 +000088 info.name = kernel->name();
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010089 info.prefix = _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +000090 info.measurements = _timer.measurements();
91 _kernels.push_back(std::move(info));
92 }
93
Georgios Pinitas0499dff2020-07-31 22:21:38 +010094 void schedule_op(ICPPKernel *kernel, const Hints &hints, ITensorPack &tensors) override
Michalis Spyroubcd23522020-05-21 15:02:36 +010095 {
96 _timer.start();
Georgios Pinitas0499dff2020-07-31 22:21:38 +010097 _real_scheduler.schedule_op(kernel, hints, tensors);
Michalis Spyroubcd23522020-05-21 15:02:36 +010098 _timer.stop();
99
100 typename SchedulerClock<output_timestamps>::kernel_info info;
101 info.name = kernel->name();
102 info.prefix = _prefix;
103 info.measurements = _timer.measurements();
104 _kernels.push_back(std::move(info));
105 }
106
Anthony Barbier148b0752018-09-11 14:19:39 +0100107 void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
Anthony Barbier52ecb062018-05-25 13:32:10 +0100108 {
109 _timer.start();
Anthony Barbier148b0752018-09-11 14:19:39 +0100110 _real_scheduler.run_tagged_workloads(workloads, tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +0100111 _timer.stop();
112
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000113 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbier148b0752018-09-11 14:19:39 +0100114 info.name = tag != nullptr ? tag : "Unknown";
Anthony Barbier52ecb062018-05-25 13:32:10 +0100115 info.prefix = _prefix;
116 info.measurements = _timer.measurements();
117 _kernels.push_back(std::move(info));
118 }
119
Anthony Barbier148b0752018-09-11 14:19:39 +0100120protected:
121 void run_workloads(std::vector<Workload> &workloads) override
122 {
123 ARM_COMPUTE_UNUSED(workloads);
124 ARM_COMPUTE_ERROR("Can't be reached");
125 }
126
Anthony Barbiere8a49832018-01-18 10:04:05 +0000127private:
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000128 std::list<struct SchedulerClock<output_timestamps>::kernel_info> &_kernels;
129 IScheduler &_real_scheduler;
130 WallClock<output_timestamps> _timer;
131 std::string _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000132};
133
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000134template <bool output_timestamps>
135SchedulerClock<output_timestamps>::SchedulerClock(ScaleFactor scale_factor)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100136 : _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 +0000137{
Georgios Pinitas12833d02019-07-25 13:31:10 +0100138 if(instruments_info != nullptr)
139 {
140 _scheduler_users = instruments_info->_scheduler_users;
141 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000142}
143
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000144template <bool output_timestamps>
145void SchedulerClock<output_timestamps>::test_start()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000146{
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100147 // Start intercepting tasks:
148 ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
149 _real_graph_function = graph::TaskExecutor::get().execute_function;
150 auto task_interceptor = [this](graph::ExecutionTask & task)
151 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000152 Interceptor<output_timestamps> *scheduler = nullptr;
153 if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100154 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000155 scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100156 if(task.node != nullptr && !task.node->name().empty())
157 {
158 scheduler->set_prefix(task.node->name() + "/");
159 }
160 else
161 {
162 scheduler->set_prefix("");
163 }
164 }
165
166 this->_real_graph_function(task);
167
168 if(scheduler != nullptr)
169 {
170 scheduler->set_prefix("");
171 }
172 };
173
Anthony Barbiere8a49832018-01-18 10:04:05 +0000174 ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
175 _real_scheduler_type = Scheduler::get_type();
176 //Note: We can't currently replace a custom scheduler
177 if(_real_scheduler_type != Scheduler::Type::CUSTOM)
178 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100179 _real_scheduler = &Scheduler::get();
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000180 _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, *_real_scheduler, _scale_factor);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100181 Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
182 graph::TaskExecutor::get().execute_function = task_interceptor;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100183
184 // Create an interceptor for each scheduler
185 // TODO(COMPID-2638) : Allow multiple schedulers, now it assumes the same scheduler is used.
186 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
187 [&](ISchedulerUser * user)
188 {
189 if(user != nullptr && user->scheduler() != nullptr)
190 {
191 user->intercept_scheduler(support::cpp14::make_unique<Interceptor<output_timestamps>>(_kernels, *user->scheduler(), _scale_factor));
192 }
193 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000194 }
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100195}
196
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000197template <bool output_timestamps>
198void SchedulerClock<output_timestamps>::start()
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100199{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000200 _kernels.clear();
201}
202
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000203template <bool output_timestamps>
204void SchedulerClock<output_timestamps>::test_stop()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000205{
206 // Restore real scheduler
207 Scheduler::set(_real_scheduler_type);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100208 _real_scheduler = nullptr;
209 _interceptor = nullptr;
210 graph::TaskExecutor::get().execute_function = _real_graph_function;
211 _real_graph_function = nullptr;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100212
213 // Restore schedulers
214 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
215 [&](ISchedulerUser * user)
216 {
217 if(user != nullptr)
218 {
219 user->restore_scheduler();
220 }
221 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000222}
223
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000224template <bool output_timestamps>
225Instrument::MeasurementsMap SchedulerClock<output_timestamps>::measurements() const
Anthony Barbiere8a49832018-01-18 10:04:05 +0000226{
227 MeasurementsMap measurements;
228 unsigned int kernel_number = 0;
229 for(auto kernel : _kernels)
230 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000231 std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
232 if(output_timestamps)
233 {
234 ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100235 for(auto const &m : kernel.measurements)
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000236 {
237 if(m.first.find("[start]") != std::string::npos)
238 {
239 measurements.emplace("[start]" + name, m.second);
240 }
241 else if(m.first.find("[end]") != std::string::npos)
242 {
243 measurements.emplace("[end]" + name, m.second);
244 }
245 else
246 {
247 ARM_COMPUTE_ERROR("Measurement not handled");
248 }
249 }
250 }
251 else
252 {
253 measurements.emplace(name, kernel.measurements.begin()->second);
254 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000255 }
256
257 return measurements;
258}
Anthony Barbiercc225be2018-11-09 15:35:20 +0000259
Anthony Barbiere8a49832018-01-18 10:04:05 +0000260} // namespace framework
261} // namespace test
262} // namespace arm_compute
Anthony Barbiercc225be2018-11-09 15:35:20 +0000263
264template class arm_compute::test::framework::SchedulerClock<true>;
265template class arm_compute::test::framework::SchedulerClock<false>;