From cuda document it states:
- If a context is created and made current via the driver API, subsequent runtime calls will pick up this context instead of creating a new one.
- If the runtime is initialized (implicitly as mentioned in CUDA Runtime), cuCtxGetCurrent() can be used to retrieve the context created during initialization. This context can be used by subsequent driver API calls.
I can make 1st point work. I can create context from cuda driver. then I can use cuda runtime functions without call cudaSetDevice()
, which implicitly create a new primary context.
However, I want to work via 2nd option. That is initialize the runtime first then do cuCtxGetCurrent()
and use it in cuda driver api. This does not work at all. I always raise error saying context has been destroyed or invalid. What did I do wrong?
Here is my example codes:
#define CUDA_DRIVER_API
#include <cuda.h>
#include <cuda_runtime.h>
#include <helper_cuda.h>
#include <iostream>
CUcontext check_current_ctx()
{
CUcontext context{0};
unsigned int api_ver;
checkCudaErrors(cuCtxGetCurrent(&context));
fprintf(stdout, "current context=%p\n", context);
checkCudaErrors( cuCtxGetApiVersion(context, &api_ver));
fprintf(stdout, "current context api version = %d\n", api_ver);
return context;
}
auto inital_runtime_context()
{
int current_device = 0;
int device_count = 0;
int devices_prohibited = 0;
CUcontext current_ctx{0};
cudaDeviceProp deviceProp;
checkCudaErrors(cudaGetDeviceCount(&device_count));;
if (device_count == 0) {
fprintf(stderr, "CUDA error: no devices supporting CUDA.\n");
exit(EXIT_FAILURE);
}
// Find the GPU which is selected by Vulkan
while (current_device < device_count) {
cudaGetDeviceProperties(&deviceProp, current_device);
if ((deviceProp.computeMode != cudaComputeModeProhibited)) {
checkCudaErrors(cudaSetDevice(current_device));
checkCudaErrors(cudaGetDeviceProperties(&deviceProp, current_device));
printf("GPU Device %d: \"%s\" with compute capability %d.%d\n\n",
current_device, deviceProp.name, deviceProp.major,
deviceProp.minor);
CUcontext current_ctx;
cuCtxGetCurrent(¤t_ctx);
std::cout << "current_ctx=" << current_ctx << "\n";
return current_device;
} else {
devices_prohibited++;
}
current_device++;
}
if (devices_prohibited == device_count) {
fprintf(stderr,
"CUDA error:"
" No Vulkan-CUDA Interop capable GPU found.\n");
exit(EXIT_FAILURE);
}
return -1;
}
void test_runtime_driver_op()
{
inital_runtime_context();
check_current_ctx();
}
It reports:
GPU Device 0: "GeForce RTX ..." with compute capability 7.5
current_ctx=0x6eb220
current context=0x6eb220
CUDA error at ... code=201(CUDA_ERROR_INVALID_CONTEXT) "cuCtxGetApiVersion(context, &api_ver)"