# Copy from GPU to CPU (C++)

**URL:** <https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558>\
**Category:** C++\
**Created:** [February 21, 2020, 1:55am UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558 "2020-02-21T01:55:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adaber](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/c2a13f/32.png) [@adaber](https://discuss.pytorch.org/u/adaber)\
**Post date:** [February 21, 2020, 1:55am UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558/1 "2020-02-21T01:55:19Z")

</div>

Hi everyone,

I did some testing on copying a tensor from GPU to CPU (.cpu() method) and it seems that Pytorch allocates a new chunk of “CPU” memory (RAM) every time the .cpu() method is executed.

Is there a way to copy a “GPU” tensor to a preallocated buffer in RAM (CPU side) using C++ ? I would like to avoid allocating new memory space for each “GPU to CPU” copy operation.

Thanks!

---

<div class="post-metadata">

**Author:** ![klory](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/klory/32/5814_2.png) [@klory](https://discuss.pytorch.org/u/klory)\
**Post date:** [February 21, 2020, 5:40pm UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558/2 "2020-02-21T17:40:39Z")

</div>

I don’t think it’s possible with current Python API

---

<div class="post-metadata">

**Author:** ![adaber](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/c2a13f/32.png) [@adaber](https://discuss.pytorch.org/u/adaber)\
**Post date:** [February 21, 2020, 8:50pm UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558/3 "2020-02-21T20:50:56Z")

</div>

Thanks for replying, Klory 🙂 My question is if it can be done in C++, though.

---

<div class="post-metadata">

**Author:** ![tom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/32/3162_2.png) [@tom](https://discuss.pytorch.org/u/tom)\
**Post date:** [February 21, 2020, 9:33pm UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558/4 "2020-02-21T21:33:50Z")

</div>

You could use `cputensor.copy_(gputensor)` (both in Python and C++).

Best regards

Thomas

---

<div class="post-metadata">

**Author:** ![adaber](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/c2a13f/32.png) [@adaber](https://discuss.pytorch.org/u/adaber)\
**Post date:** [February 22, 2020, 12:01am UTC](https://discuss.pytorch.org/t/copy-from-gpu-to-cpu-c/70558/5 "2020-02-22T00:01:52Z")

</div>

Excellent. Just what I needed.

Thanks, Thomas 🙂
