How to prepare and run a matlabpool batch job

When parfor is used in parallel computing, it requires that matlabpool be used to allocate resources and create the appropriate computing environment for which it needs. If the SCV’s recommended procedure is used, the opening and closing of matlabpool for resource allocation must be incorporated in 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 separate batch script (an m-file) is responsible for invoking the utility function createMatlabPoolJob to create the matlabpool environment. A parfor example is included below to demonstrate the details involved in preparing and submitting a batch job that require matlabpool for resource allocation.

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

% parforExample.m
function [x, y] = parforExample(n)
%function [x, y] = parforExample(n)
% parfor example running in batch through the use of
% the createMatlabPoolJob function
% n -- (input) parfor loop upper limit
% x -- (output) array
% y -- (output) scalar; y = 0 verifies that matlabpool is in effect

y = 0;   % use y to test if matlabpool is invoked
parfor i=1:n
  y = i*2;
  x(i) = sin(i*2*pi/n);
end
% if y remains 0, matlabpool is in effect; otherwise y=2*n

You need to prepare a MATLAB batch script, mymatlabpooljob.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 MATLABPOOL job object.
j = createMatlabPoolJob('Name', 'testmatlabpooljob', 'Configuration', 'SGE');
% Add the task to the job.
% Returns 2 output items, parfor loop upper limit is 1024
createTask(j, @parforExample, 2, {1024})
% 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 parforExample job to batch.

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