Celeritas 0.7-develop+db9a1d4
Loading...
Searching...
No Matches
Public Member Functions | List of all members
celeritas::StreamTimer Class Reference

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.
 

Detailed Description

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.

Example
StreamTimer timer{device(), stream_id};
std::vector<double> accum(num_actions);
for (auto step : range(num_steps))
{
timer.flush(make_span(accum)); // Previous step is already complete
timer.start();
for (auto const& action : actions)
{
action.launch_async(device().stream(stream_id));
timer.stop(TimerId{action.id()});
}
wait_for_step_results();
}
timer.flush(make_span(accum)); // Final step
Device const & device()
Get the shared default device.
Definition Device.cc:345
constexpr Range< T > range(T begin, T end)
Return a range over fixed beginning and end values.
Definition Range.hh:197
constexpr Span< T, N > make_span(Array< T, N > &x)
Get a mutable fixed-size view to an array.
Definition Span.hh:348
Type-safe "optional" index for accessing an array or collection of data.
Definition OpaqueId.hh:125
Asynchronously accumulate the device time of labeled intervals on a stream.
Definition StreamTimer.hh:89

The device must be active and its streams created to construct a timer.

Todo:
Accumulate into a Quantity -like time type rather than raw seconds (see query_elapsed_time ).

Constructor & Destructor Documentation

◆ StreamTimer()

celeritas::StreamTimer::StreamTimer ( Device const &  d,
StreamId  sid 
)

Construct with an active device and one of its streams.

All markers are recorded on d.stream(sid) .

Member Function Documentation

◆ clear()

void celeritas::StreamTimer::clear ( )

Discard pending intervals.

The event pool is kept for reuse.

◆ flush()

void celeritas::StreamTimer::flush ( SpanDbl  accum)

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) :
    ids_ = {null, a, b}
    events_ = {e0, e1, e2}
    a += e1 - e0; b += e2 - e1
  • start ; stop(a) ; (untimed work) start ; stop(c) ; stop(a) :
    ids_ = {null, a, null, c, a}
    events_ = {e0, e1, e2, e3, e4}
    a += e1 - e0; (e2 - e1 is discarded); c += e3 - e2; a += e4 - e3
  • After flushing the previous example, start ; stop(b) :
    ids_ = {null, b}
    events_ = {e0, e1, e2, e3, e4} // e2..e4 are idle until reused
    b += e1 - e0

◆ start()

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.

◆ stop()

void celeritas::StreamTimer::stop ( TimerId  id)

End the interval since the last marker and attribute it to an ID.

Precondition
start must have been called since the last flush or clear .

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