Unable to create OpenCV.sln that works successfully with CUDA because using it gives a message that CUDA is not supported

Unable to create OpenCV.sln that works successfully with CUDA because using it
gives a message that CUDA is not supported.

Explanation:

Environment:
NVIDIA GeForce RTX 4070 Ti GPU, Z790-P chipset;
Windows 11;
Visual Studio Community 17 2022;
NVIDIA GeForce Game Ready 560.70 driver;
NVIDIA toolkit 12.5.

OpenCV is built with cmake-gui v 3.30.1.
OpenCV.sln creation process:

  1. Create a new directory 4100cuda and put the source code from the latest versions there
    opencv-4.10.0-windows.exe and opencv_contrib-4.10.0.zip

  2. Run cmake-gui and set the source and output code directories in it.

  3. Run the initial configuration.

  4. Set the parameters: OPENCV_ENABLE_NONFREE=ON; CUDA_FAST_MATH=ON; ENABLE_FAST_MATH=ON;
    BUILD_opencv_world=ON; BUILD_CUDA_STUBS=OFF; OPENCV_DNN_CUDA=OFF; WITH_CUDA=ON;
    OPENCV_EXTRA_MODULES_PATH=path to opencv_contrib-4.10.0\modules and repeatedly
    press CONFIGURE until the red lines disappear.

  5. If the variables are not set, press Add Entry for CUDA_INCLUDE_DIRS and CUDA_HOME.

  6. Press Generate, Open Project for VS 17 2022.

  7. In running VS in CMakeTargets, select ALL_BUILD in Solution Configurations,
    run build with the mouse and wait for OpenCV to successfully complete in the 4100cuda directory.

  8. Run the INSTALL project build and repeat the same for DEBUG.

  9. Create or make sure that the system variables are correct
    CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5
    CUDA_PATH_V12_5=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.5
    OPENCV_DIR=d:\source.dsc\OpenCV\4100cuda\opencv\build\

  10. Successfully create your project using this version of OpenCV.
    In it, insert matrix addition as tests:

global void addKernel(int* c, const int* a, const int* b) // for testing
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}
cudaError_t addWithCuda(int* c, const int* a, const int* b, unsigned int size)

and checking for CUDA availability:

std::cout << "\n Compiled with CUDA: " << cv::cuda::getCudaEnabledDeviceCount() << std::endl;
int numDevices = cv::cuda::getCudaEnabledDeviceCount();
std::cout << "Number of CUDA devices: " << numDevices << std::endl;
if (numDevices > 0) {

} else {
std::cout << “CUDA is not supported.” << std::endl;
}

as well as the main code using CUDA:

  1. We run our project and get that CUDA works, successfully adding two matrices

{1,2,3,4,5} + {10,20,30,40,50} = {11,22,33,44,55}

but also get a message that CUDA is not supported! :

Number of CUDA devices: 0
CUDA is not supported.

And during further execution of the program, it crashes on the first operator using CUDA

%%Err:: computeCorrelation()
OpenCV(4.10.0) C:\GHA-OCV-1_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\core\include\opencv2/core/private.cuda.hpp:106: error: (-216:No CUDA support) The library is compiled without CUDA support in function ‘throw_no_cuda’

Thanks.
N.S.Y.

I suspect you haven’t built with CUDA support check the CUDA section of output the CMake configuration ouput to make sure, e.g

--   NVIDIA CUDA:                   YES (ver 12.5.40, CUFFT CUBLAS NVCUVID NVCUVENC)
--     NVIDIA GPU arch:             50 52 60 61 70 75 80 86 89 90
--     NVIDIA PTX archs:            90
--
--   cuDNN:                         YES (ver 9.2.0)

I would advise against using the CMake GUI for OpenCV with CUDA, so if you didn’t build with CUDA, try again using the CMake command line to a fresh build directory.

Thanks for the advice, but all the flags you mentioned are already set:
NVIDIA CUDA: (WITH_CUFFT=on, WITH_CUBLAS=on,WITH_NVCUVID=on,WITH_NVCUVENC=on)
CUDA_ARCH_BIN=5.0;5.2;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0, CUDA_ARCH_PTX=9.0
WITH_CUDNN=on
Perhaps the problem is something else?

First confirm that you configured with CUDA then look for something else. Requesting to build with CUDA does not mean that CMake managed to find the CUDA toolkit and configure OpenCV to use it. To verify that refer to my previous post.

I reconfigured the project following your comments.

--   NVIDIA CUDA:                   YES (ver 12.6, CUFFT CUBLAS NVCUVID NVCUVENC FAST_MATH)
--     NVIDIA GPU arch:             50 52 60 61 70 75 80 86 89 90
--     NVIDIA PTX archs:            90
-- 
--   cuDNN:                         YES (ver 9.3.0)

The result remained the same.

Did you build it successfully. CUDA 12.6 is not supported yet so I would expect the build to fail, see

Use CUDA 12.5 as described in your original post.

If the error is still

the version of OpenCV you are linking against was not built with CUDA support.

You can confirm this by inspecting the output from

std::cout << cv::getBuildInformation() << std::endl;

Yes please, here is the result:

General configuration for OpenCV 4.10.0 =====================================
Version control: 4.10.0

Platform:
Timestamp: 2024-06-02T16:42:08Z
Host: Windows 10.0.19045 AMD64
CMake: 3.23.3
CMake generator: Visual Studio 16 2019
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
MSVC: 1929
Configuration: Debug Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (16 files): + SSSE3 SSE4_1
SSE4_2 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (0 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (8 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (36 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
AVX512_SKX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe (ver 19.29.30154.0)
C++ flags (Release): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /O2 /Ob2 /DNDEBUG
C++ flags (Debug): /DWIN32 /D_WINDOWS /W4 /GR /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4819 /MP /Zi /Ob0 /Od /RTC1
C Compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe
C flags (Release): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /O2 /Ob2 /DNDEBUG
C flags (Debug): /DWIN32 /D_WINDOWS /W3 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi /fp:precise /MP /Zi /Ob0 /Od /RTC1
Linker flags (Release): /machine:x64 /INCREMENTAL:NO
Linker flags (Debug): /machine:x64 /debug /INCREMENTAL
ccache: NO
Precompiled headers: NO
Extra dependencies:
3rdparty dependencies:

OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo stitching video videoio world
Disabled: python3
Disabled by dependency: -
Unavailable: java python2 ts
Applications: apps
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
VTK support: NO

Media I/O:
ZLib: build (ver 1.3.1)
JPEG: build-libjpeg-turbo (ver 3.0.3-70)
SIMD Support Request: YES
SIMD Support: NO
WEBP: build (ver encoder: 0x020f)
PNG: build (ver 1.6.43)
SIMD Support Request: YES
SIMD Support: YES (Intel SSE)
TIFF: build (ver 42 - 4.6.0)
JPEG 2000: build (ver 2.5.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (58.134.100)
avformat: YES (58.76.100)
avutil: YES (56.70.100)
swscale: YES (5.9.100)
avresample: YES (4.0.0)
GStreamer: NO
DirectShow: YES
Media Foundation: YES
DXVA: YES

Parallel framework: Concurrency

Trace: YES (with Intel ITT)

Other third-party libraries:
Intel IPP: 2021.11.0 [2021.11.0]
at: C:/GHA-OCV-1/_work/ci-gha-workflow/ci-gha-workflow/build/3rdparty/ippicv/ippicv_win/icv
Intel IPP IW: sources (2021.11.0)
at: C:/GHA-OCV-1/_work/ci-gha-workflow/ci-gha-workflow/build/3rdparty/ippicv/ippicv_win/iw
Eigen: NO
Custom HAL: NO
Protobuf: build (3.19.1)
Flatbuffers: builtin/3rdparty (23.5.9)

OpenCL: YES (NVD3D11)
Include path: C:/GHA-OCV-1/_work/ci-gha-workflow/ci-gha-workflow/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python (for build): C:/Python-3.9/python.exe

Java:
ant: C:/apache-ant-1.9.15/bin/ant.bat (ver 1.9.15)
Java: NO
JNI: C:/Program Files/Java/jdk-11.0.9/include C:/Program Files/Java/jdk-11.0.9/include/win32 C:/Program Files/Java/jdk-11.0.9/include
Java wrappers: NO
Java tests: NO

Install to: C:/GHA-OCV-1/_work/ci-gha-workflow/ci-gha-workflow/install

1 CUDA devices available.

Compiled with CUDA: 0
Number of CUDA devices: 0
CUDA is not supported.
{1,2,3,4,5} + {10,20,30,40,50} = {11,22,33,44,55}

========================================================================================================
perspWarp v911 08.07.24 комп=lL OpenCV=source.dsc\OpenCV\4100cuda1\ world4100
Пуск в Fri Aug 9 10:21:56 PM
d:/0n/0 DATA/perspWarp1NSU-1
src5/03774.png 8UC3 640x 360y, src5/n0(1).jpg 8UC3 2497x 1190y

I’m skipping a few lines of program initialization, although I can provide them if needed.

%%Err:: computeCorrelation()
OpenCV(4.10.0) C:\GHA-OCV-1_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\core\include\opencv2/core/private.cuda.hpp:106: error: (-216:No CUDA support) The library is compiled without CUDA support in function ‘throw_no_cuda’
:0.00max0x0y 0.00 0x0y time= 1с.
Fri Aug 9 10:21:57 PM

From the output the version of OpenCV you are linking against is not built with with CUDA (there is no CUDA section) or against the contrib repository which contains all the CUDA modules.

I would follow the guide below I wrote for details on how to build with CUDA on Windows. The key is not to use CMake GUI and if you also build the test applications you can simply verify CUDA acceleration by running one of them after the build.