如何使用CUDA选择GPU? [英] How to select a GPU with CUDA?

查看:886
本文介绍了如何使用CUDA选择GPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台带有2个GPU的计算机;我写了一个CUDA C程序,我需要以某种方式告诉我我想只在2个图形卡中的1个上运行它。我需要输入什么命令,应该如何使用?我相信与 cudaSetDevice 相关的某种方式,但是我真的找不到如何使用它的方法。

I have a computer with 2 GPUs; I wrote a CUDA C program and I need to tell it somehow that I want to run it on just 1 out of the 2 graphic cards; what is the command I need to type and how should I use it? I believe somehow that is related to the cudaSetDevice but I can't really find out how to use it.

推荐答案

文档 cudaSetDevice ,但让我提供以下代码段。

It should be pretty much clear from documentation of cudaSetDevice, but let me provide following code snippet.

bool IsGpuAvailable()
{
    int devicesCount;
    cudaGetDeviceCount(&devicesCount);
    for(int deviceIndex = 0; deviceIndex < devicesCount; ++deviceIndex)
    {
        cudaDeviceProp deviceProperties;
        cudaGetDeviceProperties(&deviceProperties, deviceIndex);
        if (deviceProperties.major >= 2
            && deviceProperties.minor >= 0)
        {
            cudaSetDevice(deviceIndex);
            return true;
        }
    }

    return false;
}

这就是我遍历所有可用GPU的方式( cudaGetDeviceCount )寻找计算能力至少为2.0的第一个。如果找到了这样的设备,那么我使用 cudaSetDevice ,因此所有CUDA计算都在该特定设备上执行。如果不执行 cudaSetDevice ,则您的CUDA应用程序将在第一个GPU上执行,即,具有 deviceIndex == 0 的GPU。具体的GPU取决于哪个PCIe插槽位于哪个GPU。

This is how I iterated through all available GPUs (cudaGetDeviceCount) looking for the first one of Compute Capability of at least 2.0. If such device was found, then I used cudaSetDevice so all the CUDA computations were executed on that particular device. Without executing the cudaSetDevice your CUDA app would execute on the first GPU, i.e. the one with deviceIndex == 0 but which particular GPU is that depends on which GPU is in which PCIe slot.

编辑:

在注释中澄清您的问题后,在我看来,应该适合根据其名称选择设备。如果不确定实际的GPU名称,请运行以下代码,将所有GPU的名称打印到控制台中:

After clarifying your question in comments, it seems to me that it should be suitable for you to choose the device based on its name. If you are unsure about your actual GPU names, then run this code which will print names of all your GPUs into console:

int devicesCount;
cudaGetDeviceCount(&devicesCount);
for(int deviceIndex = 0; deviceIndex < devicesCount; ++deviceIndex)
{
    cudaDeviceProp deviceProperties;
    cudaGetDeviceProperties(&deviceProperties, deviceIndex);
    cout << deviceProperties.name << endl;
}

之后,选择要用于计算的GPU的名称,可以说是 GTX XYZ 。从您的 main 方法中调用以下方法,由于它,所有CUDA内核将在名称为 GTX XYZ的设备上执行。您还应该检查返回值-如果找到具有该名称的设备,则返回值 true ,否则为 false

After that, choose the name of the GPU that you want to use for computations, lets say it is "GTX XYZ". Call the following method from your main method, thanks to it, all the CUDA kernels will be executed on the device with name "GTX XYZ". You should also check the return value - true if device with such name is found, false otherwise:

bool SetGPU()
{
    int devicesCount;
    cudaGetDeviceCount(&devicesCount);
    string desiredDeviceName = "GTX XYZ";
    for(int deviceIndex = 0; deviceIndex < devicesCount; ++deviceIndex)
    {
        cudaDeviceProp deviceProperties;
        cudaGetDeviceProperties(&deviceProperties, deviceIndex);
        if (deviceProperties.name == desiredDeviceName)
        {
            cudaSetDevice(deviceIndex);
            return true;
        }
    }

    return false;
}

当然,您必须更改 desiredDeviceName的值更改为所需值。

Of course you have to change the value of desiredDeviceName variable to desired value.

这篇关于如何使用CUDA选择GPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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