|
Celeritas 0.7-develop+db9a1d4
|
Asynchronously accumulate the device time of labeled intervals on a stream. More...
#include <StreamTimer.hh>
Public Types | |
Type aliases | |
| using | SpanDbl = Span< double > |
Public Member Functions | |
| StreamTimer (Device const &d, StreamId sid) | |
| Construct with an active device and one of its streams. | |
| void | start () |
| Begin a new interval at the current point in the stream. | |
| void | stop (TimerId id) |
| End the interval since the last marker and attribute it to an ID. | |
| size_type | num_pending () const |
| Number of stop markers whose time has not been flushed. | |
| void | flush (SpanDbl accum) |
| Block until complete, add pending elapsed times, and clear them. | |
| void | clear () |
| Discard pending intervals. | |
Asynchronously accumulate the device time of labeled intervals on a stream.
The timer records a chain of timing-enabled DeviceEvent markers on a single stream without synchronizing. Each stop marker ends an interval that began at the previous marker and attributes the elapsed device time between them to a TimerId (e.g., corresponding to an action ID). A start marker begins a new interval without attributing time, which excludes any work enqueued since the previous marker. Consecutive stop calls share markers, so timing N back-to-back intervals takes N + 1 events.
Elapsed times are only read in flush , which blocks until the last recorded marker is complete, adds the pending times, and clears them. The timer never reads them on its own: the owner must call flush after the last interval (e.g., when the accumulated times are requested at the end of a run). Intervals that are pending when the timer is cleared or destroyed are discarded.
The backing DeviceEvent objects have the lifetime of the timer and are reused after flush or clear , since they may be expensive to create and destroy. A single timer should therefore be used across sequential iterations (e.g., steps) so that recording does not allocate in a steady state.
The measured time of an interval is the elapsed device time between the two markers: it includes kernel execution and any device idle time between them (e.g., if the host was slow to launch the next kernel) but excludes host time that overlaps with earlier device work.
All markers are recorded on the execution stream given at construction (Device::stream ), so intervals only measure work ordered on that stream. Work on another stream (e.g., an asynchronous copy on the corresponding Device::memcpy_stream ) is included in an interval only if the timed stream waits on it (e.g., with Stream::wait ) before the interval's stop marker. The stream is looked up by ID at each marker rather than stored, since the device's streams may be recreated while the timer is alive.
The device must be active and its streams created to construct a timer.
Construct with an active device and one of its streams.
All markers are recorded on d.stream(sid) .
| void celeritas::StreamTimer::clear | ( | ) |
Discard pending intervals.
The event pool is kept for reuse.
Block until complete, add pending elapsed times, and clear them.
The time of each pending interval is added to the element of accum indexed by its timer ID, which must be large enough for every pending ID. This synchronizes with the last recorded marker only. Flushing a timer with no pending intervals (e.g., at the beginning of the first step) just clears it.
The timer ID of each marker recorded since the last flush is stored in ids_ (a null ID for a start marker), and the corresponding event is the same index of events_ . The time between events i - 1 and i is attributed to ids_[i] unless that is null. The event pool may be longer than ids_ if an earlier flushed sequence had more markers, so the most recent marker is events_[ids_.size() - 1] rather than events_.back() . For example, with action IDs a , b , c :
start ; stop(a) ; stop(b) : start ; stop(a) ; (untimed work) start ; stop(c) ; stop(a) : start ; stop(b) : | void celeritas::StreamTimer::start | ( | ) |
Begin a new interval at the current point in the stream.
Any work enqueued on the stream between the previous marker and this one is not attributed to any timer ID.
End the interval since the last marker and attribute it to an ID.
start must have been called since the last flush or clear .