【问题标题】:How to use CUDA stream in Pytorch?如何在 Pytorch 中使用 CUDA 流?
【发布时间】:2019-03-01 02:35:27
【问题描述】:

我想在 Pytorch 中使用 CUDA 流来并行一些计算,但我不知道该怎么做。 例如,如果有 2 个任务 A 和 B 需要并行化,我想做以下事情:

stream0 = torch.get_stream()
stream1 = torch.get_stream()
with torch.now_stream(stream0):
    // task A
with torch.now_stream(stream1):
    // task B
torch.synchronize()
// get A and B's answer

如何在真正的python代码中实现目标?

【问题讨论】:

    标签: python pytorch


    【解决方案1】:
    s1 = torch.cuda.Stream()
    s2 = torch.cuda.Stream()
    # Initialise cuda tensors here. E.g.:
    A = torch.rand(1000, 1000, device = ‘cuda’)
    B = torch.rand(1000, 1000, device = ‘cuda’)
    # Wait for the above tensors to initialise.
    torch.cuda.synchronize()
    with torch.cuda.stream(s1):
        C = torch.mm(A, A)
    with torch.cuda.stream(s2):
        D = torch.mm(B, B)
    # Wait for C and D to be computed.
    torch.cuda.synchronize()
    # Do stuff with C and D.
    

    【讨论】:

    猜你喜欢
    • 2020-07-07
    • 2022-06-21
    • 2021-06-18
    • 2019-08-14
    • 1970-01-01
    • 2020-05-02
    • 2021-07-03
    • 2021-11-12
    • 2022-01-07
    相关资源
    最近更新 更多