Module CUDA

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 bash

Load the CUDA module:

module load cuda/12.3

Edit your CUDA code if need be.  Here is an example program to print out some information about the CUDA system:

/******************************************************************* compile with nvcc query_devices.cu -o query_devices *******************************************************************/ // for IO #include <stdio.h> // for exit #include <stdlib.h> void print_cd_props(cudaDeviceProp); void handle_error(cudaError); int main(void) { int count = 0; cudaDeviceProp prop; // get number of CUDA devices in system: handle_error(cudaGetDeviceCount(&count)); if (1 == count) { printf("There is %d CUDA capable device in this system...\n", count); } else { printf("There are %d CUDA capable devices in this system...\n", count); } puts("Device Properties:"); for (int i=0; i<count; i++) { handle_error(cudaGetDeviceProperties(&prop, i)); print_cd_props(prop); } // do this to guarantee whatever called // program understands exit value... exit(EXIT_SUCCESS); } /************************************************************** Based on info pgs. 28-31, "CUDA by Example" **************************************************************/ void print_cd_props(cudaDeviceProp prop) { puts("CUDA Device Properties:"); printf("\tName: %s.\n", prop.name); // may need %lu for some compiler... printf("\tGlobal Memory: %zu bytes.\n", prop.totalGlobalMem); printf("\tMax Shared Memory Per Block: %zu bytes.\n", prop.sharedMemPerBlock); printf("\t# 32-bit Registers Per Block: %d.\n", prop.regsPerBlock); printf("\tNumber of Threads in a Warp: %d.\n", prop.warpSize); printf("\tMax Pitch for Memory Copies: %zu bytes.\n", prop.memPitch); printf("\tMax Number of Threads in a Block: %d.\n", prop.maxThreadsPerBlock); printf("\tMax Number of Threads Each Dimension of a Block: " "(%d, %d, %d).\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]); printf("\tNumber of Blocks Allowed Each Dimension of a Grid: " "(%d, %d, %d).\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]); printf("\tAvailable Constant Memory: %zu bytes.\n", prop.totalConstMem); printf("\tMajor Revision of Device's Compute Capability: %d.\n", prop.major); printf("\tMinor Revision of Device's Compute Capability: %d.\n", prop.minor); printf("\tDevice Clock Rate: %d.\n", prop.clockRate); printf("\tDevice's Requirement for Texture Alignment: %zu.\n", prop.textureAlignment); printf("\tDevice Can Simultaneously Do cudaMemcpy() and Kernel " "Execution...? %s.\n", ((0 == prop.deviceOverlap) ? "False" : "True")); printf("\tNumber of Multiprocessors on Device: %d.\n", prop.multiProcessorCount); printf("\tRuntime Limit for Kernel Execution...? %s.\n", ((0 == prop.kernelExecTimeoutEnabled) ? "False" : "True")); printf("\tIntegrated GPU, Or Discrete Device...? %s.\n", ((0 == prop.integrated) ? "Discrete" : "Integrated")); printf("\tDevice Can Map Host Memory onto CUDA Device Address Space?" " %s.\n", ((0 == prop.canMapHostMemory) ? "False" : "True")); printf("\tDevice's Computing Mode: %d.\n", prop.computeMode); // Enumerator info from Nvidia Developer site... if (prop.computeMode == cudaComputeModeDefault) { printf("\t\tMultiple threads can use cudaSetDevice() " "with this device\n"); } else if (prop.computeMode == cudaComputeModeExclusive) { printf("\t\tOnly one thread can use cudaSetDevice() " "with this device\n"); } else if (prop.computeMode == cudaComputeModeProhibited) { printf("\t\tNo threads can use cudaSetDevice() " "with this device\n"); } else { printf("\t\tNo idea what that value means...!\n"); } printf("\tMax Size Supported for 1D Textures: %d.\n", prop.maxTexture1D); printf("\tMax Dimensions Supported for 2D Textures: (" "%d, %d).\n", prop.maxTexture2D[0], prop.maxTexture2D[1]); printf("\tMax Dimensions Supported for 3D Textures: (" "%d, %d, %d).\n", prop.maxTexture3D[0], prop.maxTexture3D[1], prop.maxTexture3D[2]); printf("\tDevice Supports Executing Multiple Kernels Within the " "Same Context, Simultaneously? %s.\n", ((0 == prop.concurrentKernels) ? "False" : "True")); } //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// void handle_error(cudaError ce) { if (0 != ce) { puts("Uh-oh, a CUDA error occurred! Exiting program..."); printf("\tCUDA error code %d...\n", ce); exit(EXIT_SUCCESS); } } ////////////////////////////////////////////////////////////////

Compile the CUDA code:

nvcc query_devices.cu -o query_devices

Run the CUDA program you just built:

./query_devices

When 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 node

Batch Job

Prepare your CUDA code.  For this example here is a CUDA program that adds two arrays of 1 million floating point values each.

/************************************************************* compile with nvcc add_cuda.cu -o add_cuda *************************************************************/ #include <iostream> #include <math.h> using namespace std; // function to add the elements of two arrays __global__ // tells CUDA compiler this runs on the GPU // but want kernel to spread the computation across parallel threads: void add(int n, float* x, float* y) { int index = threadIdx.x; int stride = blockDim.x; for (int i(index); i<n; i+=stride) { y[i] = x[i] + y[i]; } } int main(int argc, char** argv) { int N = 1000000; cout << "N == " << N << endl; if (argc < 2) { cout << "Need # threads > 0!!!" << endl; exit(EXIT_FAILURE); } // use GPU memory: float *x, *y; cudaMallocManaged(&x, N*sizeof(float)); cudaMallocManaged(&y, N*sizeof(float)); int numths = atoi(argv[1]); if (numths <= 0) { cout << "Need # threads > 0!!!" << endl; exit(EXIT_FAILURE); } // initialize x and y arrays on the host for (int i = 0; i < N; i++) { x[i] = 1.0f; y[i] = 2.0f; } int cntr = 0; cout << "On host, x == { " << endl; for (int i = 0; i < N; i++) { if ((0 != i) && (i % 40 == 0)) { cout << endl; } if (i < N-1) { cout << x[i] << ","; } else { cout << x[i] << " }" << endl; } } cout << "On host, y == { " << endl; for (int i = 0; i < N; i++) { if ((0 != i) && (i % 40 == 0)) { cout << endl; } if (i < N-1) { cout << y[i] << ","; } else { cout << y[i] << " }" << endl; } } // Run kernel on 1M elements on the __GPU__! add<<<1, numths>>>(N, x, y); // WAIT for GPU to finish before accessing on host: cudaDeviceSynchronize(); cout << "On host, after call to kernel, x == { " << endl; for (int i = 0; i < N; i++) { if ((0 != i) && (i % 40 == 0)) { cout << endl; } if (i < N-1) { cout << x[i] << ","; } else { cout << x[i] << " }" << endl; } } cout << "On host, after call to kernel, y == { " << endl; for (int i = 0; i < N; i++) { if ((0 != i) && (i % 40 == 0)) { cout << endl; } if (i < N-1) { cout << y[i] << ","; } else { cout << y[i] << " }" << endl; } } // Check for errors (all values should be 3.0f) float maxError = 0.0f; for (int i = 0; i < N; i++) { maxError = fmax(maxError, fabs(y[i]-3.0f)); if ((maxError > 0.0f) || (maxError < 0.0f)) { cout << "Uh-oh maxError == " << maxError << "..." << endl; } } std::cout << "Max error: " << maxError << std::endl; // Free memory cudaFree(x); cudaFree(y); return 0; }

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.sh

Monitor the execution of your job in the Slurm queue:

squeue -u $USER

The 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 1000

Submit the job to Slurm to run the CUDA program:

sbatch CUDA_slurm_script.sh

And monitor the execution of your job in the Slurm queue:

squeue -u $USER

The 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.