does cuda/nvidia support kvm

Hello there,
I installed NVIDIA driver and CUDA on CentOS 7, compiled ffmpeg to do hardware codec, I added video hardware coding code to libspice-server, so that KVM loaded libspice-server.so when the virtual machine played video. I hope video playback has the ability to accelerate hardware encoding, but now libspice-server.so called by kvm always prompts CUDA initialization error(cuInit return err != CUDA_SUCCESS)

I also compiled ffmpeg and CUDA examples, all example applications worked correctly, so is CUDA/NVIDIA does not support KVM or any other settings I need to do?

[AVHWDeviceContext @ 0x55db9479d0c0] Loaded lib: libcuda.so.1
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuInit
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuDeviceGetCount
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuDeviceGet
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuDeviceGetName
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuDeviceComputeCapability
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuCtxCreate_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuCtxSetLimit
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuCtxPushCurrent_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuCtxPopCurrent_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuCtxDestroy_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuMemAlloc_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuMemFree_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuMemcpy2D_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuMemcpy2DAsync_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGetErrorName
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGetErrorString
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuStreamCreate
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuStreamQuery
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuStreamSynchronize
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuStreamDestroy_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuStreamAddCallback
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuEventCreate
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuEventDestroy_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuEventSynchronize
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuEventQuery
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuEventRecord
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGLGetDevices_v2
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGraphicsGLRegisterImage
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGraphicsUnregisterResource
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGraphicsMapResources
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGraphicsUnmapResources
[AVHWDeviceContext @ 0x55db9479d0c0] Loaded sym: cuGraphicsSubResourceGetMappedArray
[AVHWDeviceContext @ 0x55db9479d0c0] Could not initialize the CUDA driver API
Failed to create a CUDA device. Error code: Unknown error occurred

If I decoded your question correctly you are missing access to nvDEC inside virtual/guest machine.

If you are looking for virtual/guest accelerate video decoding/stream-forwarding with KVM/qemu/spice I suppose that is not possible (see https://lists.freedesktop.org/archives/spice-devel/2016-August/031140.html).

If you have CUDA problems with KVM in virtualization host try also CUDA forums https://devtalk.nvidia.com/default/board/58/cuda-setup-and-installation/ or https://devtalk.nvidia.com/default/board/57/cuda-programming-and-performance/.

Thank you very much for your answer.

We do not want to use NIVIDA’s GPU virtualization. Instead, KVM is treated as a normal process. KVM enables GPU computing by loading libspice-server.so. The ffmpeg library with hardware-accelerated ability is compiled to libspice-server.so. So, how can we make KVM use GPU hardware acceleration in this situation? Whether the KVM process can directly enable hardware acceleration?

Hi lzhujian,

Are you trying to run on a virtual GPU set up?

If yes, as mentioned in earlier comment, only some Virtual GPU profiles/cards allows CUDA in virtual machine (see https://docs.nvidia.com/grid/latest/grid-vgpu-user-guide/index.html#features-grid-vgpu). Please check your’s is one of these.

Thanks,
Ryan Park

Hi rypark, thanks for your answer.

Our graphics card model is tesla p4, but we are not using GPU virtualization. Instead, KVM directly calls CUDA via libspice-server.so (actually the ffmpeg library with hardware encoding capabilities) to obtain hardware encoding capabilities.

Hi lzhujian,

Could you provide us a sample code that reproduces the issue?

Thanks,
Ryan Park