cho.sh
Check if CUDA is available

Check if CUDA is available

Warning

Over a year old. May be outdated

How to open a Jupyter Notebook running on the remote server, and how to point TensorFlow or PyTorch at one GPU.

Opening the Notebook

You need SSH access to the server, your server username (e.g., user@server), and the VPN if the server asks for it.

  1. Open a terminal. PowerShell or PuTTY on Windows, the Terminal app on macOS or Linux.
  2. SSH in with a port forward. Put your username in place of user and your server in place of server.
    text
    ssh -L 8080:localhost:8080 user@server
  3. cd into your work directory.
    text
    cd path/to/work/directory
  4. Start Jupyter and copy the URL it prints.
    text
    jupyter lab --no-browser --port=8080
  5. Paste that URL into the browser on your own machine.

Picking a GPU

The server has GPUs numbered 0 to 7. Check which ones are free first. In Jupyter, run:

python
!nvidia-smi

Then set it up for your framework.

TensorFlow

You need TensorFlow and your other packages installed. After loading your packages, pick the GPU:

python
import os
import tensorflow as tf

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # Choose an available GPU

physical_devices = tf.config.list_physical_devices('GPU')
print("Num GPUs Available: ", len(physical_devices))

if physical_devices:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)

CUDA_VISIBLE_DEVICES is what pins TensorFlow to that GPU.

PyTorch

You need PyTorch installed. It uses available GPUs on its own, but you can pick one:

python
import torch

# Check if CUDA is available
if torch.cuda.is_available():
    device = torch.device("cuda:0")  # Replace 0 with your GPU number
    print("Using GPU:", torch.cuda.get_device_name(0))
else:
    device = torch.device("cpu")
    print("Using CPU")

Then move your tensors or models onto it with device:

python
model.to(device)

Always check what's free and what's in use before you grab a GPU.

Backlinks4