# Can the CuArray/CuPtr in CUDA.jl be directly passed to foreign functions?

**URL:** https://discourse.julialang.org/t/can-the-cuarray-cuptr-in-cuda-jl-be-directly-passed-to-foreign-functions/46939
**Category:** GPU
**Tags:** question
**Created:** [September 19, 2020, 9:58pm UTC](https://discourse.julialang.org/t/can-the-cuarray-cuptr-in-cuda-jl-be-directly-passed-to-foreign-functions/46939 "2020-09-19T21:58:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![weylspinor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weylspinor/32/23897_2.png) [@weylspinor](https://discourse.julialang.org/u/weylspinor)
#### Post date: [September 19, 2020, 9:58pm UTC](https://discourse.julialang.org/t/can-the-cuarray-cuptr-in-cuda-jl-be-directly-passed-to-foreign-functions/46939/1 "2020-09-19T21:58:13Z")

</div>

Hi,

I am just wondering if there is any way to directly pass `CuArrays` or `CuPtr` in `CUDA.jl` to C/C++, like passing `Core.Array` or `Core.Ref`. For example, I would like to define a C function which gets a device memory pointer (with type `double*`, `cuComplex*`, etc.) and pass `CuArrays` or `CuPtr` to it.

I am asking this question because I want to minimize the data transfer between a device and a host. Of course I can pass `CuArrays` or `CuPtr` to C functions by first transferring them to a host but if the size of `CuArrays` becomes large, this will produce big overhead…

---

<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: [September 21, 2020, 6:16am UTC](https://discourse.julialang.org/t/can-the-cuarray-cuptr-in-cuda-jl-be-directly-passed-to-foreign-functions/46939/2 "2020-09-21T06:16:01Z")

</div>

Yes, you can pass a `CuPtr`. Just specify `CuPtr` in your `ccall` signature and pass a `CuArray`, automatic conversion will do the rest. Of course, you’ll need to make sure the array stays alive on the Julia side for as long as the C code can use the pointer, but that’s not GPU specific.

---

<div class="post-metadata">

### Author: ![Ellipse0934](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellipse0934/32/4012_2.png) [@Ellipse0934](https://discourse.julialang.org/u/Ellipse0934)
#### Post date: [September 22, 2020, 11:44am UTC](https://discourse.julialang.org/t/can-the-cuarray-cuptr-in-cuda-jl-be-directly-passed-to-foreign-functions/46939/3 "2020-09-22T11:44:10Z")

</div>

Since it’s likely that your C/C++ program is a different process you’ll have to use the CUDA IPC API. [https://github.com/NVIDIA/cuda-samples/tree/master/Samples/simpleIPC](https://github.com/NVIDIA/cuda-samples/tree/master/Samples/simpleIPC)
