事件¶
- class torch.mtia.Event(device, *, enable_timing)¶
查詢和記錄 Stream 狀態,以識別或控制 Stream 之間的相依性,並測量時間。
- 參數
device (
torch.device
, optional) – Event 的所需裝置。如果未提供,將使用目前的 加速器 類型。enable_timing (bool, optional) – 指示事件是否應測量時間 (預設值:
False
)。
- 傳回
torch.Event 物件。
- 傳回類型
範例
>>> e_cuda = torch.Event(device='cuda')
- elapsed_time(end_event) float ¶
傳回這個事件和
end_event
各自透過torch.Stream.record_event()
記錄的時間之間的毫秒差。- 參數
end_event (
torch.Event
) – 已記錄的結束事件。- 傳回
開始和結束事件之間的時間差,以毫秒為單位。
- 傳回類型
範例
>>> s_cuda = torch.Stream(device='cuda') >>> e1_cuda = s_cuda.record_event() >>> e2_cuda = s_cuda.record_event() >>> ms = e1_cuda.elapsed_time(e2_cuda)
- query() bool ¶
檢查記錄此事件的 stream 是否已超過記錄事件的時間點。如果 Event 沒有被記錄,則始終傳回
True
。- 傳回
一個布林值,表示事件目前捕獲的所有工作是否已完成。
- 傳回類型
範例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.query() True
- record(stream) None ¶
在給定的 stream 中記錄事件。stream 的裝置必須與事件的裝置相符。此函數等效於
stream.record_event(self)
。- 參數
stream (
torch.Stream
, optional) – 要記錄的 stream (可選)。given (如果沒有) –
used. (將使用目前的 stream) –
範例
>>> e_cuda = torch.Event(device='cuda') >>> e_cuda.record()
- synchronize() None ¶
等待事件完成。這會阻止 CPU 執行緒繼續執行,直到事件完成。
範例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.synchronize()
- wait(stream) None ¶
使提交到給定 stream 的所有未來工作等待此事件。
- 參數
stream (
torch.Stream
, optional) – 要同步的 stream (可選)。given (如果沒有) –
used. (將使用目前的 stream) –
範例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record() >>> e_cuda.wait(s2)