Compute Library
 22.11
SchedulerClock< output_timestamps > Class Template Reference

Instrument creating measurements based on the information returned by clGetEventProfilingInfo for each OpenCL kernel executed. More...

#include <SchedulerTimer.h>

Collaboration diagram for SchedulerClock< output_timestamps >:
[legend]

Data Structures

struct  kernel_info
 Kernel information. More...
 

Public Types

using LayerData = std::map< std::string, std::string >
 
- Public Types inherited from Instrument
using MeasurementsMap = std::map< std::string, Measurement >
 Map of measurements. More...
 

Public Member Functions

 SchedulerClock (ScaleFactor scale_factor)
 Construct a Scheduler timer. More...
 
 SchedulerClock (const SchedulerClock &)=delete
 Prevent instances of this class from being copy constructed. More...
 
SchedulerClockoperator= (const SchedulerClock &)=delete
 Prevent instances of this class from being copied. More...
 
SchedulerClockoperator= (SchedulerClock &&)=default
 Use the default move assignment operator. More...
 
 SchedulerClock (SchedulerClock &&)=default
 Use the default move constructor. More...
 
 ~SchedulerClock ()=default
 Use the default destructor. More...
 
std::string id () const override
 Identifier for the instrument. More...
 
void test_start () override
 Start of the test. More...
 
void start () override
 Start measuring. More...
 
void test_stop () override
 End of the test. More...
 
Instrument::MeasurementsMap measurements () const override
 Return the latest measurements. More...
 
std::string instrument_header () const override
 Return JSON formatted instrument header string. More...
 
- Public Member Functions inherited from Instrument
 Instrument ()=default
 Default constructor. More...
 
 Instrument (const Instrument &)=default
 Allow instances of this class to be copy constructed. More...
 
 Instrument (Instrument &&)=default
 Allow instances of this class to be move constructed. More...
 
Instrumentoperator= (const Instrument &)=default
 Allow instances of this class to be copied. More...
 
Instrumentoperator= (Instrument &&)=default
 Allow instances of this class to be moved. More...
 
virtual ~Instrument ()=default
 Default destructor. More...
 
virtual void stop ()
 Stop measuring. More...
 
virtual MeasurementsMap test_measurements () const
 Return the latest test measurements. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Instrument
template<typename T , ScaleFactor scale>
static std::unique_ptr< Instrumentmake_instrument ()
 Helper function to create an instrument of the given type. More...
 

Detailed Description

template<bool output_timestamps>
class arm_compute::test::framework::SchedulerClock< output_timestamps >

Instrument creating measurements based on the information returned by clGetEventProfilingInfo for each OpenCL kernel executed.

Definition at line 63 of file SchedulerTimer.h.

Member Typedef Documentation

◆ LayerData

using LayerData = std::map<std::string, std::string>

Definition at line 66 of file SchedulerTimer.h.

Constructor & Destructor Documentation

◆ SchedulerClock() [1/3]

SchedulerClock ( ScaleFactor  scale_factor)

Construct a Scheduler timer.

Parameters
[in]scale_factorMeasurement scale factor.

Definition at line 139 of file SchedulerTimer.cpp.

References arm_compute::test::framework::instruments_info.

140  : _kernels(),
141  _layer_data_map(),
142  _real_scheduler(nullptr),
143  _real_scheduler_type(),
144 #ifdef ARM_COMPUTE_GRAPH_ENABLED
145  _real_graph_function(nullptr),
146 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
147  _scale_factor(scale_factor),
148  _interceptor(nullptr),
149  _scheduler_users()
150 {
151  if(instruments_info != nullptr)
152  {
153  _scheduler_users = instruments_info->_scheduler_users;
154  }
155 }
std::unique_ptr< InstrumentsInfo > instruments_info
Definition: Framework.cpp:50

◆ SchedulerClock() [2/3]

SchedulerClock ( const SchedulerClock< output_timestamps > &  )
delete

Prevent instances of this class from being copy constructed.

◆ SchedulerClock() [3/3]

SchedulerClock ( SchedulerClock< output_timestamps > &&  )
default

Use the default move constructor.

◆ ~SchedulerClock()

~SchedulerClock ( )
default

Use the default destructor.

Member Function Documentation

◆ id()

std::string id ( ) const
overridevirtual

Identifier for the instrument.

Implements Instrument.

Definition at line 40 of file SchedulerTimer.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_UNUSED, arm_compute::test::validation::info, SchedulerClock< output_timestamps >::kernel_info::measurements, SchedulerClock< output_timestamps >::kernel_info::name, ICPPKernel::name(), and SchedulerClock< output_timestamps >::kernel_info::prefix.

41 {
42  if(output_timestamps)
43  {
44  return "SchedulerTimestamps";
45  }
46  else
47  {
48  return "SchedulerTimer";
49  }
50 }

◆ instrument_header()

std::string instrument_header ( ) const
overridevirtual

Return JSON formatted instrument header string.

Returns
JSON formatted string

Reimplemented from Instrument.

Definition at line 287 of file SchedulerTimer.cpp.

288 {
289  std::string output{ "" };
290  output += R"("layer_data" : {)";
291  for(auto i_it = _layer_data_map.cbegin(), i_end = _layer_data_map.cend(); i_it != i_end; ++i_it)
292  {
293  output += "\"" + i_it->first + "\" : {";
294  if(i_it->second.size() != 0)
295  {
296  // Print for each entry in layer
297  for(auto entry_it = i_it->second.cbegin(), entry_end = i_it->second.cend(); entry_it != entry_end; ++entry_it)
298  {
299  output += "\"" + entry_it->first + "\" : \"" + entry_it->second + "\"";
300  if(std::next(entry_it) != entry_end)
301  {
302  output += ",";
303  }
304  }
305  }
306  output += "}";
307  if(std::next(i_it) != i_end)
308  {
309  output += ",";
310  }
311  }
312  output += "}";
313  return output;
314 }

◆ measurements()

Instrument::MeasurementsMap measurements ( ) const
overridevirtual

Return the latest measurements.

Returns
the latest measurements.

Reimplemented from Instrument.

Definition at line 251 of file SchedulerTimer.cpp.

References ARM_COMPUTE_ERROR, ARM_COMPUTE_ERROR_ON, arm_compute::test::validation::m, name, and arm_compute::support::cpp11::to_string().

252 {
254  unsigned int kernel_number = 0;
255  for(auto kernel : _kernels)
256  {
257  std::string name = kernel.prefix + kernel.name + " #" + support::cpp11::to_string(kernel_number++);
258  if(output_timestamps)
259  {
260  ARM_COMPUTE_ERROR_ON(kernel.measurements.size() != 2);
261  for(auto const &m : kernel.measurements)
262  {
263  if(m.first.find("[start]") != std::string::npos)
264  {
265  measurements.emplace("[start]" + name, m.second);
266  }
267  else if(m.first.find("[end]") != std::string::npos)
268  {
269  measurements.emplace("[end]" + name, m.second);
270  }
271  else
272  {
273  ARM_COMPUTE_ERROR("Measurement not handled");
274  }
275  }
276  }
277  else
278  {
279  measurements.emplace(name, kernel.measurements.begin()->second);
280  }
281  }
282 
283  return measurements;
284 }
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
std::string to_string(T &&value)
Convert integer and float values to string.
#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.
const char * name
std::map< std::string, Measurement > MeasurementsMap
Map of measurements.
Definition: Instrument.h:109

◆ operator=() [1/2]

SchedulerClock& operator= ( const SchedulerClock< output_timestamps > &  )
delete

Prevent instances of this class from being copied.

◆ operator=() [2/2]

SchedulerClock& operator= ( SchedulerClock< output_timestamps > &&  )
default

Use the default move assignment operator.

◆ start()

void start ( )
overridevirtual

Start measuring.

Called just before the run of the test starts

Reimplemented from Instrument.

Definition at line 222 of file SchedulerTimer.cpp.

223 {
224  _kernels.clear();
225 }

◆ test_start()

void test_start ( )
overridevirtual

Start of the test.

Called before the test set up starts

Reimplemented from Instrument.

Definition at line 158 of file SchedulerTimer.cpp.

References ARM_COMPUTE_ERROR_ON, Scheduler::CUSTOM, arm_compute::mlgo::parser::end(), TaskExecutor::execute_function, arm_compute::utility::for_each(), Scheduler::get(), TaskExecutor::get(), Scheduler::get_type(), ISchedulerUser::intercept_scheduler(), DataLayerVisitor::layer_data(), ISchedulerUser::scheduler(), and Scheduler::set().

159 {
160 #ifdef ARM_COMPUTE_GRAPH_ENABLED
161  // Start intercepting tasks:
162  ARM_COMPUTE_ERROR_ON(_real_graph_function != nullptr);
163  _real_graph_function = graph::TaskExecutor::get().execute_function;
164  auto task_interceptor = [this](graph::ExecutionTask & task)
165  {
166  Interceptor<output_timestamps> *scheduler = nullptr;
167  if(dynamic_cast<Interceptor<output_timestamps> *>(this->_interceptor.get()) != nullptr)
168  {
169  scheduler = arm_compute::utils::cast::polymorphic_downcast<Interceptor<output_timestamps> *>(_interceptor.get());
170  if(task.node != nullptr && !task.node->name().empty())
171  {
172  scheduler->set_prefix(task.node->name() + "/");
173 
174  if(_layer_data_map.find(task.node->name()) == _layer_data_map.end())
175  {
177  task.node->accept(dlv);
178  _layer_data_map[task.node->name()] = dlv.layer_data();
179  }
180  }
181  else
182  {
183  scheduler->set_prefix("");
184  }
185  }
186 
187  this->_real_graph_function(task);
188 
189  if(scheduler != nullptr)
190  {
191  scheduler->set_prefix("");
192  }
193  };
194 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
195 
196  ARM_COMPUTE_ERROR_ON(_real_scheduler != nullptr);
197  _real_scheduler_type = Scheduler::get_type();
198  //Note: We can't currently replace a custom scheduler
199  if(_real_scheduler_type != Scheduler::Type::CUSTOM)
200  {
201  _real_scheduler = &Scheduler::get();
202  _interceptor = std::make_shared<Interceptor<output_timestamps>>(_kernels, _layer_data_map, *_real_scheduler, _scale_factor);
203  Scheduler::set(std::static_pointer_cast<IScheduler>(_interceptor));
204 #ifdef ARM_COMPUTE_GRAPH_ENABLED
205  graph::TaskExecutor::get().execute_function = task_interceptor;
206 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
207 
208  // Create an interceptor for each scheduler
209  // TODO(COMPID-2638) : Allow multiple schedulers, now it assumes the same scheduler is used.
210  std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
211  [&](ISchedulerUser * user)
212  {
213  if(user != nullptr && user->scheduler() != nullptr)
214  {
215  user->intercept_scheduler(std::make_unique<Interceptor<output_timestamps>>(_kernels, _layer_data_map, *user->scheduler(), _scale_factor));
216  }
217  });
218  }
219 }
const LayerData & layer_data() const
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
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
static TaskExecutor & get()
Task executor accessor.
Definition: Workload.cpp:75
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:110
static Type get_type()
Returns the type of the active scheduler.
Definition: Scheduler.cpp:89
std::function< decltype(execute_task)> execute_function
Function that is responsible for executing tasks.
Definition: Workload.h:63
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94

◆ test_stop()

void test_stop ( )
overridevirtual

End of the test.

Called after the test teardown ended

Reimplemented from Instrument.

Definition at line 228 of file SchedulerTimer.cpp.

References arm_compute::mlgo::parser::end(), TaskExecutor::execute_function, arm_compute::utility::for_each(), TaskExecutor::get(), ISchedulerUser::restore_scheduler(), and Scheduler::set().

229 {
230  // Restore real scheduler
231  Scheduler::set(_real_scheduler_type);
232  _real_scheduler = nullptr;
233  _interceptor = nullptr;
234 #ifdef ARM_COMPUTE_GRAPH_ENABLED
235  graph::TaskExecutor::get().execute_function = _real_graph_function;
236  _real_graph_function = nullptr;
237 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
238 
239  // Restore schedulers
240  std::for_each(std::begin(_scheduler_users), std::end(_scheduler_users),
241  [&](ISchedulerUser * user)
242  {
243  if(user != nullptr)
244  {
245  user->restore_scheduler();
246  }
247  });
248 }
static void set(std::shared_ptr< IScheduler > scheduler)
Sets the user defined scheduler and makes it the active scheduler.
Definition: Scheduler.cpp:126
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
static TaskExecutor & get()
Task executor accessor.
Definition: Workload.cpp:75
void for_each(F &&)
Base case of for_each.
Definition: Utility.h:110
std::function< decltype(execute_task)> execute_function
Function that is responsible for executing tasks.
Definition: Workload.h:63

The documentation for this class was generated from the following files: