cudaDeviceSynchronize()等待只在当前的CUDA上下文或所有上下文中完成?

Ale*_*lex 4 multithreading cuda gpgpu nvidia

我使用CUDA 6.54 x GPU开普勒.

我使用多线程,CUDA运行时API和从不同CPU线程访问CUDA上下文(通过使用OpenMP - 但它并不重要).

  1. 当我打电话cudaDeviceSynchronize();时,它会等待内核只在最新调用选择的当前CUDA上下文中完成cudaSetDevice(),还是在所有CUDA上下文中完成?

  2. 如果它将等待内核在所有CUDA上下文中完成,那么它将等待当前CPU线程中使用的所有CUDA上下文(例如CPU thread_0将等待GPU:0和1)或通常所有CUDA上下文(CPU) thread_0会等待GPU:0,1,2和3)?

以下代码:

// For using OpenMP requires to set:
// MSVS option: -Xcompiler "/openmp"
// GCC option: –Xcompiler –fopenmp
#include <omp.h>

int main() {

    // execute two threads with different: omp_get_thread_num() = 0 and 1
    #pragma omp parallel num_threads(2)
    {
        int omp_threadId = omp_get_thread_num();

        // CPU thread 0
        if(omp_threadId == 0) {

            cudaSetDevice(0);
            kernel_0<<<...>>>(...);
            cudaSetDevice(1);
            kernel_1<<<...>>>(...);

            cudaDeviceSynchronize(); // what kernel<>() will wait?

        // CPU thread 1
        } else if(omp_threadId == 1) {

            cudaSetDevice(2);
            kernel_2<<<...>>>(...);
            cudaSetDevice(3);
            kernel_3<<<...>>>(...);

            cudaDeviceSynchronize(); // what kernel<>() will wait?

        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

sro*_*drb 7

当我调用cudaDeviceSynchronize(); 是否会等待内核仅在最新调用cudaSetDevice()或所有CUDA上下文中选择的当前CUDA上下文中完成?

cudaDeviceSynchronize()只会将主机与当前设置的GPU同步,如果正在使用多个GPU并且所有GPU都需要同步,cudaDeviceSynchronize()则必须为每个GPU 单独调用.

这是一个最小的例子:

cudaSetDevice(0); cudaDeviceSynchronize();
cudaSetDevice(1); cudaDeviceSynchronize();
...
Run Code Online (Sandbox Code Playgroud)

因此,答案是仅在当前CUDA上下文cudaDeviceSynchronize() 同步所有流

资料来源:Pawel Pomorski,"多GPU上的CUDA"幻灯片.链接在这里.