tvm
Public Member Functions | Static Public Member Functions | List of all members
tvm::runtime::Timer Class Reference

Timer for a specific device. More...

#include <profiling.h>

Inheritance diagram for tvm::runtime::Timer:
Collaboration diagram for tvm::runtime::Timer:

Public Member Functions

 TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE (Timer, ObjectRef, TimerNode)
 

Static Public Member Functions

static Timer Start (Device dev)
 Get a device specific timer. More...
 

Detailed Description

Timer for a specific device.

This is a managed reference to a TimerNode.

See also
TimerNode

Member Function Documentation

◆ Start()

static Timer tvm::runtime::Timer::Start ( Device  dev)
static

Get a device specific timer.

Parameters
devThe device to time.
Returns
A Timer that has already been started.

Use this function to time runtime of arbitrary regions of code on a specific device. The code that you want to time should be running on the device otherwise the timer will not return correct results. This is a lower level interface than TimeEvaluator and only runs the timed code once (TimeEvaluator runs the code multiple times).

A default timer is used if a device specific one does not exist. This timer performs synchronization between the device and CPU, which can lead to overhead in the reported results.

Example usage:

Timer t = Timer::Start(Device::cpu());
my_long_running_function();
t->Stop();
... // some more computation
int64_t nanosecs = t->SyncAndGetElapsedNanos() // elapsed time in nanoseconds
static Timer Start(Device dev)
Get a device specific timer.

To add a new device-specific timer, register a new function "profiler.timer.my_device" (where my_device is the DeviceName of your device). This function should accept a Device and return a new Timer that has already been started.

For example, this is how the CPU timer is implemented:

class CPUTimerNode : public TimerNode {
public:
virtual void Start() { start_ = std::chrono::high_resolution_clock::now(); }
virtual void Stop() { duration_ = std::chrono::high_resolution_clock::now() - start_; }
virtual int64_t SyncAndGetElapsedNanos() { return duration_.count(); }
virtual ~CPUTimerNode() {}
static constexpr const char* _type_key = "runtime.CPUTimerNode";
TVM_FFI_DECLARE_OBJECT_INFO_FINAL(CPUTimerNode, TimerNode);
private:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::duration<int64_t, std::nano> duration_;
};
TVM_FFI_STATIC_INIT_BLOCK() {
namespace refl = tvm::ffi::reflection;
refl::GlobalDef().def("profiling.timer.cpu", [](Device dev) {
return Timer(ffi::make_object<CPUTimerNode>());
});
}
Definition: repr_printer.h:91
DLDevice Device
Definition: device_api.h:42

◆ TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE()

tvm::runtime::Timer::TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE ( Timer  ,
ObjectRef  ,
TimerNode   
)

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