Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CUDA stream in Pytorch?

Tags:

python

pytorch

I wanna use CUDA stream in Pytorch to parallel some computations, but I don't know how to do it. For instance, if there's 2 tasks, A and B, need to be parallelized, I wanna do the following things:

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

How can I achieve the goal in real python code?

like image 724
gasoon Avatar asked Sep 25 '18 12:09

gasoon


People also ask

What is a torch CUDA stream?

A CUDA stream is a linear sequence of execution that belongs to a specific device, independent from other streams. See CUDA semantics for details. device (torch. device or int, optional) – a device on which to allocate the stream. If device is None (default) or a negative integer, this will use the current device.


1 Answers

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.
like image 81
Tomas Avatar answered Sep 21 '22 10:09

Tomas