Last active
July 15, 2016 01:04
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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