Skip to content

Instantly share code, notes, and snippets.

@pastewka
Last active December 26, 2015 19:48
Show Gist options
  • Save pastewka/5546686 to your computer and use it in GitHub Desktop.
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.
/* ======================================================================
Copyright (2013) Lars Pastewka
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
====================================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <nvml.h>
#define NVML(x) { nvmlReturn_t ret = x; if (ret != NVML_SUCCESS) { printf("NVML error in line %i: %s\n", __LINE__, nvmlErrorString(ret)); \
exit(8); } }
int main(int argc, char *argv)
{
unsigned int i, n;
NVML( nvmlInit() );
NVML( nvmlDeviceGetCount(&n) );
if (n < 0 || n > 32) {
printf("nvmlDeviceGetCount returned %i. This does not make sense.\n", n);
exit(9);
}
for (i = 0; i < n; i++) {
nvmlDevice_t dev;
unsigned int num_procs = 32;
nvmlProcessInfo_t procs[32];
NVML( nvmlDeviceGetHandleByIndex(i, &dev) );
NVML( nvmlDeviceGetComputeRunningProcesses(dev, &num_procs, procs) );
if (num_procs < 1) {
printf("%i\n", i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment