I'm trying to write a custom CXX extension for Python which takes CuPy arrays as input. What's the recommended way to hand off and return CuPy arrays from a CXX extension?
My current approach is to pass a memory pointer to the extension as an integer and modify the CuPy array in-place. However, with this approach there is some strange behavior when the array is passed to two extension functions without being re-cast as a CuPy array in-between.
I'm working on a functioning example, but in the mean-time, here is a schematic of my approach, and I'd like suggestions on how to do exchange arrays between CuPy and CXX.
import cupy as cp
a = cp.zeros((1, 2, 3))
cxx_function_modifying_a_in_place(a.data.ptr)
a = cp.array(a) # required else something goes wrong
cxx_function_modifying_a_in_place(a.data.ptr)
The following function is wrapped using pybind11 or SWIG
void cxx_function_modifying_array_in_place(size_t g_)
{
// convert pointers to correct type
g = (float2 *)g_;
// do some stuff to g on a GPU
cuda_kernel<<<1, 1>>>(g);
cufftExecC2C(plan2d, (cufftComplex *)g, (cufftComplex *)g, CUFFT_FORWARD);
}