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

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…

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.

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