Run Code on Parallel Pools - MATLAB & Simulink (original) (raw)
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 asparfor
. You can specify the default cluster in your parallel preferences. The preferences panel displays your default cluster when you select in the menu. You can also specify the default cluster in the menu. Alternatively, you can choose cluster and pool size using parcluster
andparpool
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 yourparpool
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:
- parfor
- spmd
- distributed
- Composite
- parfeval
- parfevalOnAll
- gcp
- mapreduce
- mapreducer
- tall
- ticBytes and tocBytes
Your parallel preferences specify which cluster the pool runs on. To access your preferences, on the tab, in the section, click > .
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 . The pool cluster is specified by your default cluster. Your default cluster is indicated by a check mark on the > 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 .
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:
To open a pool of a specific size:
To use a cluster other than your default and specify where the pool runs:
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:
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:
Note
To stop a parallel pool while it is starting, pressCtrl+C or Ctrl+Break. OnApple macOS, you also can useCommand . (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
andPreferredPoolNumWorkers
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 theNumWorkers
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 thePreferredPoolNumWorkers
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 theNumWorkers
property value.
Pool Creation Technique
The NumWorkers
andPreferredPoolNumWorkers
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 |
---|---|
You have automatic pool creation turned on, and you run a function that can automatically start a pool.You start a parallel pool manually using the parallel status indicator in the lower left corner of the MATLAB desktopYou call the parpool function without specifying a pool size argument. | MATLAB uses the cluster profilePreferredPoolNumWorkers property value to determine the maximum number of workers in the pool. If MATLAB cannot start a pool with as many workers as specified in thePreferredPoolNumWorkers property, you get a smaller pool without any errors. The pool size cannot exceed theNumWorkers value. |
You run the parpool function and specify the pool size as an integer. | MATLAB attempts to start a pool with the exact number of workers you request. If the specified value does not exceed theNumWorkers property value, you get a pool with the specified number of workers.If the specified value exceeds theNumWorkers property value, MATLAB throws an error. |
You run the parpool function and specify the pool size as a range of integers. | MATLAB attempts to create a pool with the largest possible value within that range without exceeding theNumWorkers property value. If the lower bound of the range is greater than the NumWorkers property value, MATLAB throws an error.If the upper bound of the range exceeds theNumWorkers property value, you get the largest pool size possible up to theNumWorkers property value. |
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.
See Also
delete | gcp | parcluster | parfor | spmd | distributed | parfeval | parpool