VS2019 C++测试GPU模块来验证CUDA安装成功

在VS2019中新建控制台空白项,在菜单栏视图其他窗口属性管理器中对x64win32平台都完成库目录包含目录附加依赖项的配置。 属性页VC++目录库目录 属性页VC++目录包含目录
属性页链接器输入附加依赖项 我这里写了个Python小程序来获取D://Code/OpenCV/opencv-4.5.1/cuda_build/install/x64/vc16/lib/ 这个目录下的所有.lib文件,也就是需要添加的附加依赖项。

import os
def print_names():
    cnt = 0
    ans = 0
    path = "D://Code/OpenCV/opencv-4.5.1/cuda_build/install/x64/vc16/lib/"
    filelist = os.listdir(path)  #返回该文件夹下所有的文件、文件夹名称
    for file in filelist:   #遍历所有文件
        Olddir = os.path.join(path, file)    #原来的文件路径
        if os.path.isdir(Olddir):       #如果是文件夹则跳过
            continue
        used_name = filelist[cnt]
        if used_name.endswith(".lib"):
            print("%s" %(used_name))
            ans += 1
        cnt += 1
    print("一共有"+str(cnt)+"个文件,其中有"+str(ans)+"个.lib文件")
    return True
if __name__ == '__main__':
    print_names()

Python的运行结果如下:

D:\Code\OpenCV\opencv-4.5.1\cuda_build\install\x64\vc16>python print.py
opencv_aruco451d.lib
opencv_bgsegm451d.lib
opencv_bioinspired451d.lib
opencv_calib3d451d.lib
opencv_ccalib451d.lib
opencv_core451d.lib
opencv_cudaarithm451d.lib
opencv_cudabgsegm451d.lib
opencv_cudacodec451d.lib
opencv_cudafeatures2d451d.lib
opencv_cudafilters451d.lib
opencv_cudaimgproc451d.lib
opencv_cudalegacy451d.lib
opencv_cudaobjdetect451d.lib
opencv_cudaoptflow451d.lib
opencv_cudastereo451d.lib
opencv_cudawarping451d.lib
opencv_cudev451d.lib
opencv_datasets451d.lib
opencv_dnn451d.lib
opencv_dnn_objdetect451d.lib
opencv_dnn_superres451d.lib
opencv_dpm451d.lib
opencv_face451d.lib
opencv_features2d451d.lib
opencv_flann451d.lib
opencv_fuzzy451d.lib
opencv_gapi451d.lib
opencv_hdf451d.lib
opencv_hfs451d.lib
opencv_highgui451d.lib
opencv_imgcodecs451d.lib
opencv_imgproc451d.lib
opencv_img_hash451d.lib
opencv_intensity_transform451d.lib
opencv_line_descriptor451d.lib
opencv_mcc451d.lib
opencv_ml451d.lib
opencv_objdetect451d.lib
opencv_optflow451d.lib
opencv_phase_unwrapping451d.lib
opencv_photo451d.lib
opencv_plot451d.lib
opencv_quality451d.lib
opencv_rapid451d.lib
opencv_reg451d.lib
opencv_rgbd451d.lib
opencv_saliency451d.lib
opencv_shape451d.lib
opencv_stereo451d.lib
opencv_stitching451d.lib
opencv_structured_light451d.lib
opencv_superres451d.lib
opencv_surface_matching451d.lib
opencv_text451d.lib
opencv_tracking451d.lib
opencv_video451d.lib
opencv_videoio451d.lib
opencv_videostab451d.lib
opencv_xfeatures2d451d.lib
opencv_ximgproc451d.lib
opencv_xobjdetect451d.lib
opencv_xphoto451d.lib
一共有67个文件,其中有63个.lib文件

解决方案资源管理器源文件中添加main.cpp,输入以下代码对GPU模块进行验证。

#include <opencv2\opencv.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace cv;
using namespace cv::cuda;
int main() {
    /*-------------------------以下四种验证方式任意选取一种即可-------------------------*/
    //获取显卡简单信息
    cuda::printShortCudaDeviceInfo(cuda::getDevice());  //有显卡信息表示GPU模块配置成功
    //获取显卡详细信息
    cuda::printCudaDeviceInfo(cuda::getDevice());  //有显卡信息表示GPU模块配置成功
    //获取显卡设备数量
    int Device_Num = cuda::getCudaEnabledDeviceCount();
    cout << Device_Num << endl;  //返回值大于0表示GPU模块配置成功
    //获取显卡设备状态
    cuda::DeviceInfo Device_State;
    bool Device_OK = Device_State.isCompatible();
    cout << "Device_State: " << Device_OK << endl;  //返回值大于0表示GPU模块配置成功
    system("pause");
    return 0;
}

可以看到这段代码的输出内容和 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\demo_suite 目录下控制台执行 deviceQuery.exe 的输出内容很相似。