To use parallel computing, you must have a Parallel Computing Toolbox™ license.
You can use any of the Statistics and Machine Learning Toolbox™ functions with Parallel Computing Toolbox constructs such as
parfor (Parallel Computing Toolbox) and
spmd (Parallel Computing Toolbox). However, some functions,
such as those with interactive displays, can lose functionality in parallel. In
particular, displays and interactive usage are not effective on workers (see Vocabulary for Parallel Computation).
Additionally, some Statistics and Machine Learning Toolbox functions are enhanced to use parallel computing internally. For example, some model fitting functions perform hyperparameter optimization in parallel. For a complete list of Statistics and Machine Learning Toolbox functions that support parallel computing, see Function List (Automatic Parallel Support). For the usage notes and limitations of each function, see the Automatic Parallel Support section on the function reference page.
This section gives the simplest way to use the enhanced functions in parallel. For
more advanced topics, including the issues of reproducibility and nested
parfor loops, see the other topics in Speed Up Statistical Computations.
For information on parallel statistical computing at the command line, enter
To have a function compute in parallel:
To run a statistical computation in parallel, first set up a parallel environment.
Setting up a parallel environment can take several seconds.
For a multicore machine, enter the following at the MATLAB® command line:
n is the number of workers you want to use.
You can also run parallel code in MATLAB Online™. For details, see Use Parallel Computing Toolbox with Cloud Center Cluster in MATLAB Online (Parallel Computing Toolbox).
Create an options structure with the
statset function. To run in
parallel, set the
UseParallel option to
paroptions = statset('UseParallel',true);
Call your function with syntax that uses the options structure. For example:
% Run crossval in parallel cvMse = crossval('mse',x,y,'predfun',regf,'Options',paroptions); % Run bootstrp in parallel sts = bootstrp(100,@(x)[mean(x) std(x)],y,'Options',paroptions); % Run TreeBagger in parallel b = TreeBagger(50,meas,spec,'OOBPred','on','Options',paroptions);
For more complete examples of parallel statistical functions, see Use Parallel Processing for Regression TreeBagger Workflow, Implement Jackknife Using Parallel Computing, Implement Cross-Validation Using Parallel Computing, and Implement Bootstrap Using Parallel Computing.
After you have finished computing in parallel, close the parallel environment:
To save time, keep the pool open if you expect to compute in parallel again soon.