# Tensor.pin\_memory allocates memory on cuda:0

**URL:** <https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186>\
**Category:** distributed\
**Created:** [February 18, 2020, 2:11pm UTC](https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186 "2020-02-18T14:11:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evgeny\_Shalnov](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/evgeny_shalnov/32/20680_2.png) [@Evgeny\_Shalnov](https://discuss.pytorch.org/u/Evgeny_Shalnov)\
**Post date:** [February 18, 2020, 2:11pm UTC](https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186/1 "2020-02-18T14:11:40Z")

</div>

The following code always allocates memory on device cuda:0, but I want to use another GPU for training

import torch  
a = torch.randn([20000, 20000])  
a.pin\_memory() # \<- this operation allocates memory on “cuda:0”, but I want to leave it unused.  
b = a.cuda(‘cuda:1’)

---

<div class="post-metadata">

**Author:** ![seliad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/seliad/32/16357_2.png) [@seliad](https://discuss.pytorch.org/u/seliad)\
**Post date:** [February 18, 2020, 3:49pm UTC](https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186/2 "2020-02-18T15:49:06Z")

</div>

Not sure why it allocates memory,  
But I’d try using torch.set\_device(‘cuda:1’)

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [February 18, 2020, 5:09pm UTC](https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186/3 "2020-02-18T17:09:24Z")

</div>

This most likely happens because to get pinned memory, we need a cuda context. And so we initialize a cuda context on the current device when it is needed. Which would be cuda:0 here by default.  
Changing the device will help.

Also if you never want to touch cuda:0, a good practice is to use the `CUDA_VISIBLE_DEVICES=1` environment variable. This acts on the nvidia driver for the current process and hides the other GPUs. That way, you are sure that you never use them.

---

<div class="post-metadata">

**Author:** ![Evgeny\_Shalnov](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/evgeny_shalnov/32/20680_2.png) [@Evgeny\_Shalnov](https://discuss.pytorch.org/u/Evgeny_Shalnov)\
**Post date:** [February 18, 2020, 6:27pm UTC](https://discuss.pytorch.org/t/tensor-pin-memory-allocates-memory-on-cuda-0/70186/4 "2020-02-18T18:27:44Z")

</div>

torch.set\_device(‘cuda:1’) helped
