检测NVIDIA GPU的CUDA无 [英] detecting NVIDIA GPUs without CUDA

查看:348
本文介绍了检测NVIDIA GPU的CUDA无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取一组相当有限的关于NVIDIA GPU的信息,而无需对CUDA库链接。这是唯一的信息的需要的是计算能力和名称GPU的多,这可能是有用的,但它不是必需的。在code应该写在C(或C ++)。这些信息将在配置时使用(当CUDA工具包不可用),并在运行时(当执行二进制文件不支持CUDA编译)表明支持的GPU为present在用户系统。

I would like to extract a rather limited set of information about NVIDIA GPUs without linking against the CUDA libraries. The only information that is needed is compute capability and name of the GPU, more than this could be useful but it is not required. The code should be written in C (or C++). The information would be used at configure-time (when the CUDA toolkit is not available) and at run-time (when the executed binary is not compiled with CUDA support) to suggest the user that a supported GPU is present in the system.

据我了解,这是有可能通过驱动程序API,但我不是很熟悉的东西,这将需要的技术细节。所以我的问题是:

As far as I understand, this is possible through the driver API, but I am not very familiar with the technical details of what this would require. So my questions are:


  • 哪些具体步骤至少满足最低要求(见上文);

  • What are the exact steps to fulfill at least the minimum requirement (see above);

有没有这样的开源$ C ​​$ C可用?

Is there such open-source code available?

请注意,我的第一个步骤是有一些code为Linux,但最终我需要独立于平台的code。考虑CUDA平台可用性,对于一个完整的解决方案,这将涉及到code在x86 / AMD64在Linux,Mac OS和Windows(至少现在,这个名单可以得到很快与ARM扩展)。

Note that the my first step would be to have some code for Linux, but ultimately I'd need platform-independent code. Considering the platform-availability of CUDA, for a complete solution this would involve code for on x86/AMD64 for Linux, Mac OS, and Windows (at least for now, the list could get soon extended with ARM).

修改

我的意思是它通过驱动程序API是可能的,是一个人应该能够通过驱动程序API动态加载libcuda.​​so和查询设备属性。我不知道的细节,但。

What I meant by "it's possible through the driver API" is that one should be able to load libcuda.so dynamically and query the device properties through the driver API. I'm not sure about the details, though.

推荐答案

不幸的是NVML不提供有关设备计算能力的信息。

Unfortunately NVML doesn't provide information about device compute capability.

您需要做的是:


  1. 加载手动CUDA库(应用程序不是针对libcuda链接)

    • 如果库中不存在,未安装CUDA驱动程序

我希望这code会有所帮助。我已经在Linux下进行测试,但稍作修改也应该在Windows下进行编译。

I hope this code will be helpful. I've tested it under Linux but with minor modifications it should also compile under Windows.

#include <cuda.h>
#include <stdio.h>

#ifdef WINDOWS
#include <Windows.h>
#else
#include <dlfcn.h>
#endif


void * loadCudaLibrary() {
#ifdef WINDOWS
    return LoadLibraryA("nvcuda.dll");
#else
    return dlopen ("libcuda.so", RTLD_NOW);
#endif
}

void (*getProcAddress(void * lib, const char *name))(void){
#ifdef WINDOWS
    return (void (*)(void)) GetProcAddress(lib, name);
#else
    return (void (*)(void)) dlsym(lib,(const char *)name);
#endif
}

int freeLibrary(void *lib)
{
#ifdef WINDOWS
    return FreeLibrary(lib);
#else
    return dlclose(lib);
#endif
}

typedef CUresult CUDAAPI (*cuInit_pt)(unsigned int Flags);
typedef CUresult CUDAAPI (*cuDeviceGetCount_pt)(int *count);
typedef CUresult CUDAAPI (*cuDeviceComputeCapability_pt)(int *major, int *minor, CUdevice dev);

int main() {
    void * cuLib;
    cuInit_pt my_cuInit = NULL;
    cuDeviceGetCount_pt my_cuDeviceGetCount = NULL;
    cuDeviceComputeCapability_pt my_cuDeviceComputeCapability = NULL;

    if ((cuLib = loadCudaLibrary()) == NULL)
        return 1; // cuda library is not present in the system

    if ((my_cuInit = (cuInit_pt) getProcAddress(cuLib, "cuInit")) == NULL)
        return 1; // sth is wrong with the library
    if ((my_cuDeviceGetCount = (cuDeviceGetCount_pt) getProcAddress(cuLib, "cuDeviceGetCount")) == NULL)
        return 1; // sth is wrong with the library
    if ((my_cuDeviceComputeCapability = (cuDeviceComputeCapability_pt) getProcAddress(cuLib, "cuDeviceComputeCapability")) == NULL)
        return 1; // sth is wrong with the library

    {
        int count, i;
        if (CUDA_SUCCESS != my_cuInit(0))
            return 1; // failed to initialize
        if (CUDA_SUCCESS != my_cuDeviceGetCount(&count))
            return 1; // failed

        for (i = 0; i < count; i++)
        {
            int major, minor;
            if (CUDA_SUCCESS != my_cuDeviceComputeCapability(&major, &minor, i))
                return 1; // failed

            printf("dev %d CUDA compute capability major %d minor %d\n", i, major, minor);
        }
    }
    freeLibrary(cuLib);
    return 0; 
}

在Linux上测试:

Test on Linux:

$ gcc -ldl main.c
$ ./a.out
dev 0 CUDA compute capability major 2 minor 0
dev 1 CUDA compute capability major 2 minor 0

测试在Linux上,没有CUDA驱动程序

Test on linux with no CUDA driver

$ ./a.out
$ echo $?
1

干杯

这篇关于检测NVIDIA GPU的CUDA无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆