|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "device.hpp" |
| 4 | +#include "infinirt.h" |
| 5 | +#include <memory> |
| 6 | +#include <stdexcept> |
| 7 | + |
| 8 | +namespace infinicore { |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief A device event for timing operations and synchronization across devices. |
| 12 | + * |
| 13 | + * Similar to torch.cuda.Event, this class provides functionality to: |
| 14 | + * - Record events on specific device streams |
| 15 | + * - Synchronize with events |
| 16 | + * - Measure elapsed time between events |
| 17 | + * - Query event completion status |
| 18 | + * - Make streams wait for events |
| 19 | + */ |
| 20 | +class DeviceEvent { |
| 21 | +private: |
| 22 | + infinirtEvent_t event_; // Underlying event handle |
| 23 | + Device device_; // Device where this event was created |
| 24 | + bool is_recorded_; // Whether the event has been recorded |
| 25 | + |
| 26 | +public: |
| 27 | + /** |
| 28 | + * @brief Construct a new DeviceEvent on the current device. |
| 29 | + */ |
| 30 | + DeviceEvent(); |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Construct a new DeviceEvent on the current device with specific flags. |
| 34 | + * @param flags Event creation flags (e.g., for timing, blocking sync) |
| 35 | + */ |
| 36 | + explicit DeviceEvent(uint32_t flags); |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Construct a new DeviceEvent on a specific device. |
| 40 | + * @param device Target device for this event |
| 41 | + */ |
| 42 | + explicit DeviceEvent(Device device); |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Construct a new DeviceEvent on a specific device with flags. |
| 46 | + * @param device Target device for this event |
| 47 | + * @param flags Event creation flags |
| 48 | + */ |
| 49 | + DeviceEvent(Device device, uint32_t flags); |
| 50 | + |
| 51 | + // Disallow copying |
| 52 | + DeviceEvent(const DeviceEvent &) = delete; |
| 53 | + DeviceEvent &operator=(const DeviceEvent &) = delete; |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief Move constructor. |
| 57 | + */ |
| 58 | + DeviceEvent(DeviceEvent &&other) noexcept; |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Move assignment operator. |
| 62 | + */ |
| 63 | + DeviceEvent &operator=(DeviceEvent &&other) noexcept; |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Destroy the DeviceEvent and release underlying resources. |
| 67 | + */ |
| 68 | + ~DeviceEvent(); |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Record the event on the current stream of its device. |
| 72 | + */ |
| 73 | + void record(); |
| 74 | + |
| 75 | + /** |
| 76 | + * @brief Record the event on a specific stream. |
| 77 | + * @param stream Stream to record the event on |
| 78 | + */ |
| 79 | + void record(infinirtStream_t stream); |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Wait for the event to complete (blocking). |
| 83 | + */ |
| 84 | + void synchronize(); |
| 85 | + |
| 86 | + /** |
| 87 | + * @brief Check if the event has been completed. |
| 88 | + * @return true if completed, false otherwise |
| 89 | + */ |
| 90 | + bool query() const; |
| 91 | + |
| 92 | + /** |
| 93 | + * @brief Calculate elapsed time between this event and another event (in milliseconds). |
| 94 | + * @param other The other event to compare with |
| 95 | + * @return Elapsed time in milliseconds |
| 96 | + * @throws std::runtime_error if events are on different devices or not recorded |
| 97 | + */ |
| 98 | + float elapsed_time(const DeviceEvent &other) const; |
| 99 | + |
| 100 | + /** |
| 101 | + * @brief Make a stream wait for this event to complete. |
| 102 | + * @param stream Stream to make wait for this event (nullptr for current stream) |
| 103 | + */ |
| 104 | + void wait(infinirtStream_t stream = nullptr) const; |
| 105 | + |
| 106 | + /** |
| 107 | + * @brief Get the device where this event was created. |
| 108 | + * @return Device associated with this event |
| 109 | + */ |
| 110 | + Device device() const { return device_; } |
| 111 | + |
| 112 | + /** |
| 113 | + * @brief Get the underlying event handle. |
| 114 | + * @return Raw event handle |
| 115 | + */ |
| 116 | + infinirtEvent_t get() const { return event_; } |
| 117 | + |
| 118 | + /** |
| 119 | + * @brief Check if the event has been recorded. |
| 120 | + * @return true if recorded, false otherwise |
| 121 | + */ |
| 122 | + bool is_recorded() const { return is_recorded_; } |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace infinicore |
0 commit comments