那么是否可以通过 cudaSetDevice() 调用从第一个主机线程中选择第一个 GPU,从第二个主机线程中选择第二个 GPU?
是的,这是可能的。cudaOpenMP
示例代码中给出了此类用法的示例(摘录):
....
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
...,