{"version":"1.0","type":"rich","title":"Snippet to find number of GPUs on system and pick one for CUDA use","description":"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.","provider_name":"VizHub AI Studio","provider_url":"https://next.vizhub.com","html":"<iframe src=\"https://next.vizhub.com/viz/gist-046a3a2b81b37df914611395b4c654b7?mode=embed\" width=\"960\" height=\"500\" scrolling=\"no\" frameborder=\"1\"></iframe>","width":960,"height":500,"cache_age":3600}