I am building an application where I would like to create and manage my own CUDA contexts using the driver API. My understanding is that I should be able to create a context and have the CUDA runtime libraries use that context too. However, some particular cuFFT runtime calls appear to be changing the context, when I would like it to use the existing context. I have written a minimal example to demonstrate this:
CUcontext current_render_context;
cuCtxGetCurrent(¤t_render_context);
// prints different to above
std::cout << "current ctx: " << current_render_context << std::endl;
}
Building and running the above with CUDA 11.1 results in different values being printed for the contexts. Is this the expected behaviour, and if so, how should I ensure that I can run commands in my chosen context after a call to cuFFT?
I have not noted any other runtime calls which change the context other than this and the related cufftEstimateMany, but I have not gone digging any further. Other calls to cuFFT in my program (e.g. cufftExecC2C) do not appear to change the context.
my guess is that the other context that is popping up here is the primary context.
Anyway you may wish to file a bug. As a workaround, I assume you know how to save and restore contexts - you can do that after your usage of these calls, if you wish.
Hi @Yuki_Ni
As you said, this is a kind of bug of cuFFT. I’ve observed this behavior on Jetson Xavier (libcufft-11-4). It seem to me that
cuFFT runtime library also force the use primary context and make this context become current to calling thread.
The previous context become invalid unless, calling thread pop out the primary context
Do you know how to keep cuFFT using the same context that previous CUDA runtime APIs has been using? otherwise after cuFFT API, the following Cuda runtime calls will return Invalid resource handle
The background of this question is related to opencv build with cuda and cuFFT
conv_->convolve: will eventually calls some cuFFT api which force to use primary context
extractFirstChannel_32F: will use the previous context (the a context created by Driver API or Primary context if it’s created)
In my case, I have some driver API that create the context and push it to current. OpenCV happily using that context until opencv invoke some cuFFT library.
I’m aware of that in my CUDA driver code I can called tocuDevicePrimaryCtxRetain to use the primary context.
Is there any way to workaround this issue without touch opencv source code?