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.
See folder 30-gpu-examples from the examples for reference.
% 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.
#!/bin/bash
#
# [ gpu-bench.sbatch ]
# This script runs a MATLAB program that compares the time for a regression
# calculation on the CPU with the same calculation on a GPU. The output is
# written to gpu-bench.<jobID>.out.
#
# Submit it with:
# sbatch gpu-bench.sbatch
#SBATCH --job-name=gpu-bench
#SBATCH --partition=gpu
#SBATCH --gpu-bind=closest
#SBATCH --mem=32G
#SBATCH --time=00:09:00
#SBATCH --output=gpu-bench.%j.out # output file; %j = job ID
#SBATCH --mail-type=END,FAIL
#SBATCH --mail-user=netID@stern.nyu.edu
echo "Running on node: `hostname`"
echo ""
module purge
module load matlab/2023a
matlab -nojvm < gpu-bench.m
You can run the following command to execute the script:
sbatch gpu-bench.sbatch
When the job no longer appears in squeue, it has finished. Replace <jobid> with the number sbatch printed when you submitted the job:
more gpu-bench.<jobid>.out