How to translate heap pointer to ffi cuda

in my cuda file it is defined as below

struct CudaStreamContext;
typedef struct CudaStreamContext cuda_stream_ptr;

void _launch_stream_context(cuda_stream_ptr *cs_ptr)
{
    checkCudaErrors(cudaSetDevice(0));
    checkCudaErrors(cudaSetDeviceFlags(cudaDeviceBlockingSync | cudaDeviceMapHost));
    checkCudaErrors(cudaStreamCreate(
        (cudaStream_t *)cs_ptr->ptr
    ));
}

void _destroy_cuda_stream(cuda_stream_ptr *cs_ptr)
{
    checkCudaErrors(cudaStreamDestroy(
        *((cudaStream_t *)cs_ptr->ptr)
    ));
}

extern "C"
{
    void launch_stream_context(cuda_stream_ptr *cs_ptr)
    {
        _launch_stream_context(cs_ptr);
    }

    void destroy_cuda_stream(cuda_stream_ptr *cs_ptr)
    {
        _destroy_cuda_stream(cs_ptr);
    }
}

in my rust file

#[repr(C)]
pub struct CudaStreamContext {
    ptr: *mut c_void,
}

pub struct GpuContext {
    pub stream_context: *mut CudaStreamContext,
}

impl GpuContext {
    pub fn new() -> GpuContext {
        let mut stream_context = Box::new(CudaStreamContext {
            ptr: std::ptr::null_mut(),
        });
        let stream_context_ptr: *mut CudaStreamContext = &mut *stream_context;
        get_stream_ptr(stream_context_ptr);
        destory_stream_ptr(stream_context_ptr);
        GpuContext {
            stream_context: stream_context_ptr,
        }
    }
}

#[link(name = "cuda")]
extern "C" {
    fn launch_stream_context(stream_ptr: *mut CudaStreamContext);
    fn destroy_cuda_stream(stream_ptr: *mut CudaStreamContext);
}

fn get_stream_ptr(rust_stream_ptr: *mut CudaStreamContext) {
    unsafe {
        launch_stream_context(rust_stream_ptr);
    }
}

fn destory_stream_ptr(rust_stream_ptr: *mut CudaStreamContext) {
    unsafe {
        destroy_cuda_stream(rust_stream_ptr);
    }
}

but it throws out some error by nvcc

cargo:warning=cuda_lib/cuda.cu(46): error: pointer to incomplete class type is not allowed

please help !!!

it seems like struct define in rust can't be used in .cu
the compiler don't know the struct CudaStreamContext.
so how can i call the CudaStreamContext.ptr inner number ???

must apply in cuda file like that

typedef struct CudaStreamContext
{
    void *ptr;
} cuda_stream_ptr;

and alloc some heap memory for cuda context

void _launch_stream_context(cuda_stream_ptr *cs_ptr)
{
    checkCudaErrors(cudaSetDevice(0));
    checkCudaErrors(cudaSetDeviceFlags(cudaDeviceBlockingSync | cudaDeviceMapHost));
    cs_ptr->ptr = malloc(sizeof(cudaStream_t));
    checkCudaErrors(cudaStreamCreate(
        (cudaStream_t *)cs_ptr->ptr));
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.