Skip to content

Instantly share code, notes, and snippets.

@adanner
Last active July 15, 2016 01:04
Show Gist options
  • Select an option

  • Save adanner/046a3a2b81b37df914611395b4c654b7 to your computer and use it in GitHub Desktop.

Select an option

Save adanner/046a3a2b81b37df914611395b4c654b7 to your computer and use it in GitHub Desktop.
Snippet to find number of GPUs on system and pick one for CUDA use
/* get number of GPUs on local host */
int getGPUCount(){
int ans=0;
if(cudaGetDeviceCount(&ans) != cudaSuccess){
return 0;
}
return ans;
}
/* for multithreaded or mpi tasks, pick a GPU
* without conflict. Return unique GPU id for
* a given rank, or -1 if error
* rank: MPI task or thread ID
* ngpus: number of GPUs on local host
*/
int pickGPU(int rank, int ngpus){
int gpuID = rank%ngpus; //any better way?
if(cudaSetDevice(gpuID) != cudaSuccess){
return -1;
}
return gpuID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment