# Selecting the GPU

**URL:** <https://discuss.pytorch.org/t/selecting-the-gpu/20276>\
**Category:** Uncategorized\
**Created:** [June 26, 2018, 1:10pm UTC](https://discuss.pytorch.org/t/selecting-the-gpu/20276 "2018-06-26T13:10:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWSurfer](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gwsurfer/32/7345_2.png) [@GWSurfer](https://discuss.pytorch.org/u/GWSurfer)\
**Post date:** [June 26, 2018, 1:10pm UTC](https://discuss.pytorch.org/t/selecting-the-gpu/20276/1 "2018-06-26T13:10:59Z")

</div>

Hi guys, I am a PyTorch beginner trying to get my model to train on a specific GPU on my machine. I am giving as an input the following code:

```auto
torch.cuda.device_count()
cuda0 = torch.cuda.set_device(0)
torch.cuda.current_device(). # output: 0
torch.cuda.get_device_name(0)

```

The output for the last command is ‘Tesla K40c’, which is the GPU I want to use. The problem is that my the training time for an epoch keeps being exactly the same as the training time on an outdated GPU1 Quadro 2000 (cuda 2.1)…

The question is: how do I set the device, define the variables and the model correctly (e.g. .cuda(0)??) so that my training runs on the right GPU? Is it possible to do it without passing through bash?

---

<div class="post-metadata">

**Author:** ![justusschock](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/justusschock/32/4637_2.png) [@justusschock](https://discuss.pytorch.org/u/justusschock)\
**Post date:** [June 26, 2018, 3:31pm UTC](https://discuss.pytorch.org/t/selecting-the-gpu/20276/2 "2018-06-26T15:31:19Z")

</div>

If you are using Pytorch 0.4 you could specify the device by doing

```auto
device = torch.device('cuda:0')
X = X.to(device)

```

Cuda:0 is always the first visible GPU. So if you set CUDA\_VISIBLE\_DEVICES (which I would recommend since pytorch will create cuda contexts on all other GPUs otherwise) to another index (e.g. 1), this GPU is referred to as cuda:0. Alternatively you could specify the device as `torch.device('cpu')` for running your model/tensor on CPU.

The global GPU index (which is necessary to set CUDA\_VISIBLE\_DEVICES in the right way) can be seen by the `nvidia-smi` command in the shell/bash.
