Quantcast

Documentation Center

  • Trial Software
  • Product Updates

How to Use Parallel Processing

Multicore Processors

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

    at the command line. MATLAB® starts a pool of workers using the multicore processor. If you had previously set a nondefault cluster profile, you can enforce multicore (local) computing:

    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.

  • Set your solver to use parallel processing.

    MultiStartPatternsearchGA

    ms = MultiStart('UseParallel', true);

    or

    ms.UseParallel = true

    options = psoptimset('UseParallel', true, 'CompletePoll', 'on', 'Vectorized', 'off');

    options = gaoptimset('UseParallel', true, 'Vectorized', 'off');

     

    For Optimization app:

    • Options > User function evaluation > Evaluate objective and constraint functions > in parallel

    • Options > Complete poll > on

    For Optimization app:

    • Options > User function evaluation > Evaluate fitness and constraint functions > 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)

Processor Network

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:

  1. 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:

    1. At the command line, enter

      parpool(prof)

      where prof is your cluster profile.

    2. 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:

      1. 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.

      2. 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.

    3. Check whether a file is on the path of every worker by entering

      pctRunOnAll('which filename')

      If any worker does not have a path to the file, it reports

      filename not found.
  2. Set your solver to use parallel processing.

    MultiStartPatternsearchGA

    ms = MultiStart('UseParallel', true);

    or

    ms.UseParallel = true

    options = psoptimset('UseParallel', true, 'CompletePoll', 'on', 'Vectorized', 'off');

    options = gaoptimset('UseParallel', true, 'Vectorized', 'off');

     

    For Optimization app:

    • Options > User function evaluation > Evaluate objective and constraint functions > in parallel

    • Options > Complete poll > on

    For Optimization app:

    • Options > User function evaluation > Evaluate fitness and constraint functions > 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)

Parallel Search Functions or Hybrid Functions

To have a patternsearch search function run in parallel, or a hybrid function for ga or simulannealbnd run in parallel, do the following.

  1. Set up parallel processing as described in Multicore Processors or Processor Network.

  2. Ensure that your search function or hybrid function has the conditions outlined in these sections:

patternsearch Search Function

patternsearch uses a parallel search function under the following conditions:

  • CompleteSearch is 'on'.

  • The search method is not @searchneldermead or custom.

  • If the search method is a patternsearch poll method or Latin hypercube search, UseParallel is true. Set at the command line with psoptimset:

    options = psoptimset('UseParallel',true,...
        'CompleteSearch','on','SearchMethod',@GPSPositiveBasis2N);

    Or you can use the Optimization app.

  • If the search method is ga, the search method option structure has UseParallel set to true. Set at the command line with psoptimset and gaoptimset:

    iterlim = 1; % iteration limit, specifies # ga runs
    gaopt = gaoptimset('UseParallel',true);
    options = psoptimset('SearchMethod',...
        {@searchga,iterlim,gaopt});

In the Optimization app, first create the gaopt structure as above, and then use these settings:

For more information about search functions, see Using a Search Method.

Parallel Hybrid Functions

ga and simulannealbnd can have other solvers run after or interspersed with their iterations. These other solvers are called hybrid functions. For information on using a hybrid function with gamultiobj, see Parallel Computing with gamultiobj. Both patternsearch and fmincon can be hybrid functions. You can set options so that patternsearch runs in parallel, or fmincon estimates gradients in parallel.

Set the options for the hybrid function as described in Hybrid Function Options for ga, or Hybrid Function Options for simulannealbnd. To summarize:

  • If your hybrid function is patternsearch

    1. Create a patternsearch options structure:

      hybridopts = psoptimset('UseParallel',true,...
          'CompletePoll','on');
    2. Set the ga or simulannealbnd options to use patternsearch as a hybrid function:

      options = gaoptimset('UseParallel',true); % for ga
      options = gaoptimset(options,...
          'HybridFcn',{@patternsearch,hybridopts});
      % or, for simulannealbnd:
      options = saoptimset('HybridFcn',{@patternsearch,hybridopts});

      Or use the Optimization app.

    For more information on parallel patternsearch, see Pattern Search.

  • If your hybrid function is fmincon:

    1. Create a fmincon options structure:

      hybridopts = optimoptions(@fmincon,'UseParallel',true,...
          'Algorithm','interior-point');
      % You can use any Algorithm except trust-region-reflective
    2. Set the ga or simulannealbnd options to use fmincon as a hybrid function:

      options = gaoptimset('UseParallel',true);
      options = gaoptimset(options,'HybridFcn',{@fmincon,hybridopts});
      % or, for simulannealbnd:
      options = saoptimset('HybridFcn',{@fmincon,hybridopts});

      Or use the Optimization app.

    For more information on parallel fmincon, see Parallel Computing.

Was this topic helpful?