Enumerating and Selecting Devices
cudaSetDevice and per-GPU contexts.
Enumerating and Selecting Devices is a free CUDA Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
More Than One GPU
A single machine can hold several GPUs. To use them all, your program first needs to discover how many devices are present before it sends any work.
Counting the Devices
One call tells you how many GPUs the runtime can see. cudaGetDeviceCount writes that number into an int you hand it.
int count;
cudaGetDeviceCount(&count);Devices Are Numbered
Each GPU gets an integer id from 0 up to count minus one. That device id is how you point the runtime at one specific card.
Picking the Active Device
You choose which GPU your next calls target with cudaSetDevice. After this, allocations and launches go to that card.
cudaSetDevice(1);There Is a Current Device
At any moment exactly one GPU is the current device for the calling thread. Every cudaMalloc or kernel launch lands on whatever device is current.
Inspecting a Device
Before trusting a GPU you can read its specs. cudaGetDeviceProperties fills a struct with name, memory size, and compute capability.
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);Reading the Properties
The properties struct is rich. Fields like name and totalGlobalMem help you pick the best card or skip one that is too small.
Each Device Owns Its Memory
A pointer from cudaMalloc belongs to whichever GPU was current then. Using it while another device is current is an error waiting to happen.
Per-Device Contexts
Behind each GPU sits a context that holds its allocations and streams. Switching the current device switches you into that device's context.
Looping Over All GPUs
A common pattern is a loop that calls cudaSetDevice for each id, then does setup on that card. This is how you spread work across every device.
for (int d = 0; d < count; d++) {
cudaSetDevice(d);
}Restore Before You Leave
If a helper changes the current device, switch it back when done. Leaving the current device changed can surprise the rest of your code.
Quick Check
Recall which call chooses the GPU your next allocations and launches will use.
Recap
You count GPUs, pick one with cudaSetDevice, and inspect it with properties. Each device owns its own memory and context. Next: splitting work across them. ✨
Frequently asked questions
Is the “Enumerating and Selecting Devices” lesson free?
Yes — the full text of “Enumerating and Selecting Devices” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “Enumerating and Selecting Devices”?
cudaSetDevice and per-GPU contexts. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Enumerating and Selecting Devices” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Enumerating and Selecting Devices
- Partitioning Work Across GPUs
- Peer-to-Peer Memory Access
- Multi-GPU with NCCL