CUDA interop

Heya,

I’m trying to pass Optix a pointer to device memory created in prior running CUDA code, to act as an input buffer (density values for a voxel grid).

A piece of the code:

rtBufferCreateForCUDA(context, RT_BUFFER_INPUT, &inputBuffer)

// Setup the input buffer
rtBufferSetFormat(inputBuffer, RT_FORMAT_FLOAT)
rtBufferSetSize1D(inputBuffer, tsdf->bufferSize)

rtBufferSetDevicePointer(inputBuffer, 0, (CUdeviceptr)tsdf->bufferPtr)

I’ve removed the error checks to make it easier to read.
The bufferPtr has previously been cudaMalloc’d and then cudaMemcpy’d in the previously mentioned CUDA code.

Upon running this code, rtBufferSetDevicePointer causes 2 first-chance exceptions:

  • Microsoft C++ exception: optix::GLContextCreateError at memory location
  • Microsoft C++ exception: cudaError at memory location

however this does not cause the function call to return an error (or halt my program).

This later upon the rtContextLaunch call causes another exception, and returns an error

  • Microsoft C++ exception: optix::CudaError at memory location

The error text returned is:
Unknown error (Details: Function “_rtContextLaunch2D” caught exception: Encountered a CUDA error: cuGLGetDevices() returned (999): Unknown, [3801440])HELP

Any help would be greatly appreciated!

Sounds like you’re running into some issues related to CUDA interop and GL interop not playing nicely together. I know we’ve fixed a number of issues related to CUDA interop since beta 1; please contact me privately and I’ll see about getting you the latest OptiX 3.0 build to see if this is still a bug.

I am having a similar error, here is my code:

typedef struct { 
    float x; 
    float y; 
    float z; 
    float xhat; 
    float yhat;
    float zhat; 
} source_point;

typedef struct { 
    source_point * space;
    float * E;
    float * sa;
    float * rand;
    int * cell;
} history;

int                      N=1000;
RTcontect          context;
RTbuffer            particles_obj;
RTvariable          particles;

history d_hist;
cudaMalloc( (void **) &d_hist.space,  N*sizeof(source_point));

rtContextDeclareVariable( context, "particles", &particles );
rtBufferCreateForCUDA( context, RT_BUFFER_INPUT_OUTPUT, &particles_obj  );
rtBufferSetDevicePointer( particles_obj, 0, (CUdeviceptr) d_hist.space );
rtBufferSetFormat( particles_obj, RT_FORMAT_USER );
rtBufferSetElementSize( particles_obj, sizeof(source_point) );
rtBufferSetSize1D( particles_obj, N );
rtVariableSetObject( particles, particles_obj );
printf("ERROR CHECK ---------> %s
",cudaGetErrorString(cudaPeekAtLastError()));

And I get this error after: ERROR CHECK ---------> cannot set while device is active in this process

Which is happening at the rtBufferSetDevicePointer call. Am I getting device pointers incorrectly or recasting them to CUdeviceptr incorrectly? Or does the problem have to do with the order I initialize things? I make a CUDA context by doing cudaMemcpy a few times before I do an OptiX things. It seems like OptiX cannot latch onto the existing CUDA context…