If you have a multicore processor, you might see speedup 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 Getting Started with Parallel Computing Toolbox.
Suppose you have a dual-core processor, and want to use parallel computing:
Enter
parpool
parpool('local')
Note: Depending on your preferences, MATLAB can start a parallel pool automatically. To enable this feature, check Automatically create a parallel pool in Home > Parallel > Parallel Preferences. |
For command-line use, enter
options = optimoptions('solvername','UseParallel',true);
For Optimization app, check Options > Approximated derivatives > Evaluate in parallel.
When you run an applicable solver with options
,
applicable solvers automatically use parallel computing.
To stop computing optimizations in parallel, set UseParallel
to false
,
or set the Optimization app not to compute in parallel. To halt all
parallel computation, enter
delete(gcp)
If you have multiple processors on a network, use Parallel Computing Toolbox functions and MATLAB Distributed Computing Server™ software to establish parallel computation. Here are the steps to take:
Make sure your system is configured properly for parallel computing. Check with your systems administrator, or refer to the Parallel Computing Toolbox documentation.
To perform a basic check:
At the command line, enter
parpool(prof)
prof
is
your cluster profile.Workers must be able to access your objective function file and, if applicable, your nonlinear constraint function file. There are two ways of ensuring access:
Distribute the files to the workers using the parpool
AttachedFiles
argument.
For example, if objfun.m
is your objective function
file, and constrfun.m
is your nonlinear constraint
function file, enter
parpool('AttachedFiles',{'objfun.m','constrfun.m'});
Workers access their own copies of the files.
Give a network file path to your files. If network_file_path
is
the network path to your objective or constraint function files, enter
pctRunOnAll('addpath network_file_path')
Workers access the function files over the network.
Check whether a file is on the path of every worker by entering
pctRunOnAll('which filename')
filename not found.
For command-line use, enter
options = optimoptions('solvername','UseParallel',true);
For Optimization app, check Options > Approximated derivatives > Evaluate in parallel.
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
,
or set the Optimization app not to compute in parallel. To halt all
parallel computation, enter
delete(gcp)
To test see if a problem runs correctly in parallel,
Try your problem without parallel computation to ensure that it runs properly serially. Make sure this is successful (gives correct results) before going to the next test.
Set UseParallel
to true
,
and ensure that there is no parallel pool using delete(gcp)
.
Uncheck Automatically create a parallel pool in Home
> Parallel > Parallel Preferences so MATLAB does
not create a parallel pool . Your problem runs parfor
serially,
with loop iterations in reverse order from a for
loop.
Make sure this is successful (gives correct results) before going
to the next test.
Set UseParallel
to true
,
and create a parallel pool using parpool
. Unless
you have a multicore processor or a network set up, you won't see
any speedup. This testing is simply to verify the correctness of the
computations.
Remember to call your solver using an options structure to test or use parallel functionality.