事件
- classtorch.mtia.Event(device, *, enable_timing)
-
查询并记录 Stream 状态,以识别和控制跨 Stream 的依赖关系,并测量时间。
- 参数
-
-
device (
torch.device
, 可选) – 事件所期望的设备。如果没有指定,则使用当前的 加速器 类型。 -
enable_timing (bool, 可选) – 是否启用事件的时间测量(默认值:
False
)。
-
- 返回值
-
一个 torch.Event 对象。
- 返回类型
示例:
>>> e_cuda = torch.Event(device='cuda')
- elapsed_time(end_event) → float
-
返回从这个事件到通过
torch.Stream.record_event()
记录的end_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
-
检查记录该事件的流是否已超出事件记录时的位置。如果没有记录该事件,则始终返回
True
。- 返回值
-
一个布尔值,表示事件中所有已捕获的工作是否已完成。
- 返回类型
示例:
>>> s_cuda = torch.Stream(device='cuda') >>> e_cuda = s_cuda.record_event() >>> e_cuda.query() True
- record(stream) → None 该函数记录一个流并返回 None。
-
在此流中记录事件。流的设备需与事件的设备一致。此函数等同于
stream.record_event(self)
。- 参数
-
-
stream (
torch.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
-
让所有提交到该流的 future 工作都等待此事件。
- 参数
-
-
stream (
torch.Stream
, 可选) – 用于同步的 torch 流。 -
给定 (如果未提供) –
-
已使用。 (当前流将会被)–
-
示例:
>>> s1_cuda = torch.Stream(device='cuda') >>> s2_cuda = torch.Stream(device='cuda') >>> e_cuda = s1_cuda.record() >>> e_cuda.wait(s2)