blob: 76f1a58e9c8b30033030906cb0fcc42986d89a13 [file] [log] [blame]
Anthony Barbiere8a49832018-01-18 10:04:05 +00001/*
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"
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{
37std::string SchedulerTimer::id() const
38{
39 return "SchedulerTimer";
40}
41
42class Interceptor final : public IScheduler
43{
44public:
45 /** Default constructor. */
46 Interceptor(std::list<SchedulerTimer::kernel_info> &kernels, IScheduler &real_scheduler, ScaleFactor scale_factor)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010047 : _kernels(kernels), _real_scheduler(real_scheduler), _timer(scale_factor), _prefix()
Anthony Barbiere8a49832018-01-18 10:04:05 +000048 {
49 }
50
51 void set_num_threads(unsigned int num_threads) override
52 {
53 _real_scheduler.set_num_threads(num_threads);
54 }
55
56 unsigned int num_threads() const override
57 {
58 return _real_scheduler.num_threads();
59 }
60
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010061 void set_prefix(std::string prefix)
62 {
63 _prefix = std::move(prefix);
64 }
65
Anthony Barbier376c85f2018-05-25 14:17:21 +010066 void schedule(ICPPKernel *kernel, const Hints &hints) override
Anthony Barbiere8a49832018-01-18 10:04:05 +000067 {
68 _timer.start();
Anthony Barbier376c85f2018-05-25 14:17:21 +010069 _real_scheduler.schedule(kernel, hints.split_dimension());
Anthony Barbiere8a49832018-01-18 10:04:05 +000070 _timer.stop();
71
72 SchedulerTimer::kernel_info info;
73 info.name = kernel->name();
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +010074 info.prefix = _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +000075 info.measurements = _timer.measurements();
76 _kernels.push_back(std::move(info));
77 }
78
Anthony Barbier148b0752018-09-11 14:19:39 +010079 void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
Anthony Barbier52ecb062018-05-25 13:32:10 +010080 {
81 _timer.start();
Anthony Barbier148b0752018-09-11 14:19:39 +010082 _real_scheduler.run_tagged_workloads(workloads, tag);
Anthony Barbier52ecb062018-05-25 13:32:10 +010083 _timer.stop();
84
85 SchedulerTimer::kernel_info info;
Anthony Barbier148b0752018-09-11 14:19:39 +010086 info.name = tag != nullptr ? tag : "Unknown";
Anthony Barbier52ecb062018-05-25 13:32:10 +010087 info.prefix = _prefix;
88 info.measurements = _timer.measurements();
89 _kernels.push_back(std::move(info));
90 }
91
Anthony Barbier148b0752018-09-11 14:19:39 +010092protected:
93 void run_workloads(std::vector<Workload> &workloads) override
94 {
95 ARM_COMPUTE_UNUSED(workloads);
96 ARM_COMPUTE_ERROR("Can't be reached");
97 }
98
Anthony Barbiere8a49832018-01-18 10:04:05 +000099private:
100 std::list<SchedulerTimer::kernel_info> &_kernels;
101 IScheduler &_real_scheduler;
102 WallClockTimer _timer;
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100103 std::string _prefix;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000104};
105
106SchedulerTimer::SchedulerTimer(ScaleFactor scale_factor)
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100107 : _kernels(), _real_scheduler(nullptr), _real_scheduler_type(), _real_graph_function(nullptr), _scale_factor(scale_factor), _interceptor(nullptr)
Anthony Barbiere8a49832018-01-18 10:04:05 +0000108{
109}
110
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100111void SchedulerTimer::test_start()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000112{
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100113 // Start intercepting tasks:
114 ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
115 _real_graph_function = graph::TaskExecutor::get().execute_function;
116 auto task_interceptor = [this](graph::ExecutionTask & task)
117 {
118 Interceptor *scheduler = nullptr;
119 if(dynamic_cast<Interceptor *>(this->_interceptor.get()) != nullptr)
120 {
121 scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor *>(_interceptor.get());
122 if(task.node != nullptr && !task.node->name().empty())
123 {
124 scheduler->set_prefix(task.node->name() + "/");
125 }
126 else
127 {
128 scheduler->set_prefix("");
129 }
130 }
131
132 this->_real_graph_function(task);
133
134 if(scheduler != nullptr)
135 {
136 scheduler->set_prefix("");
137 }
138 };
139
Anthony Barbiere8a49832018-01-18 10:04:05 +0000140 ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
141 _real_scheduler_type = Scheduler::get_type();
142 //Note: We can't currently replace a custom scheduler
143 if(_real_scheduler_type != Scheduler::Type::CUSTOM)
144 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100145 _real_scheduler = &Scheduler::get();
146 _interceptor = std::make_shared<Interceptor>(_kernels, *_real_scheduler, _scale_factor);
147 Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
148 graph::TaskExecutor::get().execute_function = task_interceptor;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000149 }
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100150}
151
152void SchedulerTimer::start()
153{
Anthony Barbiere8a49832018-01-18 10:04:05 +0000154 _kernels.clear();
155}
156
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100157void SchedulerTimer::test_stop()
Anthony Barbiere8a49832018-01-18 10:04:05 +0000158{
159 // Restore real scheduler
160 Scheduler::set(_real_scheduler_type);
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100161 _real_scheduler = nullptr;
162 _interceptor = nullptr;
163 graph::TaskExecutor::get().execute_function = _real_graph_function;
164 _real_graph_function = nullptr;
Anthony Barbiere8a49832018-01-18 10:04:05 +0000165}
166
167Instrument::MeasurementsMap SchedulerTimer::measurements() const
168{
169 MeasurementsMap measurements;
170 unsigned int kernel_number = 0;
171 for(auto kernel : _kernels)
172 {
Georgios Pinitas5c2fb3f2018-05-01 15:26:20 +0100173 measurements.emplace(kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++), kernel.measurements.begin()->second);
Anthony Barbiere8a49832018-01-18 10:04:05 +0000174 }
175
176 return measurements;
177}
178} // namespace framework
179} // namespace test
180} // namespace arm_compute