Module CUDA
Interactive Job
Open an interactive session to a compute node. Adjust the number of GPUs (--gpus 1) and the amount of memory (--mem 4G) needed for your job. Increase the number of --gpus only when using code that can use multiple GPUs.
srun --partition gpu --gpus 1 --mem 4G --pty bashLoad the CUDA module:
module load cuda/12.3Edit your CUDA code if need be. Here is an example program to print out some information about the CUDA system:
Compile the CUDA code:
nvcc query_devices.cu -o query_devicesRun the CUDA program you just built:
./query_devicesWhen the job finishes, you can check the output, and leave the compute node if you're ready:
[user@athena1:/lustre/home/user/CUDA]$ srun --partition gpu --gpus 1 --mem 4G --pty bash ## start on the head node
[user@athena501:/lustre/home/user/CUDA]$ module load cuda/12.3 ## now on a compute node
[user@athena501:/lustre/home/user/CUDA]$ nvcc query_devices.cu -o query_devices
[user@athena501:/lustre/home/user/CUDA]$ ./query_devices
There is 1 CUDA capable device in this system...
Device Properties:
CUDA Device Properties:
Name: Tesla P100-PCIE-12GB.
Global Memory: 12786073600 bytes.
Max Shared Memory Per Block: 49152 bytes.
# 32-bit Registers Per Block: 65536.
Number of Threads in a Warp: 32.
Max Pitch for Memory Copies: 2147483647 bytes.
Max Number of Threads in a Block: 1024.
Max Number of Threads Each Dimension of a Block: (1024, 1024, 64).
Number of Blocks Allowed Each Dimension of a Grid: (2147483647, 65535, 65535).
Available Constant Memory: 65536 bytes.
Major Revision of Device's Compute Capability: 6.
Minor Revision of Device's Compute Capability: 0.
Device Clock Rate: 1328500.
Device's Requirement for Texture Alignment: 512.
Device Can Simultaneously Do cudaMemcpy() and Kernel Execution...? True.
Number of Multiprocessors on Device: 56.
Runtime Limit for Kernel Execution...? False.
Integrated GPU, Or Discrete Device...? Discrete.
Device Can Map Host Memory onto CUDA Device Address Space? True.
Device's Computing Mode: 0.
Multiple threads can use cudaSetDevice() with this device
Max Size Supported for 1D Textures: 131072.
Max Dimensions Supported for 2D Textures: (131072, 65536).
Max Dimensions Supported for 3D Textures: (16384, 16384, 16384).
Device Supports Executing Multiple Kernels Within the Same Context, Simultaneously? True.
[user@athena501:/lustre/home/user/CUDA]$ exit ## leave the compute node
[user@athena1:/lustre/home/user/CUDA]$ ## now back on the head nodeBatch Job
Prepare your CUDA code. For this example here is a CUDA program that adds two arrays of 1 million floating point values each.
This can be compiled just like the interactive job above, or the following Slurm job script can be used to build it. Whichever method you use, take note of the machine where your CUDA code is built, as it may need to run on that machine afterwards. If so, then you will have to list that machine in the Slurm job script as the node to run the job on, using "#SBATCH --nodelist=athena511" for example. Here is a Slurm job script to build the add_cuda.cu code:
CUDA_compile_add.sh
#!/bin/bash
#SBATCH --job-name Compile_add_cuda
#SBATCH --output CUDA_compile_job_output.log
#SBATCH --error CUDA_compile_job_error.log
#SBATCH --partition=gpu
#SBATCH --gpus=1
#SBATCH --mem=4G
module purge
module load cuda/12.3
nvcc add_cuda.cu -o add_cuda
# get name of node where code was built:
echo "Built on `hostname`."Submit the job to Slurm to build the CUDA program:
sbatch CUDA_compile_add.shMonitor the execution of your job in the Slurm queue:
squeue -u $USERThe output file can be checked to see on which node the code was built:
cat CUDA_compile_job_output.log
Built on athena511.When that finishes successfully, then a Slurm job script to run the compiled code can be used:
CUDA_slurm_script_add_arrays.sh
#!/bin/bash
#SBATCH --job-name CUDA_add_arrays
#SBATCH --output CUDA_add_job_output.log
#SBATCH --error CUDA_add_job_error.log
#SBATCH --partition=gpu
#SBATCH --gpus=1
#SBATCH --mem=4G
module purge
module load cuda/12.3
./add_cuda 1000Submit the job to Slurm to run the CUDA program:
sbatch CUDA_slurm_script.shAnd monitor the execution of your job in the Slurm queue:
squeue -u $USERThe job may show up in the queue as running (R) or pending (PD). When the job completes, it will disappear from the queue and you can check the output. (it should contain rows of "3"s, 40 per row, and the Max Error value 0.)
[user@athena1 ~]$ sbatch CUDA_compile_add.sh
Submitted batch job 1848
[user@athena1 ~]$ cat CUDA_compile_job_output.log
Built on athena501.
[user@athena1 ~]$ sbatch CUDA_slurm_script.sh
Submitted batch job 1849
[user@athena1 ~]$ squeue -u $USER
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) ## JOBID 1849 completed and exited the queue
[user@athena1 ~]$ tail CUDA_add_job_output.log
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 }
Max error: 0
[user@athena1 ~]$ Further Resources
In case you are interested in learning more about CUDA development, here is a very useful link: https://developer.nvidia.com/blog/even-easier-introduction-cuda/ which begins with some of these same examples.
And always feel free to read about the HPRC.