# What torch.cuda.set\_device() does?

**URL:** <https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473>\
**Category:** Uncategorized\
**Created:** [March 30, 2017, 3:44am UTC](https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473 "2017-03-30T03:44:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [March 30, 2017, 3:44am UTC](https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473/1 "2017-03-30T03:44:04Z")

</div>

Does `torch.cuda.set_device(args.gpu)` set a GPU for execution or it sets the number of GPUs should be used for execution?

If it sets the GPU for execution, how can I set multiple GPUs to run my experiment? For example, I want to tell to pytorch that you should use two GPUs (if available) to run my experiment. How can I achieve that?

---

<div class="post-metadata">

**Author:** ![xwgeng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xwgeng/32/6030_2.png) [@xwgeng](https://discuss.pytorch.org/u/xwgeng)\
**Post date:** [March 30, 2017, 4:10am UTC](https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473/2 "2017-03-30T04:10:56Z")

</div>

`torch.cuda.set_device` sets the default GPU and in order to use multi-GPU, you may use `nn.DataParallel`

---

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [March 30, 2017, 4:13am UTC](https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473/3 "2017-03-30T04:13:43Z")

</div>

Can you give an example in order to use multi-GPU?

---

<div class="post-metadata">

**Author:** ![xwgeng](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xwgeng/32/6030_2.png) [@xwgeng](https://discuss.pytorch.org/u/xwgeng)\
**Post date:** [March 30, 2017, 4:16am UTC](https://discuss.pytorch.org/t/what-torch-cuda-set-device-does/1473/4 "2017-03-30T04:16:08Z")

</div>

you may see the following example [OpenNMT](https://github.com/OpenNMT/OpenNMT-py)
