Unknown error when using opencv with cuda

Hello. I am new to using opencv with cuda.
I have built opencv-4.5.1 from source with cuda 11.2 capability

Result of nvcc --version:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Nov_30_19:08:53_PST_2020
Cuda compilation tools, release 11.2, V11.2.67
Build cuda_11.2.r11.2/compiler.29373293_0

I built opencv-4.5.1 with the following command

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=ON -D BUILD_DOCS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D BUILD_PACKAGE=OFF -D BUILD_EXAMPLES=OFF -D WITH_TBB=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D WITH_CUFFT=ON -D WITH_NVCUVID=ON -D WITH_IPP=OFF -D WITH_V4L=ON -D WITH_1394=OFF -D WITH_GTK=ON -D WITH_QT=OFF -D WITH_OPENGL=ON -D WITH_EIGEN=ON -D WITH_FFMPEG=ON -D WITH_GSTREAMER=ON -D BUILD_JAVA=OFF -D BUILD_opencv_python3=ON -D BUILD_opencv_python2=OFF -D BUILD_NEW_PYTHON_SUPPORT=ON -D OPENCV_SKIP_PYTHON_LOADER=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_ENABLE_NONFREE=ON -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.5.1/modules -D WITH_CUDNN=ON -D OPENCV_DNN_CUDA=ON -D CUDA_ARCH_BIN=8.6  -D CUDA_ARCH_PTX=8.6 -D CUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so.8.1.0 -D CUDNN_INCLUDE_DIR=/usr/local/cuda/include  .. 

I checked that opencv was installed with correct cuda & cudnn versions associated. My desktop has RTX 3070 ti installed, so compute capability = 8.6

Then I built the following test code with g++ (version 9.3)

#include <iostream>
using namespace std;

#include <opencv2/core.hpp>
#include <opencv2/core/version.hpp>
using namespace cv;

#include <opencv2/cudaarithm.hpp>
using namespace cv::cuda;

int main()
{
    
    printShortCudaDeviceInfo(getDevice());


    int cuda_devices_number = getCudaEnabledDeviceCount();
    cout << "CUDA Device(s) Number: "<< cuda_devices_number << endl;
    DeviceInfo _deviceInfo;


    bool _isd_evice_compatible = _deviceInfo.isCompatible();
    cout << "CUDA Device(s) Compatible: " << _isd_evice_compatible << endl;


    return 0;
}

But I got the following error code instead of a valid result.

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.1) /media/kgc0609/kgc_storage/opencv-sources/opencv-4.5.1/modules/core/src/cuda_info.cpp:84: error: (-217:Gpu API call) unknown error in function 'getDevice'

Aborted (core dumped)

What could have gone wrong? I tried re-installing opencv multiple times, but failed to solve this issue.

nvdia-smi result :

Tue Mar  8 23:51:16 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.103.01   Driver Version: 470.103.01   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0  On |                  N/A |
|  0%   48C    P8    23W / 220W |    652MiB /  7973MiB |      3%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

I manually installed cuda version 11.2, but it is written 11.4 above. Could this be the reason of this problem?

why such an outdated version ?
you also needed to build with the opencv_contrib modules and if you just git clone that, you get 4.5.5-dev there, so possibly a version conflict

do anyone a favour and try again with latest for both repos