About Open MP and cudaSetDevice()

About Open MP and cudaSetDevice()

本文关键字:cudaSetDevice and Open MP About      更新时间:2023-10-16

有人知道以下cudaSetDevice的用法是否正确吗?我想在任何时间、任何主机线程中重复调用在不同设备上创建的资源;在CUDA有办法做到这一点吗?

 cudaSetDevice(0);
 /...create cuda streams and do some memory allocation on gpu.../
 cudaSetDevice(1);
 /...create cuda streams and do some memory allocation on gpu.../
 #pragma omp parallel num_threads(2)
 { 
   int omp_threadID=omp_get_thread_num();
    ....
   if (omp_threadID==0)
   {
    cudaSetDevice(0);
    /...calling streams/memory created on device 0.../
   }
   else
   {
    cudaSetDevice(1);
    /...calling streams/memory created on device 1.../
    }; 
  };

是的,这样的东西应该可以工作。请确保您在设备0上创建的所有东西都只在OpenMP线程0中使用,设备1和线程1也是如此。

您可能还想查看CUDA OpenMP示例代码,该代码演示了如何使用OpenMP线程分别管理单个设备。