Cuda error cannot set while device is active in this process (using OpenGL)

I’m getting this error when trying to call a global function from inside an OpenGL loop.
I removed all code from that function for testing and the error is still there.
I’m guessing this has something to do with threads, I create my cuda device in the main thread, then I start the OpenGL loop and I try to call my cuda function from inside the loop.
Has anyone tried to do that, combine cuda code with OpenGL for display?
I’d like to find out how to use the GPU for computation while running OpenGL, if at all possible.


Have you had a chance to study the simple OpenGL/CUDA sample app that ships with CUDA? I have CUDA 9.2 installed on a Windows machine, where the app is located here:

C:\Users\All Users\NVIDIA Corporation\CUDA Samples\v9.2\2_Graphics\simpleGL

Description in code comments:

    This example demonstrates how to use the Cuda OpenGL bindings to
    dynamically modify a vertex buffer using a Cuda kernel.

    The steps are:
    1. Create an empty vertex buffer object (VBO)
    2. Register the VBO with Cuda
    3. Map the VBO for writing from Cuda
    4. Run Cuda kernel to modify the vertex positions
    5. Unmap the VBO
    6. Render the results using OpenGL

    Host code

Unfortunately the “simpleGL” example is not using cuda computation inside the rendering loop:
// register callbacks

    // create VBO
    createVBO(&vbo, &cuda_vbo_resource, cudaGraphicsMapFlagsWriteDiscard);

    // run the cuda part

    // start rendering mainloop

As you can see it runs cuda computation only once, before entering OpenGL display loop.
I’d like to process captured video frames as they come in and then display them as an OpenGL texture. I got that part working, but any calls to runCuda (in this example) result in the mentioned error.
Is there a way to switch between computation and display? Running cuda kernel only once is unfortunately not going to work for me.


Sorry, I have zero insights into CUDA/OpenGL interop. Maybe someone else will come along and explain how it wants to work. Have your tried trawling the internet for more advanced code of that nature? Or maybe try getting help from the participants of the OpenGL subforum here in the NVIDIA forums?