Using Parallel Computing in Optimization Toolbox
Using Parallel Computing with Multicore Processors
If you have a multicore processor, you can increase processing speed by using parallel processing. You can establish a parallel pool of several workers with a Parallel Computing Toolbox™ license. For a description of Parallel Computing Toolbox software, see Get Started with Parallel Computing Toolbox (Parallel Computing Toolbox).
Suppose you have a dual-core processor, and want to use parallel computing. Enter this code at the command line.
parpool
MATLAB® starts a pool of workers using the multicore processor. If you previously set a nondefault cluster profile, you can enforce multicore (local) computing by entering this code.
parpool('local')
Note
Depending on your preferences, MATLAB can start a parallel pool automatically. To enable this feature, select Parallel > Parallel Preferences in the Environment group on the Home tab, and then select Automatically create a parallel pool.
Set solver options to use parallel computing.
options = optimoptions('solvername','UseParallel',true);
When you run an applicable solver with options
,
applicable solvers automatically use parallel computing.
To stop computing optimizations in parallel, set UseParallel
to
false
. To halt all parallel computation, enter this code.
delete(gcp)
Note
The documentation recommends not to use parfor
or
parfeval
when calling Simulink®; see Using sim Function Within parfor (Simulink). Therefore, you might
encounter issues when optimizing a Simulink simulation in parallel using a solver's built-in parallel functionality. For
an example showing how to optimize a Simulink model with several Global Optimization Toolbox solvers, see Optimize Simulink Model in Parallel (Global Optimization Toolbox).
Using Parallel Computing with a Multiprocessor Network
If you have multiple processors on a network, use Parallel Computing Toolbox functions and MATLAB Parallel Server™ software to establish parallel computation.
Make sure your system is configured properly for parallel computing. Check with your systems administrator, or refer to the Parallel Computing Toolbox documentation.
Perform a basic check by entering this code, where
prof
is your cluster profile.parpool(prof)
Workers must be able to access your objective function file and, if applicable, your nonlinear constraint function file. Complete one of these steps to ensure access:
Distribute the files to the workers using the
parpool
(Parallel Computing Toolbox)AttachedFiles
argument. In this example,objfun.m
is your objective function file, andconstrfun.m
is your nonlinear constraint function file.parpool('AttachedFiles',{'objfun.m','constrfun.m'});
Workers access their own copies of the files.
Give a network file path to your objective or constraint function files.
pctRunOnAll('addpath network_file_path')
Workers access the function files over the network.
Check whether a file is on the path of every worker.
If any worker does not have a path to the file, it reportspctRunOnAll('which filename')
filename not found.
Set solver options to specify using parallel computing. The argument
'solvername'
represents a nonlinear solver that supports
parallel evaluation.
options = optimoptions('solvername','UseParallel',true);
After you establish your parallel computing environment, applicable
solvers automatically use parallel computing whenever you call them
with options
.
To stop computing optimizations in parallel, set UseParallel
to
false
. To halt all parallel computation, enter this code.
delete(gcp)
Note
The documentation recommends not to use parfor
or
parfeval
when calling Simulink; see Using sim Function Within parfor (Simulink). Therefore, you might
encounter issues when optimizing a Simulink simulation in parallel using a solver's built-in parallel functionality. For
an example showing how to optimize a Simulink model with several Global Optimization Toolbox solvers, see Optimize Simulink Model in Parallel (Global Optimization Toolbox).
Testing Parallel Computations
Follow these steps to test whether your problem runs correctly in parallel.
Try your problem without parallel computation to ensure that it runs serially. Make sure this test is successful (gives correct results) before going to the next test.
Set
UseParallel
totrue
, and ensure that no parallel pool exists by enteringdelete(gcp)
. To make sure that MATLAB does not create a parallel pool, select Parallel > Parallel Preferences in the Environment group on the Home tab, and then clear Automatically create a parallel pool. Your problem runsparfor
serially, with loop iterations in reverse order from afor
loop. Make sure this test is successful (gives correct results) before going to the next test.Set
UseParallel
totrue
, and create a parallel pool usingparpool
. Unless you have a multicore processor or a network set up, this test does not increase processing speed. This testing is simply to verify the correctness of the computations.
Remember to call your solver using an options
argument to test or use
parallel functionality.