Escolar Documentos
Profissional Documentos
Cultura Documentos
This guide is a compilation of the steps and clues necessary to get your already-running
MATLAB code and make it run in parallel in several processors, so that you can speed up
your executions and results
Original:
Last Revision:
April 2010
-
TABLE OF CONTENTS
OBJECTIVE:
EXAMPLE CODE
PERFORMANCE CONSIDERATIONS
OBJECTIVE:
This guide is a compilation of the steps and clues necessary to get your already-running
MATLAB code and make it run in parallel in several processors, so that you can speed up
your executions and results.
#: number of cores you want to use (max is number of cores in your PC)
- this instruction opens the parallel computing toolbox
- at this moment, MATLAB checks for a distributed-computing license (there
are only 19 of these available campus-wide, so be aware that if there is no one
available, you may get an error message, and will have to wait).
at the end of the code, type:
matlabpool('close');
- this will close the parallel computing, however, the license will still be in use
until you close your MATLAB session.
parfor:
is the parallel computing version of the traditional for command for a loop.
use it in place of for, just as simple as that:
parfor i = 1 : 100
(your code)
end
the parfor loop can only be done for one level, i.e., you cannot have a parfor
loop inside another parfor loop. Sorry! However, the parfor loop doesnt need to
be the outermost, it can be inside a regular for loop if needed.
You can have for loops inside the parfor loop, but you will need to pre-allocate
all the variables that grow inside the for loops. Just pre-allocating immediately
before the corresponding for loop will do. (This is usually a recommendation for
speed in regular MATLAB runs, but it is a must in parallel mode.)
You cannot have any instruction that calls to the workspace inside the parfor loop,
such as delete variables (clear) or list variables (who).
Be careful if you have figures inside the parfor loop. They should work, and you
can even save them. But if you are displaying on screen, make sure you close the
figures within the loop, so that they dont flood your desktop!
The best way to get your results out is to have a one-dimensional variable inside
the parfor loop, with the dimension being the parfor loop itself (see example).
You can also print to file and save data inside the parfor loop, this is also a good
way to take results out of the parfor loop.
(include here any more tips that you may want to share after working in parallel
mode yourself)
After all this, we can say that parallel computing done this way in MATLAB is great when you
need to run a code for (a lot of) different cases, when the cases are independent from each
other (also known as embarrassingly parallel applications ).
EXAMPLE CODE
This code will run in parallel mode in MATLAB:
matlabpool ('open',2);
x = zeros(100,10);
parfor i = 1:100
y = zeros(1,10);
for j = 1:10
y(j) = i;
end
y
x(i,:) = y;
end
x
matlabpool close;
%
%
%
%
%
It wont work because we are using outside the parfor loop a variable (a) that gets
modified inside the parfor loop (even though it is a constant in this case).
Solutions:
a) Instead, we may need to define a outside the parfor loop (if it is going to remain
constant):
matlabpool ('open',2);
a = 3;
parfor i = 1:100
(your code)
end
a
matlabpool close;
This case may run even if we dont initialize the variable, but it is good to do it, for
more broad cases.
PERFORMANCE CONSIDERATIONS
For an overview of performance comparisons when running a MATLAB code in different
hardware with various numbers of processors, check Nils Oberg et al.s paper entitled
MATLAB Parallel Computing Toolbox Benchmark for an Embarrassingly Parallel Application
(see http://vtchl.illinois.edu/publications/matlab/parallel_benchmark_2008).