Stream¶
- class torch.mtia.Stream(device, *, priority)¶
一个按顺序执行的队列,用于以先进先出 (FIFO) 顺序异步执行各自的任务。它可以控制或同步其他 Stream 的执行,或阻止当前主机线程以确保正确的任务排序。
有关适用于所有设备的精确语义的详细信息,请参阅 CUDA 行为的深入描述,请访问 CUDA 语义。
- 参数
device (
torch.device
, optional) – Stream 的所需设备。如果未给出,将使用当前的 加速器 类型。priority (int, 可选) – 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)