Compute Library
 21.08
SchedulerTimer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 "Instruments.h"
27 #include "WallClockTimer.h"
31 #include "support/Cast.h"
32 
33 namespace arm_compute
34 {
35 namespace test
36 {
37 namespace framework
38 {
39 template <bool output_timestamps>
41 {
42  if(output_timestamps)
43  {
44  return "SchedulerTimestamps";
45  }
46  else
47  {
48  return "SchedulerTimer";
49  }
50 }
51 
52 template <bool output_timestamps>
53 class Interceptor final : public IScheduler
54 {
55 public:
56  /** Default constructor. */
57  Interceptor(std::list<struct SchedulerClock<output_timestamps>::kernel_info> &kernels,
58  std::map<std::string, SchedulerTimer::LayerData> &layers, IScheduler &real_scheduler,
59  ScaleFactor scale_factor)
60  : _kernels(kernels), _layer_data_map(layers), _real_scheduler(real_scheduler), _timer(scale_factor), _prefix()
61  {
62  }
63 
64  void set_num_threads(unsigned int num_threads) override
65  {
66  _real_scheduler.set_num_threads(num_threads);
67  }
68 
69  void set_num_threads_with_affinity(unsigned int num_threads, BindFunc func) override
70  {
71  _real_scheduler.set_num_threads_with_affinity(num_threads, func);
72  }
73 
74  unsigned int num_threads() const override
75  {
76  return _real_scheduler.num_threads();
77  }
78 
79  void set_prefix(const std::string &prefix)
80  {
81  _prefix = prefix;
82  }
83 
84  void schedule(ICPPKernel *kernel, const Hints &hints) override
85  {
86  _timer.start();
87  _real_scheduler.schedule(kernel, hints);
88  _timer.stop();
89 
91  info.name = kernel->name();
92  info.prefix = _prefix;
93  info.measurements = _timer.measurements();
94  _kernels.push_back(std::move(info));
95  }
96 
97  void schedule_op(ICPPKernel *kernel, const Hints &hints, const Window &window, ITensorPack &tensors) override
98  {
99  _timer.start();
100  _real_scheduler.schedule_op(kernel, hints, window, tensors);
101  _timer.stop();
102 
104  info.name = kernel->name();
105  info.prefix = _prefix;
106  info.measurements = _timer.measurements();
107  _kernels.push_back(std::move(info));
108  }
109 
110  void run_tagged_workloads(std::vector<Workload> &workloads, const char *tag) override
111  {
112  _timer.start();
113  _real_scheduler.run_tagged_workloads(workloads, tag);
114  _timer.stop();
115 
117  info.name = tag != nullptr ? tag : "Unknown";
118  info.prefix = _prefix;
119  info.measurements = _timer.measurements();
120  _kernels.push_back(std::move(info));
121  }
122 
123 protected:
124  void run_workloads(std::vector<Workload> &workloads) override
125  {
126  ARM_COMPUTE_UNUSED(workloads);
127  ARM_COMPUTE_ERROR("Can't be reached");
128  }
129 
130 private:
131  std::list<struct SchedulerClock<output_timestamps>::kernel_info> &_kernels;
132  std::map<std::string, SchedulerTimer::LayerData> &_layer_data_map;
133  IScheduler &_real_scheduler;
135  std::string _prefix;
136 };
137 
138 template <bool output_timestamps>
140  : _kernels(), _layer_data_map(), _real_scheduler(nullptr), _real_scheduler_type(), _real_graph_function(nullptr),
141  _scale_factor(scale_factor), _interceptor(nullptr), _scheduler_users()
142 {
143  if(instruments_info != nullptr)
144  {
145  _scheduler_users = instruments_info->_scheduler_users;
146  }
147 }
148 
149 template <bool output_timestamps>
151 {
152  // Start intercepting tasks:
153  ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
154  _real_graph_function = graph::TaskExecutor::get().execute_function;
155  auto task_interceptor = [this](graph::ExecutionTask & task)
156  {
157  Interceptor<output_timestamps> *scheduler = nullptr;
158  if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
159  {
160  scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
161  if(task.node != nullptr && !task.node->name().empty())
162  {
163  scheduler->set_prefix(task.node->name() + "/");
164 
165  if(_layer_data_map.find(task.node->name()) == _layer_data_map.end())
166  {
168  task.node->accept(dlv);
169  _layer_data_map[task.node->name()] = dlv.layer_data();
170  }
171  }
172  else
173  {
174  scheduler->set_prefix("");
175  }
176  }
177 
178  this->_real_graph_function(task);
179 
180  if(scheduler != nullptr)
181  {
182  scheduler->set_prefix("");
183  }
184  };
185 
186  ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
187  _real_scheduler_type = Scheduler::get_type();
188  //Note: We can't currently replace a custom scheduler
189  if(_real_scheduler_type != Scheduler::Type::CUSTOM)
190  {
191  _real_scheduler = &Scheduler::get();
192  _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, _layer_data_map, *_real_scheduler, _scale_factor);
193  Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
194  graph::TaskExecutor::get().execute_function = task_interceptor;
195 
196  // Create an interceptor for each scheduler
197  // TODO(COMPID-2638) : Allow multiple schedulers, now it assumes the same scheduler is used.
198  std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
199  [&](ISchedulerUser * user)
200  {
201  if(user != nullptr && user->scheduler() != nullptr)
202  {
203  user->intercept_scheduler(std::make_unique<Interceptor<output_timestamps>>(_kernels, _layer_data_map, *user->scheduler(), _scale_factor));
204  }
205  });
206  }
207 }
208 
209 template <bool output_timestamps>
211 {
212  _kernels.clear();
213 }
214 
215 template <bool output_timestamps>
217 {
218  // Restore real scheduler
219  Scheduler::set(_real_scheduler_type);
220  _real_scheduler = nullptr;
221  _interceptor = nullptr;
222  graph::TaskExecutor::get().execute_function = _real_graph_function;
223  _real_graph_function = nullptr;
224 
225  // Restore schedulers
226  std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
227  [&](ISchedulerUser * user)
228  {
229  if(user != nullptr)
230  {
231  user->restore_scheduler();
232  }
233  });
234 }
235 
236 template <bool output_timestamps>
238 {
240  unsigned int kernel_number = 0;
241  for(auto kernel : _kernels)
242  {
243  std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
244  if(output_timestamps)
245  {
246  ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
247  for(auto const &m : kernel.measurements)
248  {
249  if(m.first.find("[start]") != std::string::npos)
250  {
251  measurements.emplace("[start]" + name, m.second);
252  }
253  else if(m.first.find("[end]") != std::string::npos)
254  {
255  measurements.emplace("[end]" + name, m.second);
256  }
257  else
258  {
259  ARM_COMPUTE_ERROR("Measurement not handled");
260  }
261  }
262  }
263  else
264  {
265  measurements.emplace(name, kernel.measurements.begin()->second);
266  }
267  }
268 
269  return measurements;
270 }
271 
272 template <bool output_timestamps>
274 {
275  std::string output{""};
276  output += R"("layer_data" : {)";
277  for(auto i_it = _layer_data_map.cbegin(), i_end = _layer_data_map.cend(); i_it != i_end; ++i_it)
278  {
279  output += "\"" + i_it->first + "\" : {";
280  if(i_it->second.size() != 0)
281  {
282  // Print for each entry in layer
283  for(auto entry_it = i_it->second.cbegin(), entry_end = i_it->second.cend(); entry_it != entry_end; ++entry_it)
284  {
285  output += "\"" + entry_it->first + "\" : \"" + entry_it->second + "\"";
286  if(std::next(entry_it) != entry_end)
287  {
288  output += ",";
289  }
290  }
291  }
292  output += "}";
293  if(std::next(i_it) != i_end)
294  {
295  output += ",";
296  }
297  }
298  output += "}";
299  return output;
300 }
301 
302 } // namespace framework
303 } // namespace test
304 } // namespace arm_compute
305 
virtual IScheduler * scheduler()=0
Real scheduler accessor.
std::string instrument_header() const override
Return JSON formatted instrument header string.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Common interface for all kernels implemented in C++.
Definition: ICPPKernel.h:38
std::string to_string(T &&value)
Convert integer and float values to string.
Scheduler interface to run kernels.
Definition: IScheduler.h:41
Instrument::MeasurementsMap measurements
Time it took the kernel to run.
const LayerData & layer_data() const
std::unique_ptr< InstrumentsInfo > instruments_info
Definition: Framework.cpp:50
static void set(std::shared_ptr< IScheduler > scheduler)
Sets the user defined scheduler and makes it the active scheduler.
Definition: Scheduler.cpp:126
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Instrument::MeasurementsMap measurements() const override
Return the latest measurements.
Implementation of an instrument to measure elapsed wall-clock time in milliseconds.
Copyright (c) 2017-2021 Arm Limited.
SchedulerClock(ScaleFactor scale_factor)
Construct a Scheduler timer.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void start() override
Start measuring.
std::string id() const override
Identifier for the instrument.
virtual void restore_scheduler()=0
Restore the original scheduler.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
static TaskExecutor & get()
Task executor accessor.
Definition: Workload.cpp:75
const char * name
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:110
void test_stop() override
End of the test.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
static Type get_type()
Returns the type of the active scheduler.
Definition: Scheduler.cpp:89
Tensor packing service.
Definition: ITensorPack.h:39
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
Definition: Instrument.h:109
void test_start() override
Start of the test.
virtual void intercept_scheduler(std::unique_ptr< IScheduler > interceptor)=0
Intercept the scheduler used by.
Describe a multidimensional execution window.
Definition: Window.h:39
virtual const char * name() const =0
Name of the kernel.
std::function< decltype(execute_task)> execute_function
Function that is responsible for executing tasks.
Definition: Workload.h:63
Instrument creating measurements based on the information returned by clGetEventProfilingInfo for eac...
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94