【问题标题】:Can different threads set different GPUs as their current CUDA device?不同的线程可以将不同的 GPU 设置为当前的 CUDA 设备吗?
【发布时间】:2016-02-10 09:06:56
【问题描述】:

例如,我有 2 个 GPU 和 2 个主机线程。我无法检查它,因为 multigpu PC 离我很远。我想让第一个主机线程与第一个 GPU 一起工作,第二个主机线程与第二个 GPU 一起工作。所有主机线程都由许多 cublas 调用组成。那么是否可以通过 cudaSetDevice() 调用从第一个主机线程中选择第一个 GPU,从第二个主机线程中选择第二个 GPU?

例如,对于第二个宿主线程,我将调用cudaSetDevice(1),对于第一个线程,我将调用cudaSetDevice(0)

【问题讨论】:

    标签: multithreading cuda gpu multi-gpu


    【解决方案1】:

    那么是否可以通过 cudaSetDevice() 调用从第一个主机线程中选择第一个 GPU 并从第二个主机线程中选择第二个 GPU?

    是的,这是可能的。 cudaOpenMPsample code 中给出了此类用法的示例,(摘录​​):

    ....
    omp_set_num_threads(num_gpus);  // create as many CPU threads as there are CUDA devices
    //omp_set_num_threads(2*num_gpus);// create twice as many CPU threads as there are CUDA devices
    #pragma omp parallel
    {
        unsigned int cpu_thread_id = omp_get_thread_num();
        unsigned int num_cpu_threads = omp_get_num_threads();
    
        // set and check the CUDA device for this CPU thread
        int gpu_id = -1;
    --> checkCudaErrors(cudaSetDevice(cpu_thread_id % num_gpus));   // "% num_gpus" allows more CPU threads than GPU devices
        ...,
    

    【讨论】:

    • 我怎样才能得到一个无符号整数与我的 CPU 上的线程静态关联?如果我调用 GetCurrentProcessorNumber() 我会得到非常数。
    • 这取决于您使用的线程模型。如果您使用 OpenMP,omp_get_thread_num() 将从 0..(#threads-1) 返回一个唯一 ID,用于 OMP 知道的每个线程。
    猜你喜欢
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2023-02-16
    • 2022-11-11
    相关资源
    最近更新 更多