Run a MATLAB script that performs a regression test comparing the computation time of a non-GPU-based computation with that of a GPU-based computation.
MATLAB Script
% gpu-bench.m
for i = 300:100:700
fprintf('i = %d\n', i);
% Generates a random matrix X of size 1000i by 800 and a vector Y of size 1000i by 1
X = random('unif', 0, 1, 1000*i, 800);
Beta = [1:801]';
allones = ones(1000*i, 1);
% Add a constant term
X = [allones, X];
Y = X * Beta + random('norm', 0, 1, 1000*i, 1);
% Non-GPU computation
disp('Time for non-GPU:');
tic;
Bestimate = inv(X'*X) * (X'*Y);
Bestimate = inv(X'*X) * (X'*Y);
Bestimate = inv(X'*X) * (X'*Y);
Bestimate = inv(X'*X) * (X'*Y);
toc;
% Now do it on the GPU
% Copy the data to the GPU and create gX and gY in the GPU
disp('Load time for GPU:');
tic;
gX = gpuArray(X);
gY = gpuArray(Y);
gBestimate = gpuArray(zeros(801, 1));
toc;
% GPU computation
disp('Time for GPU compute:');
tic;
gBestimate = inv(gX'*gX) * (gX'*gY);
gBestimate = inv(gX'*gX) * (gX'*gY);
gBestimate = inv(gX'*gX) * (gX'*gY);
gBestimate = inv(gX'*gX) * (gX'*gY);
toc;
% Copy estimates back
disp('Copy estimates back:');
tic;
Bestimate = gather(gBestimate);
toc;
end
The script generates a random matrix X of size 1000i by 800 and a vector Y of size 1000i by 1.
It then adds a column of ones to X to account for the intercept term in the regression model.
Next, it computes the regression coefficients (Bestimate) using the ordinary least squares (OLS) method for the non-GPU-based computation, and it computes the regression coefficients (gBestimate) using the OLS method on the GPU.
Slurm Script
#!/bin/bash
#
# [ gpu-bench.sbatch ]
#SBATCH --job-name=gpu-bench
#SBATCH --output=gpu-bench.%j.out
#SBATCH --export=ALL
#SBATCH --gpu-bind=closest
#SBATCH --time=00:9:00
#SBATCH --mem=32G
#SBATCH --mail-type=END,FAIL
#SBATCH --partition=gpu
#
# `--partition=gpu` specifies that we want to run it on the GPU node
#
# This script runs a MATLAB program saving the output into a files called:
# gpu-bench_#.out, where the # represents the jobID. See below.
#
#
#
# You SUBMIT this 'gpu-bench.sbatch' script to the grid with the sbatch command:
#
#
# sbatch gpu-bench.sbatch
#
# Specify the software and version to use, and run the MATLAB program:
echo "Running on node: `hostname`"
echo ""
module purge
module load matlab/2019a
matlab -nojvm < gpu-bench.m
You can run the following command to execute the script:
sbatch gpu-bench.sbatch