How to prepare and run a spmd batch job

When drange (or other jobs that require spmd) is used in parallel computing, it requires that matlabpool be used to allocate resources along with the Single Program Multiple Data (spmd) to rpovide a parallel environment. If the SCV's recommended procedure is used, the opening and closing of matlabpool for resource allocation is part of the user's application m-file. On the contrary, if you prefer to use the batch procedure as described in the PCT's User Guide, then the resource allocation procedure is not needed in the user m-file. Instead, a batch utility function createParallelJob must be called to create the matlabpool environment for your drange applications. A drange example is included below to demonstrate the details involved in preparing and submitting a batch job that require spmd.

A similar batch procedure for parallel jobs using spmd is also available.

function [x,y] = spmdExample(n)
%function [x,y] = spmdExample(n)
% Solves system Ax=b as a parallel (spmd) job.
% Job submitted via myparalleljob.m

c = ones(n,1);   % a vector used to generate solution
C = codistributed(c, 'convert');
M = rand(n, codistributor());
[A, x, b] = linearSystem(M, C, 2);
y = Ab;            % solves Ay = b; y should equal x

You need to prepare a MATLAB batch script, myparalleljob.m, to run the parforExample.m in batch. The PCT provides built-in functions for users to create tasks specifically for invoking matlabpool distributed or parallel jobs. With task created for matlabpool, you will NOT need to explicitly open and close matlabpool in your own application m-file, in this case the parforExample.m file.

% Construct a parallel job object.
j = createParallelJob('Name', 'testparalleljob', 'Configuration', 'SGE');
% Add the task to the job.
% Returns 1 output item, drange loop upper limit is 4
createTask(j, @spmdExample, 2, {4})
% Set the number of workers required for parallel execution.
j.MinimumNumberOfWorkers = 2;
j.MaximumNumberOfWorkers = 4;
% Run the job.
submit(j);
% Wait until the job is finished.
waitForState(j, 'finished');
% Retrieve job results.
out = getAllOutputArguments(j);
% Display the output.
celldisp(out);
% Destroy the job.
destroy(j);

With the above two files prepared, you can use either of the below two methods to submit the spmdExample job to batch.

  • Run from MATLAB client
    >> myparalleljob
    
    
  • Run as background job at the system prompt
    katana% mbatch myparalleljob parallel_out