Main Content

快速启动并行计算Statistics and Machine Learning Toolbox

Note

要使用并行计算,您必须具有并行计算工具箱™许可证。

ParallelStatistics and Machine Learning Toolbox功能

You can use any of the Statistics and Machine Learning Toolbox™ functions with Parallel Computing Toolbox constructs such asparfor(Parallel Computing Toolbox)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 (seeVocabulary 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, seeFunction List (Automatic Parallel Support). For the usage notes and limitations of each function, see the Automatic Parallel Support section on the function reference page.

How to Compute in Parallel

This section gives the simplest way to use the enhanced functions in parallel. For more advanced topics, including the issues of reproducibility and nestedparforloops, see the other topics inSpeed Up Statistical Computations.

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

help parallelstats

To have a function compute in parallel:

Set Up a Parallel Environment

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是您要使用的工人数量。

You can also run parallel code inMATLAB Online™. For details, see在MATLAB在线使用并行计算工具箱与Cloud Center群集(Parallel Computing Toolbox).

Set theUseParallelOption to真的

Create an options structure with thestatsetfunction. To run in parallel, set theUseParallel选项真的:

paroptions = statset('UseParallel',true);

Call the Function Using the Options Structure

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使用并行处理进行回归TreeBagger工作流程,Implement Jackknife Using Parallel Computing,使用并行计算实施交叉验证, andImplement Bootstrap Using Parallel Computing.

After you have finished computing in parallel, close the parallel environment:

删除mypool

Tip

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