有谁知道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 Sample Code,它演示了如何使用OpenMP线程来分别管理单个设备。

关于c++ - 关于Open MP和cudaSetDevice(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17602855/

10-11 18:25