blob: 9e8bba28e81c608a0442688f26bf04003aec942e [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
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
Anthony Barbier148b0752018-09-11 14:19:39 +010089 void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
Anthony Barbier52ecb062018-05-25 13:32:10 +010090 {
91 _timer.start();
Anthony Barbier148b0752018-09-11 14:19:39 +010092 _real_scheduler.run_tagged_workloads(workloads, tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +010093 _timer.stop();
94
Anthony Barbier72f4ae52018-11-07 17:33:54 +000095 typename SchedulerClock<output_timestamps>::kernel_info info;
Anthony Barbier148b0752018-09-11 14:19:39 +010096 info.name = tag != nullptr ? tag : "Unknown";
Anthony Barbier52ecb062018-05-25 13:32:10 +010097 info.prefix = _prefix;
98 info.measurements = _timer.measurements();
99 _kernels.push_back(std::move(info));
100 }
101
Anthony Barbier148b0752018-09-11 14:19:39 +0100102protected:
103 void run_workloads(std::vector<Workload> &workloads) override
104 {
105 ARM_COMPUTE_UNUSED(workloads);
106 ARM_COMPUTE_ERROR("Can't be reached");
107 }
108
Anthony Barbiere8a49832018-01-18 10:04:05 +0000109private:
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000110 std::list<struct SchedulerClock<output_timestamps>::kernel_info> &_kernels;
111 IScheduler &_real_scheduler;
112 WallClock<output_timestamps> _timer;
113 std::string _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000114};
115
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000116template <bool output_timestamps>
117SchedulerClock<output_timestamps>::SchedulerClock(ScaleFactor scale_factor)
Georgios Pinitas12833d02019-07-25 13:31:10 +0100118 : _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 +0000119{
Georgios Pinitas12833d02019-07-25 13:31:10 +0100120 if(instruments_info != nullptr)
121 {
122 _scheduler_users = instruments_info->_scheduler_users;
123 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000124}
125
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000126template <bool output_timestamps>
127void SchedulerClock<output_timestamps>::test_start()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000128{
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100129 // Start intercepting tasks:
130 ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
131 _real_graph_function = graph::TaskExecutor::get().execute_function;
132 auto task_interceptor = [this](graph::ExecutionTask & task)
133 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000134 Interceptor<output_timestamps> *scheduler = nullptr;
135 if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100136 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000137 scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100138 if(task.node != nullptr && !task.node->name().empty())
139 {
140 scheduler->set_prefix(task.node->name() + "/");
141 }
142 else
143 {
144 scheduler->set_prefix("");
145 }
146 }
147
148 this->_real_graph_function(task);
149
150 if(scheduler != nullptr)
151 {
152 scheduler->set_prefix("");
153 }
154 };
155
Anthony Barbiere8a49832018-01-18 10:04:05 +0000156 ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
157 _real_scheduler_type = Scheduler::get_type();
158 //Note: We can't currently replace a custom scheduler
159 if(_real_scheduler_type != Scheduler::Type::CUSTOM)
160 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100161 _real_scheduler = &Scheduler::get();
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000162 _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, *_real_scheduler, _scale_factor);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100163 Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
164 graph::TaskExecutor::get().execute_function = task_interceptor;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100165
166 // Create an interceptor for each scheduler
167 // TODO(COMPID-2638) : Allow multiple schedulers, now it assumes the same scheduler is used.
168 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
169 [&](ISchedulerUser * user)
170 {
171 if(user != nullptr && user->scheduler() != nullptr)
172 {
173 user->intercept_scheduler(support::cpp14::make_unique<Interceptor<output_timestamps>>(_kernels, *user->scheduler(), _scale_factor));
174 }
175 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000176 }
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100177}
178
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000179template <bool output_timestamps>
180void SchedulerClock<output_timestamps>::start()
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100181{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000182 _kernels.clear();
183}
184
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000185template <bool output_timestamps>
186void SchedulerClock<output_timestamps>::test_stop()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000187{
188 // Restore real scheduler
189 Scheduler::set(_real_scheduler_type);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100190 _real_scheduler = nullptr;
191 _interceptor = nullptr;
192 graph::TaskExecutor::get().execute_function = _real_graph_function;
193 _real_graph_function = nullptr;
Georgios Pinitas12833d02019-07-25 13:31:10 +0100194
195 // Restore schedulers
196 std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
197 [&](ISchedulerUser * user)
198 {
199 if(user != nullptr)
200 {
201 user->restore_scheduler();
202 }
203 });
Anthony Barbiere8a49832018-01-18 10:04:05 +0000204}
205
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000206template <bool output_timestamps>
207Instrument::MeasurementsMap SchedulerClock<output_timestamps>::measurements() const
Anthony Barbiere8a49832018-01-18 10:04:05 +0000208{
209 MeasurementsMap measurements;
210 unsigned int kernel_number = 0;
211 for(auto kernel : _kernels)
212 {
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000213 std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
214 if(output_timestamps)
215 {
216 ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100217 for(auto const &m : kernel.measurements)
Anthony Barbier72f4ae52018-11-07 17:33:54 +0000218 {
219 if(m.first.find("[start]") != std::string::npos)
220 {
221 measurements.emplace("[start]" + name, m.second);
222 }
223 else if(m.first.find("[end]") != std::string::npos)
224 {
225 measurements.emplace("[end]" + name, m.second);
226 }
227 else
228 {
229 ARM_COMPUTE_ERROR("Measurement not handled");
230 }
231 }
232 }
233 else
234 {
235 measurements.emplace(name, kernel.measurements.begin()->second);
236 }
Anthony Barbiere8a49832018-01-18 10:04:05 +0000237 }
238
239 return measurements;
240}
Anthony Barbiercc225be2018-11-09 15:35:20 +0000241
Anthony Barbiere8a49832018-01-18 10:04:05 +0000242} // namespace framework
243} // namespace test
244} // namespace arm_compute
Anthony Barbiercc225be2018-11-09 15:35:20 +0000245
246template class arm_compute::test::framework::SchedulerClock<true>;
247template class arm_compute::test::framework::SchedulerClock<false>;