In MATLAB, there are two complementary ways to perform parallel computations:
- pmode is a parallel utility strictly for interactive parallel processing. Upon invoking it from the MATLAB prompt (
>>), pmode spawns a new window with tiles, each of which functions like a MATLAB window for an individual worker.
- pmode may be started with the keyword open or start. See the help page on pmode for details.
- This automatically puts you in a Single Program Multiple Data, or spmd, parallel environment. The “Single Program” refers to the fact that a single statement or a whole m-file invoked in spmd (in this case, the P>> prompt) will be executed concurrently on all workers. To truly benefit from multiprocessing, you would want to have the operation on each worker to run on distinct data segment (e.g., different segments of an array or matrix assigned to different workers) which leads to the “Multiple Data” part of the name. Towards this end, numlabs may be used to query the number of workers allotted while labindex is used to identify the worker ID (which is an integer ranging from 1 to numlabs). Generally, use of these two query functions is sufficient for the (single) program to execute a task with each acting on its own data segment. Here is an example,
a(labindex) = sum(rand(1,numlabs)); % sum random array, store in a(labindex) if labindex == 1 c(labindex) = 0; % set c(1) to 0 else c(labindex) = a(labindex); % for all other workers ... end
- While there are multiple windows to represent individual workers, there is only one parallel prompt (
P>>) below the worker tiles. This is the direct consequence of being in the spmd mode. A typical usage is as follows
P>> labindex % concurrently query/print local worker ID P>> if labindex==1, numlabs, end % On worker 1 only, query/print the number of workers allotted
- To close pmode
Or, from the MATLAB client window
>> pmode close
- matlabpool is the general parallel mode. It may be used for interactive processing (without the tiles for individual workers) and batch processing. It can only be started with the keyword open
>> matlabpool open local 4 % open matlabpool locally with 4 workers >> . . . >> % any matlab or PCT commands, script m-files, or function m-files >> . . . >> matlabpool close % to close matlabpool
Each MATLAB session allows only one parallel environment at a time. For instance, you need to close an existing pmode session before opening a matlabpool session. Alternatively, you could start another (new) MATLAB session, and then open matlabpool from the second MATLAB command window. Note, however, that if you launch multiple copies of MATLAB for the express purpose of launching pmode or matlabpool sessions, then the same physical processors are shared among the sessions and hence true parallel performance may be affected. Examples include: a laptop running Windows or Linux; a command window from an interactive batch
qsh session on the SCC. To run multiple parallel background (as opposed to interactive) batch jobs on the SCC will not have this problem as each batch job is assigned distinct processors or nodes.