嵌入式
返回首页

OpenCV编译C++测试程序获取CUDA设备信息

2026-04-15 来源:EEWorld 论坛

OpenCV编译C++测试程序获取CUDA设备信息

本文介绍如何编译一个OpenCV C++测试程序,以获取CUDA设备信息。测试代码基于OpenCV库,使用g++编译器进行编译,并运行在支持CUDA的设备上。

测试代码

首先,创建一个C++源文件(例如test-cv.cpp),包含以下代码:

#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <iostream>

using namespace cv;
using namespace cv::cuda;
using namespace std;

int main(int argc, char **argv) {
    cuda::printCudaDeviceInfo(cuda::getDevice());
    int cnt = getCudaEnabledDeviceCount();
    printf("Now get CUDA device count:%d \r\n", cnt);
    return 0;
}

编译步骤

手动编译的OpenCV安装目录在/usr/local下(根据CMake参数 -DCMAKE_INSTALL_PREFIX=/usr/local 设定)。因此,使用g++编译时需要指定头文件目录和动态库位置:

  • 使用 -I 参数指定OpenCV头文件目录,例如 -I/usr/local/include/opencv4
  • 使用 -L 参数指定动态库位置,例如 -L/usr/local/lib
  • 链接必要的OpenCV库,基础库为 -lopencv_core,其他可能用到的库包括 -lopencv_highgui-lopencv_imgproc 等。

编译命令示例:

g++ test-cv.cpp -o test-cv -I/usr/local/include/opencv4 -L/usr/local/lib -lopencv_core

运行结果

运行编译后的程序,输出CUDA设备信息:

./test-cv 
*** CUDA Device Query (Runtime API) version (CUDART static linking) *** 

Device count: 1

Device 0: "Orin"
  CUDA Driver Version / Runtime Version          11.40 / 11.40
  CUDA Capability Major/Minor version number:    8.7
  Total amount of global memory:                 7471 MBytes (7834177536 bytes)
  GPU Clock Speed:                               0.62 GHz
  Max Texture Dimension Size (x,y,z)             1D=(131072), 2D=(131072,65536), 3D=(16384,16384,16384)
  Max Layered Texture Size (dim) x layers        1D=(32768) x 2048, 2D=(32768,32768) x 2048
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per block:           1024
  Maximum sizes of each dimension of a block:    1024 x 1024 x 64
  Maximum sizes of each dimension of a grid:     2147483647 x 65535 x 65535
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and execution:                 Yes with 2 copy engine(s)
  Run time limit on kernels:                     No
  Integrated GPU sharing Host Memory:            Yes
  Support host page-locked memory mapping:       Yes
  Concurrent kernel execution:                   Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support enabled:                No
  Device is using TCC driver mode:               No
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Bus ID / PCI location ID:           0 / 0
  Compute Mode:
      Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) 

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version  = 11.40, CUDA Runtime Version = 11.40, NumDevs = 1

Now get CUDA device count:1

以上内容根据原帖子整理,保留了核心逻辑和代码示例。更多详细步骤、视频讲解和其他资源,请阅读 原帖子:OpenCV编译C++测试程序获取cuda设备信息

原帖子内容来源:https://bbs.eeworld.com.cn/thread-1270395-1-1.html



进入嵌入式查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • FollowMe 第二季:3 - EK_RA6M5 开发板入门

  • FollowMe 第二季: 1 Adafruit Circuit Playground Express及任务讲解

  • Azure RTOS step by step workshop

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • 从0到1:树莓派与物联网教程(英文)

精选电路图
  • 10-14瓦甲类放大器

  • 立体声调频发射器

  • 射频前置放大器

  • 4瓦锁相环调频发射机

  • 30瓦特线性调频放大器(采用BLY89晶体管)

  • 7瓦乙类音频放大器

    相关电子头条文章