Last active
July 6, 2026 20:24
-
-
Save pastewka/5546686 to your computer and use it in GitHub Desktop.
Print a list of free CUDA devices on screen. This is useful to select a free GPU in a job submission script.
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
| /* | |
| * cuda_idle.c — print the index of each GPU with no compute process running. | |
| * | |
| * Build: gcc -O2 -Wall -o gpu_idle gpu_idle.c -I/usr/local/cuda/include -lnvidia-ml | |
| * | |
| * MIT License | |
| * | |
| * Copyright (c) 2013-2026 Lars Pastewka | |
| * | |
| * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| * of this software and associated documentation files (the "Software"), to deal | |
| * in the Software without restriction, including without limitation the rights | |
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| * copies of the Software, and to permit persons to whom the Software is | |
| * furnished to do so, subject to the following conditions: | |
| * | |
| * The above copyright notice and this permission notice shall be included in all | |
| * copies or substantial portions of the Software. | |
| * | |
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| * SOFTWARE. | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdbool.h> | |
| #include <nvml.h> | |
| #define NVML_TRY(call) \ | |
| do { \ | |
| nvmlReturn_t rc_ = (call); \ | |
| if (rc_ != NVML_SUCCESS) { \ | |
| fprintf(stderr, "%s:%d: %s failed: %s\n", \ | |
| __FILE__, __LINE__, #call, nvmlErrorString(rc_)); \ | |
| nvmlShutdown(); \ | |
| exit(EXIT_FAILURE); \ | |
| } \ | |
| } while (0) | |
| /* A device is "idle" when no compute process is running on it. We query with | |
| * a zero-length buffer: NVML then reports the count without us allocating | |
| * anything or depending on the (version-dependent) layout of | |
| * nvmlProcessInfo_t. */ | |
| static bool device_is_idle(nvmlDevice_t dev) | |
| { | |
| unsigned int count = 0; | |
| nvmlReturn_t rc = nvmlDeviceGetComputeRunningProcesses(dev, &count, NULL); | |
| if (rc == NVML_SUCCESS) | |
| return count == 0; /* fit in the empty buffer -> none running */ | |
| if (rc == NVML_ERROR_INSUFFICIENT_SIZE) | |
| return false; /* buffer too small -> at least one running */ | |
| fprintf(stderr, "nvmlDeviceGetComputeRunningProcesses failed: %s\n", | |
| nvmlErrorString(rc)); | |
| nvmlShutdown(); | |
| exit(EXIT_FAILURE); | |
| } | |
| int main(void) | |
| { | |
| NVML_TRY(nvmlInit()); | |
| unsigned int count = 0; | |
| NVML_TRY(nvmlDeviceGetCount(&count)); | |
| for (unsigned int i = 0; i < count; i++) { | |
| nvmlDevice_t dev; | |
| NVML_TRY(nvmlDeviceGetHandleByIndex(i, &dev)); | |
| if (device_is_idle(dev)) | |
| printf("%u\n", i); | |
| } | |
| NVML_TRY(nvmlShutdown()); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment