参考:https://pytorch.org/docs/stable/notes/cuda.html
    torch.cuda.device()修改默认cuda设备,修改之后不管是.cuda()还是.to(device=cuda)都会在默认cuda设备上生成张量

    1. cuda = torch.device('cuda') # Default CUDA device
    2. cuda0 = torch.device('cuda:0')
    3. cuda2 = torch.device('cuda:2') # GPU 2 (these are 0-indexed)
    4. x = torch.tensor([1., 2.], device=cuda0)
    5. # x.device is device(type='cuda', index=0)
    6. y = torch.tensor([1., 2.]).cuda()
    7. # y.device is device(type='cuda', index=0)
    8. with torch.cuda.device(1):
    9. # allocates a tensor on GPU 1
    10. a = torch.tensor([1., 2.], device=cuda)
    11. # transfers a tensor from CPU to GPU 1
    12. b = torch.tensor([1., 2.]).cuda()
    13. # a.device and b.device are device(type='cuda', index=1)
    14. # You can also use ``Tensor.to`` to transfer a tensor:
    15. b2 = torch.tensor([1., 2.]).to(device=cuda)
    16. # b.device and b2.device are device(type='cuda', index=1)
    17. c = a + b
    18. # c.device is device(type='cuda', index=1)
    19. z = x + y
    20. # z.device is device(type='cuda', index=0)
    21. # even within a context, you can specify the device
    22. # (or give a GPU index to the .cuda call)
    23. d = torch.randn(2, device=cuda2)
    24. e = torch.randn(2).to(cuda2)
    25. f = torch.randn(2).cuda(cuda2)
    26. # d.device, e.device, and f.device are all device(type='cuda', index=2)