Tuesday, 7 October 2014

parallel computing toolbox - MATLAB ( for beginner guide )

STEP 1: Have own code ready and to runordinary MATLAB

first of all , make sure your code is running and doing what you want it to do in your ordinary MATLAB execution in your PC.
Also, to run in parallel, you must use a version of MATLAB that is 2007b or later.

STEP 2: Make the preparation of your ordinary-running code to become a parallel-running code

In order to tell MATLAB that you want to run using multiple processors, you need two main instructions in your code:

matlabpool

matlabpool tells MATLAB that you want to use multiple processors in your run
At the beginning of the code, type:
matlabpool('open',#);
where # is the number of cores you want to use (max is number of cores in your PC).  This instruction opens the parallel computing toolbox.  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

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 loop variable (i) can only be a vector of consecutive integers.  Also, the code inside this loop is the portion of your code that will be executed in parallel.
Check “MATLAB help” for particular conditions for both “matlabpool” and “parfor”.

STEP 3: Things to consider in your code when going “parallel”

On the right side of your MATLAB Editor window you can see the warnings and errors that MATLAB is detecting in your code before you even run.
In the case of having a “parfor” loop, MATLAB may become a little bit more picky than usual.
Here are some tips:
  • when using “parfor”, all the executions of the loop need to be independent from each other. This is such that the “parfor” loop doesn’t even executes consecutively (i = 1, 2, 3, 4, …), but quite randomly (i = 3, 4, 59, 37, …) decided by MATLAB. This way, you cannot have any variable that can be modified by all the different processors at the same time, unless it has the proper dimensional index (x(i,j,k)).
  • 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 doesn’t 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 don’t 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.
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);      % Call to open the distributed processing
x = zeros(100,10);          % Initialize the main variable
parfor i = 1:100            % Parallel loop
     y = zeros(1,10);       % Initialize the secondary variable
     for j = 1:10           % Inner loop
         y(j) = i;
     end
     y                      % Display the inner variable (note the random execution about "i" in the command window)
     x(i,:) = y;            % Get values from loop into the main variable
end
x                           % Display main variable
matlabpool close;           % Close the distributed computing
This code will NOT run in parallel mode:
matlabpool ('open',2);      % Call to open the distributed processing
parfor i = 1:100            % Parallel loop
     a = 3;
 (your code)
end
a                           % Display main variable
matlabpool close;           % Close the distributed computing
It won’t 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);      % Call to open the distributed processing
a = 3;
parfor i = 1:100            % Parallel loop
     (your code)
end
a                           % Display main variable
matlabpool close;           % Close the distributed computing
b) Or assign it a dimension according to the “parfor” loop (i):
matlabpool ('open',2);      % Call to open the distributed processing
a = zeros(1,100);      % Initialize the main variable
parfor i = 1:100            % Parallel loop
 a(i) = 3;
     (your code)
end
a                           % Display main variable
matlabpool close;           % Close the distributed computing

No comments:

Post a Comment