cuda - 在两个 GPU 上运行的代码无法达到并发执行并且加速效果无关

标签 cuda gpgpu multi-gpu

我有这样的代码:

for(int i =0; i<2; i++)
{
    //initialization of memory and some variables
    ........
    ........
    RunDll(input image, output image); //function that calls kernel
}

上述循环中的每次迭代都是独立的。我想同时运行它们。所以,我尝试了这个:

for(int i =0; i<num_devices; i++)
{
    cudaSetDevice(i);
    //initialization of memory and some variables
    ........
    ........
    RunDll(input image, output image); 
    {
        RunBasicFBP_CUDA(parameters); //function that calls kernel 1

        xSegmentMetal(parameters); //CPU function

        RunBasicFP_CUDA(parameters);  //function that uses output of kernel 1 as input for kernel 2

        for (int idx_view = 0; idx_view < param.fbp.num_view; idx_view++)
        {
            for (int idx_bin = 1; idx_bin < param.fbp.num_bin-1; idx_bin++)
            {
                sino_diff[idx_view][idx_bin] = sino_org[idx_view][idx_bin] - sino_mask[idx_view][idx_bin];
            }
        }

        RunBasicFP_CUDA(parameters);
        if(some condition)
        {
            xInterpolateSinoLinear(parameters);  //CPU function
        }
        else
        {
            xInterpolateSinoPoly(parameters);  //CPU function
        }

        RunBasicFBP_CUDA( parameters );
    }
}

我正在使用 2 个 GTX 680,并且我想同时使用这两个设备。使用上面的代码,我没有得到任何加速。处理时间与在单个 GPU 上运行时几乎相同。

如何在两个可用设备上实现并发执行?

最佳答案

在您的评论中您说:

RunDll has two kernels and they are being launched one by one. Kernels do have cudaThreadSynchronize()

请注意,cudaThreadSynchronize() 相当于cudaDeviceSynchronize()(前者实际上是 deprecated ),这意味着您将在一个 GPU 上运行、同步、然后在另一个GPU上运行。另请注意,cudaMemcpy() 是一个阻塞例程,您需要 cudaMemcpyAsync() 版本来避免所有阻塞(正如 @JackOLantern 在评论中指出的那样)。

一般来说,您需要发布有关 RunDLL() 内部内容的更多详细信息,因为如果没有这些信息,您的问题就没有足够的信息来给出明确的答案。最好遵循 these guidelines .

关于cuda - 在两个 GPU 上运行的代码无法达到并发执行并且加速效果无关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18632511/

相关文章:

Cuda 重用事件来确定多个部分的执行时间

machine-learning - 具有多 GPU 方法的 tensorflow 分布式训练混合

c++ - cufftPlan2d 异常

cuda - 为什么 "a =(b>0)?1:0"在 CUDA 中比 "if-else"版本更好?

CUDA 的 cudaMemcpyToSymbol() 抛出 "invalid argument"错误

Ray Worker 调用时 Tensorflow 无法检测到 GPU

python - tensorflow 多 GPU 并行使用

cuda - CUDA 在哪里为内核分配堆栈帧?

c++ - 通过GPU读取多个.dat文件

cuda - Nvidia和AMD硬件上的OpenCL FFT?