# How to access device memory from the host whilst a kernel is running

**URL:** <https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472>\
**Category:** GPU\
**Tags:** question\
**Created:** [January 12, 2022, 7:02am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472 "2022-01-12T07:02:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johntrinder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johntrinder/32/31783_2.png) [@johntrinder](https://discourse.julialang.org/u/johntrinder)\
**Post date:** [January 12, 2022, 7:02am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/1 "2022-01-12T07:02:12Z")

</div>

I have allocated memory on the device and have passed it to a kernel function. Is it possible to read that memory from the host before the kernel finishes its execution?

If it is possible could you please put me on the right track (bearing in mind I’m a newbie to CUDA.jl)

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 12, 2022, 7:04am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/2 "2022-01-12T07:04:27Z")

</div>

You’ve basically asked the same question here: [https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/](https://discourse.julialang.org/t/julia-how-to-set-and-use-cuda-host-global-memory/)

---

<div class="post-metadata">

**Author:** ![johntrinder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johntrinder/32/31783_2.png) [@johntrinder](https://discourse.julialang.org/u/johntrinder)\
**Post date:** [January 12, 2022, 7:10am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/3 "2022-01-12T07:10:19Z")

</div>

Thanks for your speedy reply. No - the previous question was how to _write_ to device memory from the host whilst a kernel is running (which I have achieved by tasking the kernel execution).  
My new question is how to _read_ from the host side the device memory during the kernel execution. I’ve tried every which way but it seems as if it is unreadable until the kernel finishes.

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 12, 2022, 7:17am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/4 "2022-01-12T07:17:59Z")

</div>

Reading or writing doesn’t make any difference. If you allocate a device-mapped host array, you’ll be able to perform ordinary reads and writes while the GPU will be able to read and write that memory too. But again, since that’s host memory the GPU will read over the PCIe bus and those memory operations will be slow. If you want to read/write device memory without having to wait for the kernel to finish, use a separate stream to break the ordering. In that case there’s no guarantees about the validity of the memory contents.

These questions are not specific to CUDA.jl and equally apply to CUDA C, so you can also Google for them. For example, [gpgpu - Accessing cuda device memory when the cuda kernel is running - Stack Overflow](https://stackoverflow.com/questions/11005842/accessing-cuda-device-memory-when-the-cuda-kernel-is-running).

---

<div class="post-metadata">

**Author:** ![johntrinder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johntrinder/32/31783_2.png) [@johntrinder](https://discourse.julialang.org/u/johntrinder)\
**Post date:** [January 12, 2022, 8:39am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/5 "2022-01-12T08:39:16Z")

</div>

You are right, I made a booboo in my code. Now it’s working correctly. Thankyou

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 12, 2022, 8:40am UTC](https://discourse.julialang.org/t/how-to-access-device-memory-from-the-host-whilst-a-kernel-is-running/74472/6 "2022-01-12T08:40:10Z")

</div>

Great!
