Cutorch provides a CUDA backend for torch7.
Cutorch provides the following:
- a new tensor type:
torch.CudaTensor
that acts liketorch.FloatTensor
, but all it's operations are on the GPU. Most of the tensor operations are supported by cutorch. There are a few missing ones, which are being implemented. The missing list can be found here: torch#70 cutorch.*
- Functions to set/get GPU, get device properties, memory usage, set/get low-level streams, set/get random number generator's seed, synchronization etc. They are described in more detail below.
Computations on CUDA tensors must be run on the CUDA device where the tensor resides. Running a computation on a tensor from the wrong device will lead to a cutorch error.
If device is set to 0, cutorch will automatically determine where to run computation. In this mode, tensors must be created with the torch.CudaTensorOn(device,...)
, :cudaOn(device,...)
, and :cloneOn(device)
convenience methods.
cutorch.setDevice(0)
local t1 = torch.CudaTensorOn(2, 1000) -- on device 2
local t2 = torch.Tensor(1000):cudaOn(3) -- on device 3
local t3 = t1 + 1 -- on device 2
Transfering a FloatTensor src
to the GPU:
dest = src:cuda() -- dest is on the current GPU
Allocating a tensor on a given GPU:
Allocate src
on GPU 3
src = torch.CudaTensorOn(3, 100)
Copying a CUDA tensor from one GPU to another:
Given a tensor called src
on GPU 1, if you want to create its clone on GPU 2, then:
local dest = src:cloneOn(2)