Run Code on Parallel Pools
What Is a Parallel Pool?
A parallel pool is a set of MATLAB® workers on a compute cluster or desktop. By default, a parallel pool
starts automatically when needed by parallel language features such as
parfor
. You can specify the default cluster in your
parallel preferences. The preferences panel displays your default cluster when you
select Parallel Preferences in the
Parallel menu. You can also specify the default
cluster in the Parallel menu. Alternatively, you can
choose cluster and pool size using parcluster
and
parpool
respectively, on the MATLAB command line. See the image for more detail.
The workers in a parallel pool can be used interactively and communicate with
each other during the lifetime of the job. You can view your
parpool
jobs in the Job Monitor. While these pool
workers are reserved for your interactive use, they are not available to other
users. You can have only one parallel pool at a time from a MATLAB client session. In MATLAB, the current parallel pool is represented by a parallel.Pool
object.
Automatically Start and Stop a Parallel Pool
By default, a parallel pool starts automatically when needed by certain parallel language features. Many functions can automatically start a parallel pool, including:
Your parallel preferences specify which cluster the pool runs on. To access your preferences, on the Home tab, in the Environment section, click Parallel > Parallel Preferences.
In your parallel preferences, you can turn off the option for the pool to open or close automatically. If you turn off the option to open a pool automatically and you use any Parallel Computing Toolbox functionality without an open parallel pool, your code will run on the client.
Alternative Ways to Start and Stop Pools
If you choose not to have the pool open automatically, you can control the pool with the following techniques.
Control the Parallel Pool from the MATLAB Desktop
You can use the parallel status indicator in the lower left corner of the MATLAB desktop to start a parallel pool manually.
In MATLAB Online, the parallel status indicator is not visible by
default. You must start a parallel pool first by using parpool
or any of the functions
that automatically start a parallel pool.
Click the indicator icon, and select Start Parallel Pool. The pool cluster is specified by your default cluster. Your default cluster is indicated by a check mark on the Parallel > Default Cluster menu.
The menu options are different when a pool is running. You can:
View the number of workers and cluster name
Change the time until automatic shut-down
Shut down the parallel pool
To stop a pool, you can also select Shut Down Parallel Pool.
Programming Interface
Start a Parallel Pool. You can start and stop a parallel pool programmatically by using default settings or specifying alternatives.
To open a parallel pool based on your default settings:
parpool
To open a pool of a specific size:
parpool(4)
To use a cluster other than your default and specify where the pool runs:
parpool('MyCluster',4)
You can run a parallel pool on different parallel environments. For more information, see Choose Between Thread-Based and Process-Based Environments.
Shut Down a Parallel Pool. To get the current parallel pool and use that object when you want to shut down the pool:
p = gcp; delete(p)
Ensure That No Parallel Pool Is Running. When you issue the command gcp
without arguments, you
might inadvertently open a pool. To avoid this problem:
delete(gcp('nocreate'))
Note
To stop a parallel pool while it is starting, press Ctrl+C or Ctrl+Break. On Apple macOS, you also can use Command. (the Command key and the period key).
Factors That Affect Pool Size
Parallel Computing Toolbox™ can support parallel pools with up to 2000 workers. When you create a
parallel pool, the values of the NumWorkers
and
PreferredPoolNumWorkers
cluster object properties affect
the size of the parallel pool you get.
NumWorkers
Cluster Property
The NumWorkers
cluster property is a hard limit on the
number of workers available to the cluster.
For MATLAB Job Scheduler clusters, the software determines the
NumWorkers
property value from the number of workers running in the cluster.For local clusters running on the client and third-party scheduler clusters, you can specify the
NumWorkers
property value in the cluster profile.
You cannot create a parallel pool larger than the
NumWorkers
property value.
PreferredPoolNumWorkers
Cluster Property
The PreferredPoolNumWorkers
cluster object property
specifies a preference for the size of the pools the
software creates with the cluster object. It is not a requirement or a request
for a specific number of workers. The default value for the
PreferredPoolNumWorkers
property depends on cluster
type. You can modify this value in the cluster profile. The following is a list
of cluster types and their default PreferredPoolNumWorkers
values:
MATLAB Job Scheduler, shared cloud, and third-party scheduler clusters — 32
Personal cloud clusters —
Inf
local Processes clusters —
Inf
(since R2023b)In R2023a: For local profiles, the default value is the
NumWorkers
property value.
Pool Creation Technique
The NumWorkers
and
PreferredPoolNumWorkers
property values in combination
with the technique you use to create the pool determine actual size of your
parallel pool.
Pool Creation Technique | Actual Pool Size |
---|---|
| MATLAB uses the cluster profile
|
You run the | MATLAB attempts to start a pool with the exact number of workers you request.
|
You run the | MATLAB attempts to create a pool with the largest
possible value within that range without exceeding the
|
Precedence for Cluster Selection
For selection of the cluster on which the pool runs, precedence is determined by the following:
The command-line cluster object argument overrides the default profile setting and uses the cluster identified by the profile
'MyProfile'
.c = parcluster('MyProfile'); p = parpool(c);
The cluster is specified in the default profile.
p = parpool;
See Also
delete
| gcp
| parcluster
| parfor
| spmd
| distributed
| parfeval
| parpool