Cuda 中的多个 GPU - 以前可以工作的代码,但现在不行了

Multiple GPUs in Cuda - Working code before, but not any more

我最近在 Cuda 应用程序中遇到 运行 多个 NVidia GPU 的问题。随附的代码能够在 Visual Studio 2013 年和 2015 年(Windows 7、Cuda 9.2、Nvidia 驱动程序 398.26、1xGTX1080 和 1xGTX960)的系统上一致地重现该问题。我正在为我的卡片(5.2 和 6.1)构建正确的计算能力。

具体来说,在第一个 GPU 初始化后,我无法让第二个 GPU 上的任何函数调用正常工作。错误代码始终是 "CudaErrorMemoryAllocation"。它在 Nvidia 分析器以及调试和发布版本中都失败了。我可以按任一顺序在 GPU 上进行初始化并重现问题。

在尝试扩展我当前的应用程序时出现了这个问题,该应用程序是一个大型图像处理算法流水线。此管道可以有多个独立实例,并且由于内存限制,将需要多张卡。我对这个问题感到困惑的主要原因是我以前曾使用过它 - 我有一个 Visual Profile 会话,我 运行 几年前显示我的相同卡片按预期运行。我知道的唯一区别是它在 Cuda 8.0 中。

有什么想法吗?

#include "cuda_runtime.h"
#include "cuda.h"

#include <thread>
#include <conio.h>
#include <iostream>

// Function for each thread to run
void gpuThread(int gpuIdx, bool* result)
{
    cudaSetDevice(gpuIdx); // Set gpu index

    // Create an int array on CPU
    int* hostMemory = new int[1000000];
    for (int i = 0; i < 1000000; i++)
        hostMemory[i] = i;

    // Allocate and copy to GPU
    int* gpuMemory;
    cudaMalloc(&gpuMemory, 1000000 * sizeof(int));
    cudaMemcpy(gpuMemory, hostMemory, 1000000 * sizeof(int), cudaMemcpyHostToDevice);

    // Synchronize and check errors
    cudaDeviceSynchronize();
    cudaError_t error = cudaGetLastError();
    if (error != CUDA_SUCCESS)
    {
        result[0] = false;
        return;
    }

    result[0] =  true;
}

int main()
{
    bool result1 = false;
    bool result2 = false;

    std::thread t1(gpuThread, 0, &result1);
    std::thread t2(gpuThread, 1, &result2);

    t1.join();  // Wait for both threads to complete
    t2.join();

    if (!result1 || !result2) // Verify our threads returned success
        std::cout << "Failed\n";
    else
        std::cout << "Passed\n";

    std::cout << "Press a key to exit!\n";
    _getch();

    return 0;
}

经过一天的卸载和重新安装程序后,看来这是 398.26 驱动程序的问题。较新的版本 399.07 按预期工作。