CUDA is not enabled in Ubuntu 24.04

Hello

I have compiled OpenCV 4.8.0 with CUDA support on my Ubuntu 24.04. However, C++ and Rust do not detect it, while Python does. What am I doing wrong?

Attached is my PC information.

CUDA

$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Fri_Jan__6_16:45:21_PST_2023
Cuda compilation tools, release 12.0, V12.0.140
Build cuda_12.0.r12.0/compiler.32267302_0

Nvidia

$ nvidia-smi
Tue Jul 23 12:13:56 2024       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.183.01             Driver Version: 535.183.01   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| 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  Quadro P2000                   Off | 00000000:05:00.0 Off |                  N/A |
| 45%   33C    P8               4W /  75W |      6MiB /  5120MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      5998      G   /usr/lib/xorg/Xorg                            4MiB |
+---------------------------------------------------------------------------------------+

This my python script test.py

import torch

if torch.cuda.is_available():
  print("Number of CUDA devices:", torch.cuda.device_count())
else:
  print("No CUDA devices found")

this is the result of the script

Number of CUDA devices: 1

This my CUDA script test.cu

#include <stdio.h>

__global__ void sayHello() {
    printf("Hello world from the GPU!\n");
}

int main (void)
{
    int count = 0;
    cudaError_t stat = cudaGetDeviceCount (&count);
    printf ("stat=%08x count=%d\n", stat, count);
    return EXIT_SUCCESS;
}

this is the result of the script

stat=00000000 count=1

This my c++ script test.cpp

#include <opencv2/opencv.hpp>
#include <filesystem>
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/videoio.hpp>
#include <opencv2/core.hpp> 
#include <opencv2/core/cuda.hpp>

int main() {
    int cudaDeviceCount = cv::cuda::getCudaEnabledDeviceCount();

    if (cudaDeviceCount > 0) {
        std::cout << "CUDA is enabled. It will be detected " << cudaDeviceCount << " dispositivos CUDA." << std::endl;
    } else {
        std::cout << "CUDA is not enabled on this system." << cudaDeviceCount << std::endl;
    }

    return 0;
}

this is the result of the script

CUDA is not enabled on this system.0

What am I doing wrong or what do I need to compile?

I suspect you may not have compiled OpenCV with CUDA, can you share your CMake configuration output, specifically the CUDA section. e.g.

NVIDIA CUDA: YES (ver 12.5, CUFFT CUBLAS NVCUVID NVCUVENC)
NVIDIA GPU arch: 50 52 53 60 61 62 70 72 75 80 86 87 89 90
NVIDIA PTX archs: 90

cuDNN: YES (ver 9.2.0)