Instructor note

Total: 75min (Teaching:45Min | Discussion:0min | Breaks:0min | Exercises:30Min)


  • Objectives

    • Recruit GPUs in a jobs.

PyTorch example to demonstrate how to use GPUs

Not all software uses GPUs, just because there are available GPUs. Inorder to demonstrate the use of GPUs we need to run a small software that can utilise them. PyTorch is an example of a package that we can use to write code that would utilise GPUs, if they are available.


import torch

# Check if CUDA is available
if torch.cuda.is_available():  
    num_gpu = torch.cuda.device_count()

    for i in range(num_gpu):
        print(f'GPU {i+1}:', torch.cuda.get_device_name(i))
    print('No GPU available, using the CPU.')

Call the program from the job script


 #SBATCH --nodes=1
 #SBATCH --time=00:10:00
 #SBATCH --mem=8G
 #SBATCH --partition=accel
 #SBATCH --mem=8G
 #SBATCH --gpus=1

 module purge
 module load PyTorch/1.10.0-foss-2021a-CUDA-11.3.1

 python > ${SLURM_JOB_UID}.out