when to run statistical functions in parallel
why run in parallel?
the main reason to run statistical computations in parallel is to gain speed, meaning to reduce the execution time of your program or functions. factors affecting speed discusses the main items affecting the speed of programs or functions. factors affecting results discusses details that can cause a parallel run to give different results than a serial run.
note
some statistics and machine learning toolbox™ functions have built-in parallel computing capabilities. see quick
start parallel computing for statistics and machine learning toolbox. you can also use any statistics and machine learning toolbox functions with parallel computing toolbox™ functions such as parfor
loops. to decide
when to call functions in parallel, consider the factors affecting speed and
results.
factors affecting speed
some factors that can affect the speed of execution of parallel processing are:
parallel environment setup. it takes time to run
parpool
to begin computing in parallel. if your computation is fast, the setup time can exceed any time saved by computing in parallel.parallel overhead. there is overhead in communication and coordination when running in parallel. if function evaluations are fast, this overhead could be an appreciable part of the total computation time. thus, solving a problem in parallel can be slower than solving the problem serially. for an example, see in matlab® digest, march 2009.
no nested
parfor
loops. this is described in working with parfor.parfor
does not work in parallel when called from within anotherparfor
loop. if you have programmed your custom functions to take advantage of parallel processing, the limitation of no nestedparfor
loops can cause a parallel function to run slower than expected.when executing serially,
parfor
loops run slightly slower thanfor
loops.passing parameters. parameters are automatically passed to worker sessions during the execution of parallel computations. if there are many parameters, or they take a large amount of memory, passing parameters can slow the execution of your computation.
contention for resources: network and computing. if the pool of workers has low bandwidth or high latency, parallel computation can be slow.
factors affecting results
some factors can affect results when using parallel processing. you might need to adjust your code to run in parallel, for example, you need independent loops and the workers must be able to access the variables. some important factors are:
persistent or global variables. if any functions use persistent or global variables, these variables can take different values on different worker processors. the body of a
parfor
loop cannot contain global or persistent variable declarations.accessing external files. the order of computations is not guaranteed during parallel processing, so external files can be accessed in unpredictable order, leading to unpredictable results. furthermore, if multiple processors try to read an external file simultaneously, the file can become locked, leading to a read error, and halting function execution.
noncomputational functions, such as
input
,plot
, andkeyboard
, can behave badly when used in your custom functions. do not use these functions in aparfor
loop, because they can cause a worker to become nonresponsive, since it is waiting for input.parfor
does not allowbreak
orreturn
statements.the random numbers you use can affect the results of your computations. see reproducibility in parallel statistical computations.
for advice on converting for loops to use parfor
, see parallel for-loops (parfor) (parallel computing toolbox).