# MATLAB Parallel Computing Toolbox (PCT)

### Alternative to dfeval

This page demonstrates an effective alternative to the MATLAB Parallel Computing Toolbox’s dfeval method for distributed parallel computing. Distributed parallel computing, or task-parallel computing, is a special type of parallel computing in which the tasks are independent of each other and hence can be processed individually. The alternative method is described in the Katana’s multiple_matlab_tasks webpage. Its advantages are: 1) no learning of MATLAB PCT is required; 2) No PCT or worker licenses are required. Included below are the specifics of applying that to the example of solving multiple linear algebraic systems.

There are three scripts required to accomplish the goal:

1. The first is a matlab function that solves a linear system
``````function myfunc(M, C, i)
% Solves a linear system of Ax = b
% Multiple systems are solved in parallel; each on a separate worker.
% Companion scripts used: submitmjobs and run_matlab_job
% This demonstrates the use of SCV's parallel method in place of
% MATLAB Parallel Computing Toolbox's dfeval

[A, x, b] = linearSystem(M, C, i)  % compute coefficient matrices
y = Ab    % solves Ay = b; y should equal x
```
```
2. Next, a run_matlab_job shell script is needed to run the myfunc m-file defined above
``````#!/bin/csh
# this script is a companion script for submitmjobs for MATLAB apps
unsetenv DISPLAY
@ rank = \$MATLAB_RANK
@ size = \$MATLAB_N
# IMPORTANT: DONOT indent any of the below statements
matlab -nojvm  >! myoutput_\$rank << MATLAB_ENV
% The below works like a script m-file between MATLAB_ENVs
i = \$rank + 1        % define index i
n = \$size;           % matrix size
M = rand(n);         % n x n random number matrix
C = ones(n, 1);      % define array c
myfunc(M, C, i)      % how to handle a function m-file
exit                 % don't forget to exit
MATLAB_ENV
```
```
3. Finally, a submitmjobs shell script is responsible for submitting jobs to the batch queue
``````#!/bin/csh
#
#Usage: submitmjobs ntasks batch_script "SGE options such as wallclock limit"
#   batch_script -- batch script, see run_matlab_job for MATLAB app
#   The OPTIONAL input (in red) must be valid SGE batch parameters.
# Example: To submit 4 tasks with run_matlab_job and 9-hour wallclock limit
# katana% submitmjobs 4 run_matlab_job "-l h_rt=09:00:00"
#
# The Sun Grid Engine batch preserves the current dir in batch.
# A different dir may also be used
# cd my_dir
set batch_script = \$2
set options = "\$3"
@ size = 3
@ rank = 0
setenv MATLAB_RANK \$rank
setenv MATLAB_N \$size
qsub \$options -V \$batch_script
@ rank++
end
```
```
4. Example.
``````
katana% submitmjobs 4 run_matlab_job
Your job 62446 ("run_matlab_job") has been submitted
Your job 62447 ("run_matlab_job") has been submitted
Your job 62448 ("run_matlab_job") has been submitted
Your job 62449 ("run_matlab_job") has been submitted
```
```

The four independent tasks are submitted as four single processor jobs. The output, as specified in run_matlab_job, go to myoutput_1, myoutput_2, myoutput_3, myoutput_4, respectively.

5. The m-file linearSystem, defined in the Katana PCT page, is included below for your convenience
``````function [A, x, b] = linearSystem(M, C, i)
%function [A, x, b] = linearSystem(M, C, i)
% Returns matrices of a linear algebraic system Ax=b
%  M -- matrix
%  C -- a vector used to generate the solution
%  i -- number used to generate solution x;
%       in this example, pass in the for-loop index

A = M + M';      % A is real and symmetric
x = i*C;         % define solution
b = A * x;       % Ax = b is the RHS of linear system
```
```