Stream¶
- class torch.Stream(device, *, priority)¶
一個依序佇列,以先進先出 (FIFO) 的順序非同步執行各自的任務。它可以控制或同步其他 Stream 的執行,或封鎖目前的主機線程以確保正確的任務排序。
請參閱 CUDA 語義 的深入說明,以瞭解適用於所有裝置的確切語義的詳細資訊。
- 參數
device (
torch.device
, optional) – Stream 期望使用的裝置。如果未指定,則將使用目前的 加速器 類型。priority (int, optional) – Stream 的優先順序,應為 0 或負數,其中負數表示更高的優先順序。 預設情況下,Stream 的優先順序為 0。
- 返回值
一個 torch.Stream 物件。
- 返回值類型
範例
>>> s_cuda = torch.Stream(device='cuda')
- query() bool ¶
檢查所有提交的工作是否已完成。
- 返回值
一個布林值,指示此 Stream 中的所有核心是否已完成。
- 返回值類型
範例
>>> s_cuda = torch.Stream(device='cuda') >>> s_cuda.query() True
- record_event(event) Event ¶
記錄一個事件。將其排隊到 Stream 中,以便從 FIFO 佇列中的目前點進一步同步。
- 參數
event (
torch.Event
, optional) – 要記錄的事件。如果未給定,將分配一個新的事件。- 返回值
已記錄的事件。
- 返回值類型
範例
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event()
- synchronize() None ¶
等待此 Stream 中的所有核心完成。
範例
>>> s_cuda = torch.Stream(device='cuda') >>> s_cuda.synchronize()
- wait_event(event) None ¶
使所有提交給 Stream 的未來工作等待一個事件。
- 參數
event (
torch.Event
) – 要等待的事件。
範例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record_event() >>> s2_cuda.wait_event(e_cuda)
- wait_stream(stream) None ¶
與另一個 Stream 同步。所有提交到此 Stream 的未來工作都將等待,直到所有已提交到給定 Stream 的核心完成。
- 參數
stream (
torch.Stream
) – 要同步的 Stream。
範例
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> s2_cuda.wait_stream(s1_cuda)