Snippet to find number of GPUs on system and pick one for CUDA use
✓ Published0🌍 Public
Aadanner
Last edited Jul 15, 2016
Created on Jul 14, 2016
This snippet demonstrates how to query the local system for the number of available CUDA-enabled GPUs and assign one to a specific thread or MPI rank. It shows a two-step workflow: first, `getGPUCount()` uses `cudaGetDeviceCount` to retrieve the GPU count, returning zero on failure; second, `pickGPU()` maps a rank to a GPU via modulo arithmetic and activates it with `cudaSetDevice`, returning -1 if the assignment fails. The code relies on the CUDA runtime API and is intended for multithreaded or MPI environments to avoid device conflicts.
AI-generated description