MATLAB has some built-in routines that can take advantage of a GPU. The sample code below performs a matrix decomposition using MATLAB GPU functions.
gpu = gpuDevice();
fprintf('Using a %s GPU.\n', gpu.Name);
disp(gpuDevice);
X = gpuArray([1 0 2; -1 5 0; 0 3 -9]);
whos X;
[U,S,V] = svd(X)
fprintf('trace(S): %f\n', trace(S))
quit;
Save the code below as svd.m
.
We will now use the following SLURM script matlab-gpu.sh
to run the code:
#!/bin/bash
#SBATCH --job-name="Matlab-GPU-Demo" # job name
#SBATCH --partition=peregrine-gpu # partition to which job should be submitted
#SBATCH --qos=gpu_debug # qos type
#SBATCH --nodes=1 # node count
#SBATCH --ntasks=1 # total number of tasks across all nodes
#SBATCH --cpus-per-task=1 # cpu-cores per task
#SBATCH --mem=4G # total memory per node
#SBATCH --gres=gpu:nvidia_a100_3g.39gb:1 # Request 1 GPU (A100 40GB)
#SBATCH --time=00:05:00 # wall time
module purge
module load matlab
matlab -singleCompThread -nodisplay -nosplash -r svd
Submit the job as
sbatch matlab-gpu.sh
The result will be saved in a file named slurm-####.out
and should look like
< M A T L A B (R) >
Copyright 1984-2022 The MathWorks, Inc.
R2022a (9.12.0.1884302) 64-bit (glnxa64)
February 16, 2022
To get started, type doc.
For product information, visit www.mathworks.com.
Using a NVIDIA A100-SXM4-80GB MIG 3g.40gb GPU.
CUDADevice with properties:
Name: 'NVIDIA A100-SXM4-80GB MIG 3g.40gb'
Index: 1
ComputeCapability: '8.0'
SupportsDouble: 1
DriverVersion: 11.7000
ToolkitVersion: 11.2000
-----------------------------------------------------------
-------------------TRUNCATED-------------------------------
-----------------------------------------------------------
V =
0.0403 0.1761 -0.9835
-0.3974 -0.9003 -0.1775
0.9168 -0.3980 -0.0337
trace(S): 15.718392