Skip to main content

autocvd, a tool for setting CUDA_VISIBLE_DEVICES based on GPU utilization

On a system with multiple NVIDIA GPUs, autocvd eliminates the need for manually specifying the CUDA_VISIBLE_DEVICES environment variable. This comes in especially handy on systems with multiple users, like a shared GPU server. It is dependency-free and requires no code changes.

To execute a command on a single free GPU, run

$ eval $(autocvd) <command>

This will select a free GPU (or wait if none is available) and run the command with the appropriate environment variables set.

For ease of use you might want to define an alias in your .bashrc, e.g., to run a Python script on a free GPU:

$ alias gpupython="eval $(autocvd) python"

Examples

# run command on two free GPUs
$ eval $(autocvd -n 2) <command>

# run command on least-used GPU (i.e., do not wait if no GPU is free)
$ eval $(autocvd -l) <command>

# exclude certain GPUs
$ eval $(autocvd -x 0 2) <command>

# if no free GPU is available immediately, wait for 60 seconds only
$ eval $(autocvd -t 60) <command>

# export environment variables into the current shell
$ . <(autocvd -e)  # alternatively: source <(autocvd -e)

Requirements

autocvd uses nvidia-smi to query GPU utilization. Make sure that it is installed and callable.

Installation

pip install autocvd

Usage

usage: autocvd [-h] [-n NUM_GPUS] [-l] [-t TIMEOUT] [-i INTERVAL] [-e] [-o] [-q]

A tool for setting CUDA_VISIBLE_DEVICES based on utilization. Basic usage: eval $(autocvd) <command>

optional arguments:
  -h, --help            show this help message and exit
  -n NUM_GPUS, --num-gpus NUM_GPUS
                        Number of required GPUs. Defaults to 1.
  -l, --least-used      Select least-used GPUs instead of waiting for free GPUs. Defaults to False.
  -x EXCLUDE [EXCLUDE ...], --exclude EXCLUDE [EXCLUDE ...]
                        One or multiple GPUs (separated by space) to be excluded. Defaults to no GPU being excluded.
  -t TIMEOUT, --timeout TIMEOUT
                        Timeout for waiting in seconds. Defaults to no timeout.
  -i INTERVAL, --interval INTERVAL
                        Interval to query GPU status in seconds. Defaults to 30.
  -e, --export          Add 'export' statements such that environment can be sourced.
  -o, --id-only         Return comma-separated GPU IDs only instead of environment variable assignment.
  -q, --quiet           Do not print any messages. Defaults to False.

autocvd can also be used to set the environment variables from a Python script itself:

from autocvd import autocvd


autocvd()

# code accessing GPUs

Note that some packages read CUDA_VISIBLE_DEVICES when being imported, which makes it necessary to call autocvd before importing it.

Notes

  • Besides setting CUDA_VISIBLE_DEVICES, autocvd also sets CUDA_DEVICE_ORDER=PCI_BUS_ID. This is required to ensure that the ordering of CUDA devices is consistent.

Metadata

Release files for autocvd 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for autocvd 0.2.1
File Size Uploaded
autocvd-0.2.1.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for autocvd 0.2.1
File Interpreter ABI Platform
autocvd-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / autocvd-0.2.1.tar.gz

Download URL autocvd-0.2.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
56eb834ff775a4d94a6d1708471fa5a52349abaff39ac4979662f36ee0d762bb
BLAKE2b-256 checksum
How to use checksums
3f4435786d8e810051b3970dbb4bf9bf192f330cfef8408e0769ec55d979884b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / autocvd-0.2.1-py3-none-any.whl

Download URL autocvd-0.2.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb5f73fd18eee0b1b7fc36b778fb7acda179305bba1b0ad25bf93e42b2422af8
BLAKE2b-256 checksum
How to use checksums
007cf9ef793292e4e28d9b1149ca5948571d6a92d60c650e4578a9851d123fb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page