Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeLaunchTimer.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <cstdint>
9#include <cstdlib>
10#include <iostream>
11#include <memory>
12#include <optional>
13
14namespace mneme {
15
16// Measures the GPU durations of kernel launches. Durations may arrive after
17// later launches, so collect() and drain() add them to the record database.
18template <DeviceVendors VendorTypes> class LaunchTimer {
19public:
22
23 // What end() needs to know about how begin() started timing the launch.
28
29 virtual ~LaunchTimer() = default;
30
31 // Adds the durations that have arrived.
32 virtual void collect(RecordDatabase &DB) = 0;
33 // Returns nothing when the launch on Stream is not timed.
34 virtual std::optional<StartedTiming> begin(const LaunchHandle &Launch,
35 DeviceStream_t Stream) = 0;
36 virtual void end(const StartedTiming &Started,
37 DeviceError_t LaunchResult) = 0;
38 // Waits for the durations of all launches and adds them.
39 virtual void drain(RecordDatabase &DB) = 0;
40};
41
42// The vendor profiler's callbacks reach the process-wide KernelTimer, so this
43// only forwards to it.
44template <DeviceVendors VendorTypes>
45class ProfilerLaunchTimer final : public LaunchTimer<VendorTypes> {
46 using DevTraits = DeviceTraits<VendorTypes>;
50
51 KernelTimer &Profiler;
52
53public:
54 explicit ProfilerLaunchTimer(KernelTimer &Profiler) : Profiler(Profiler) {}
55
56 void collect(RecordDatabase &DB) override { Profiler.collect(DB); }
57
58 std::optional<StartedTiming> begin(const LaunchHandle &Launch,
59 DeviceStream_t Stream) override {
60 auto [CaptureQueryResult, Capturing] =
61 DevTraits::deviceStreamIsCapturing(Stream);
62 // A captured launch dispatches nothing now, and its graph's later
63 // dispatches carry no token, so it would never get a duration.
64 if (Capturing)
65 return std::nullopt;
66 std::optional<uint64_t> Token = Profiler.begin(Launch);
67 if (!Token)
68 return std::nullopt;
69 return StartedTiming{*Token, CaptureQueryResult};
70 }
71
72 // The capture query fails only where the launch should fail the same way,
73 // such as on the legacy stream while another stream captures, so a launch
74 // that succeeds anyway may have been captured instead of dispatched.
75 void end(const StartedTiming &Started, DeviceError_t LaunchResult) override {
76 if (LaunchResult != DevTraits::DeviceSuccess) {
77 Profiler.abandon(Started.Token);
78 return;
79 }
80 if (Started.CaptureQueryResult != DevTraits::DeviceSuccess) {
81 std::cerr << "[mneme] Kernel timing cannot tell whether a launch was "
82 "captured into a graph: "
83 << *DevTraits::DeviceErrorCheck(Started.CaptureQueryResult)
84 << "\n";
85 std::abort();
86 }
87 Profiler.end(Started.Token);
88 }
89
90 // The profiler reports a dispatch only after it completes.
91 void drain(RecordDatabase &DB) override {
92 (void)DevTraits::DeviceSynchronize();
93 Profiler.drain(DB);
94 }
95};
96
97// Returns nullptr when timing is off.
98template <DeviceVendors VendorTypes>
99std::unique_ptr<LaunchTimer<VendorTypes>>
101 switch (Type) {
103 return nullptr;
105 return std::make_unique<ProfilerLaunchTimer<VendorTypes>>(
107 }
108 return nullptr;
109}
110
111} // namespace mneme
Definition MnemeKernelTimer.hpp:21
static KernelTimer & get()
Definition MnemeKernelTimer.cpp:68
Definition MnemeLaunchTimer.hpp:18
virtual void drain(RecordDatabase &DB)=0
virtual ~LaunchTimer()=default
typename DeviceTraits< VendorTypes >::DeviceStream_t DeviceStream_t
Definition MnemeLaunchTimer.hpp:21
virtual void collect(RecordDatabase &DB)=0
typename DeviceTraits< VendorTypes >::DeviceError_t DeviceError_t
Definition MnemeLaunchTimer.hpp:20
virtual std::optional< StartedTiming > begin(const LaunchHandle &Launch, DeviceStream_t Stream)=0
virtual void end(const StartedTiming &Started, DeviceError_t LaunchResult)=0
void end(const StartedTiming &Started, DeviceError_t LaunchResult) override
Definition MnemeLaunchTimer.hpp:75
void drain(RecordDatabase &DB) override
Definition MnemeLaunchTimer.hpp:91
std::optional< StartedTiming > begin(const LaunchHandle &Launch, DeviceStream_t Stream) override
Definition MnemeLaunchTimer.hpp:58
void collect(RecordDatabase &DB) override
Definition MnemeLaunchTimer.hpp:56
ProfilerLaunchTimer(KernelTimer &Profiler)
Definition MnemeLaunchTimer.hpp:54
Definition MnemeSnapshot.hpp:479
Definition MnemeAnnotation.cpp:3
KernelTimingType
Definition MnemeConfig.hpp:26
@ Profiler
Definition MnemeConfig.hpp:26
@ Off
Definition MnemeConfig.hpp:26
std::unique_ptr< LaunchTimer< VendorTypes > > createLaunchTimer(KernelTimingType Type)
Definition MnemeLaunchTimer.hpp:100
Definition DeviceTraits.hpp:73
Definition MnemeSnapshot.hpp:50
Definition MnemeLaunchTimer.hpp:24
uint64_t Token
Definition MnemeLaunchTimer.hpp:25
DeviceError_t CaptureQueryResult
Definition MnemeLaunchTimer.hpp:26