Main Content

**Note**

To use parallel computing as described in this chapter, 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, the following functions are enhanced to use parallel computing
internally. These functions use `parfor`

internally to
parallelize calculations.

The following functions for fitting multiclass models for support vector machines and other classifiers are also enhanced to use parallel computing internally.

Methods of the class

`ClassificationECOC`

:Methods of the class

`CompactClassificationECOC`

Methods of the class

`ClassificationPartitionedECOC`

Methods of the class

`ClassificationPartitionedLinearECOC`

The following functions perform hyperparameter optimization in parallel.

This chapter gives the simplest way to use these enhanced functions in parallel.
For more advanced topics, including the issues of reproducibility and nested
`parfor`

loops, see the other sections in this
chapter.

For information on parallel statistical computing at the command line, enter

help parallelstats

To have a function compute in parallel:

To run a statistical computation in parallel, first set up a parallel environment.

**Note**

Setting up a parallel environment can take several seconds.

For a multicore machine, enter the following at the MATLAB^{®} command line:

parpool(n)

* 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).

`UseParallel`

Option to `true`

Create an options structure with the `statset`

function. To run in
parallel, set the `UseParallel`

option to
`true`

:

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:

delete mypool

**Tip**

To save time, keep the pool open if you expect to compute in parallel again soon.