Skip to content

Instantly share code, notes, and snippets.

@Wildcarde
Last active October 11, 2020 02:01
Show Gist options
  • Save Wildcarde/a31ef90344e4cec90a2fd35a29e21bb7 to your computer and use it in GitHub Desktop.
Save Wildcarde/a31ef90344e4cec90a2fd35a29e21bb7 to your computer and use it in GitHub Desktop.
Parallel Matlab Spock Demo
function [] = parbench()
%main function for building pool and running benchmark
% this is adapted from the mathworks parallel benchmark demo
% to occupy only one file instead of several
% original article here: https://www.mathworks.com/help/distcomp/examples/simple-benchmarking-of-parfor-using-blackjack.html
%% Setup
%If you want more than 12 core you MUST create the pool by hand
% Below that it will detect the number of cores requested by and
% create a pool of the appropriate size. If this pool size is too
% large the function will fail and dump an error in your slurm output file.
%raw parpool will cause conflicts between simultaneously running jobs
%parpool('local',4)
%create custom cluster to modify
pc = parcluster('local')
% set working area to local tmp file to avoid collisions
pc.JobStorageLocation = strcat('/tmp/',getenv('USER'),'-',getenv('SLURM_JOB_ID'))
%start customized parallel pool
parpool(pc,4)
p = gcp;
if isempty(p)
error('pctexample:backslashbench:poolClosed', ...
['This example requires a parallel pool. ' ...
'Manually start a pool using the parpool command or set ' ...
'your parallel preferences to automatically start a pool.']);
end
poolSize = p.NumWorkers
numHands = 2000;
numPlayers = 6;
%% Parallel run
fprintf('Simulating each player playing %d hands.\n', numHands);
t1 = zeros(1, poolSize);
for n = 2:poolSize
tic;
pctdemo_aux_parforbench(numHands, n*numPlayers, n);
t1(n) = toc;
fprintf('%d workers simulated %d players in %3.2f seconds.\n', ...
n, n*numPlayers, t1(n));
end
%% Sequential Comparison
tic;
S = zeros(numHands, numPlayers);
for i = 1:numPlayers
S(:, i) = pctdemo_task_blackjack(numHands, 1);
end
t1(1) = toc;
fprintf('Ran in %3.2f seconds using a sequential for-loop.\n', t1(1));
%% Graph the Speedup
speedup = (1:poolSize).*t1(1)./t1;
fig = pctdemo_setup_blackjack(1.0);
fig.Visible = 'on';
ax = axes('parent', fig);
x = plot(ax, 1:poolSize, 1:poolSize, '--', ...
1:poolSize, speedup, 's', 'MarkerFaceColor', 'b');
t = ax.XTick;
t(t ~= round(t)) = []; % Remove all non-integer x-axis ticks.
ax.XTick = t;
legend(x, 'Linear Speedup', 'Measured Speedup', 'Location', 'NorthWest');
xlabel(ax, 'Number of MATLAB workers participating in computations');
ylabel(ax, 'Speedup');
% save speedup to file
set(gcf, 'paperpositionmode', 'auto');
saveas(gcf,'/fastscratch/gmcgrath/speedup','png');
%% Measure Speedup Distribution
numIter = 100;
t2 = zeros(1, numIter);
for i = 1:numIter
tic;
pctdemo_aux_parforbench(numHands, poolSize*numPlayers, poolSize);
t2(i) = toc;
if mod(i,20) == 0
fprintf('Benchmark has run %d out of %d times.\n',i,numIter);
end
end
%% Graph and write out distribution
speedup = t1(1)./t2*poolSize;
clf(fig);
ax = axes('parent', fig);
hist(speedup, 5);
a = axis(ax);
a(4) = 5*ceil(a(4)/5); % Round y-axis to nearest multiple of 5.
axis(ax, a)
xlabel(ax, 'Speedup');
ylabel(ax, 'Frequency');
title(ax, sprintf('Speedup of parfor with %d workers', poolSize));
m = median(speedup);
fprintf(['Median speedup is %3.2f, which corresponds to '...
'efficiency of %3.2f.\n'], m, m/poolSize);
set(gcf, 'paperpositionmode', 'auto');
saveas(gcf,'/fastscratch/gmcgrath/distribution','png');
function S = pctdemo_aux_parforbench(numHands, numPlayers, n)
% create the parallel pool and check number of workers
%c = parcluster('local') % build the local cluster object
%numworkers = c.NumWorkers % get the number of workers in the cluster
%PCTDEMO_AUX_PARFORBENCH Use parfor to play blackjack.
% S = pctdemo_aux_parforbench(numHands, numPlayers, n) plays
% numHands hands of blackjack numPlayers times, and uses no
% more than n MATLAB(R) workers for the computations.
% Copyright 2007-2009 The MathWorks, Inc.
S = zeros(numHands, numPlayers);
parfor (i = 1:numPlayers, n)
S(:, i) = pctdemo_task_blackjack(numHands, 1);
end
#!/usr/bin/env bash
#name job matlabdemo, output to slurm file, use partition all, run for 60 minutes and use 20GB of ram
#SBATCH -J 'matlabdemo'
#SBATCH -o %j.out
#SBATCH -p debug
#SBATCH -t 60
#SBATCH --mem 10240
#SBATCH -c 4
module load matlab/R2015b
#setup local working area for matlab parallel
mkdir -p /tmp/$USER-$SLURM_JOB_ID
#run a paranoid version of matlab that crashes gracefully and records why just incase.
xvfb-run -w 5 -e %j.xvfberr -f %j.xvfbauth matlab -nosplash -nodisplay -nodesktop -r "try; matlabparscript(); catch me; fprintf('%s / %s\n',me.identifier,me.message); end; exit"
#matlab -nosplash -nodisplay -nodesktop -r "try; matlabparscript(); catch me; fprintf('%s / %s\n',me.identifier,me.message); end; exit"
rm -rf /tmp/$USER-$SLURM_JOB_ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment