Main Content

parpool

在集群上创建并行池

Description

example

parpoolstarts a parallel pool of workers using the default cluster profile. With default preferences, MATLAB®starts a pool on the local machine with one worker per physical CPU core, up to the preferred number of workers. For more information on parallel preferences, see指定您的平行首选项

In general, the pool size is specified by your parallel preferences and the default profile.parpoolcreates a pool on the default cluster with itsNumWorkers在范围内[1, preferredNumWorkers]for running parallel language features.preferredNumWorkers是the value defined in your parallel preferences. For all factors that can affect your pool size, seePool Size and Cluster Selection

parpoolenables the full functionality of the parallel language features in MATLAB by creating a special job on a pool of workers, and connecting the MATLAB client to the parallel pool. Parallel language features includeparfor,parfeval,parfevalOnAll,spmd, 和分散式。If possible, the working folder on the workers is set to match that of the MATLAB client session.

example

Parpool(poolsize)creates and returns a pool with the specified number of workers.poolsizecan be a positive integer or a range specified as a 2-element vector of integers. Ifpoolsize是一个范围,在所需的范围内,生成的池的大小尽可能大。

Specifying thepoolsize覆盖偏好或配置文件中指定的工人人数,并启动精确的工人数量,即使必须等待他们可用。大多数集群都有最多可以启动的工人数量。如果配置文件指定MATLAB作业调度程序群集,则parpool从已经运行并在该MATLAB作业调度程序下可用的人员中保留其工人。如果配置文件指定本地或第三方调度程序,parpoolinstructs the scheduler to start the workers for the pool.

example

Parpool(resources)或者Parpool(resources,poolsize)starts a worker pool on the resources specified byresources

example

Parpool(___,Name,Value)applies the specified values for certain properties when starting the pool.

example

poolobj= parpool(___)返回一个Parallel.Pool对象代表群集上池的客户端工作区。您可以使用池对象来编程删除池或访问其属性。采用删除(pool)关闭平行池。

Examples

全部收缩

Start a parallel pool using the default profile to define the number of workers. With default preferences, the default pool is on the local machine.

parpool

You can create pools on different types of parallel environments on your local machine.

  • Start a parallel pool of process workers.

    Parpool('local')
  • Start a parallel pool of thread workers.

    Parpool('threads')

Fore more information on parallel environments, seeChoose Between Thread-Based and Process-Based Environments

Start a parallel pool of 16 workers using a profile calledmyProf

Parpool('myProf',16)

创建一个代表默认配置文件标识的群集的对象,并使用该群集对象启动并行池。池大小由默认配置文件确定。

c = parcluster parpool(c)

Start a parallel pool with the default profile, and pass two code files to the workers.

Parpool('AttachedFiles',{'mod1.m','mod2.m'})

If you have access to several GPUs, you can perform your calculations on multiple GPUs in parallel using a parallel pool.

To determine the number of GPUs that are available for use in MATLAB, use thegpuDeviceCountfunction.

availableGPUs = gpuDeviceCount("available")
availableGPUs = 3

Start a parallel pool with as many workers as available GPUs. For best performance, MATLAB assigns a different GPU to each worker by default.

Parpool('local',availableGPUs);
开始平行池(parpool)使用the 'local' profile ... Connected to the parallel pool (number of workers: 3).

要确定每个工人使用哪个GPU,请致电gpuDevice内部spmd堵塞。这spmdblock runsgpuDevice在每个工人上。

spmdgpuDeviceend

使用平行语言功能,例如parfor或者parfeval, to distribute your computations to workers in the parallel pool. If you usegpuArrayenabled functions in your computations, these functions run on the GPU of the worker. For more information, seeRun MATLAB Functions on a GPU。例如,请参阅Run MATLAB Functions on Multiple GPUs

完成计算后,关闭并行池。You can use thegcpfunction to obtain the current parallel pool.

删除(gcp('nocreate'));

If you want to use a different choice of GPUs, then you can usegpuDevice使用GPU设备索引在每个工人上选择一个特定的GPU。您可以使用该系统中每个GPU设备的索引gpuDeviceCountfunction.

假设您的系统中有三个GPU,但是您只想使用两个进行计算。获取设备的索引。

[可用GPU,gpuindx] = gpudevicecount("available")
availableGPUs = 3
gpuIndx =1×31 2 3

定义要使用的设备的指标。

useGPUs = [1 3];

Start your parallel pool. Use anspmdblock andgpuDeviceto associate each worker with one of the GPUs you want to use, using the device index. Thelabindexfunction identifies the index of each worker.

Parpool('local',numel(useGPUs));
开始平行池(parpool)使用the 'local' profile ... Connected to the parallel pool (number of workers: 2).
spmdgpudevice(usegpus(labindex));end

作为最佳实践,并且为了获得最佳性能,请为每个工人分配不同的GPU。

完成计算后,关闭并行池。

删除(gcp('nocreate'));

使用默认配置文件创建一个并行池,然后删除池。

poolobj = parpool; delete(poolobj)

Find the number of workers in the current parallel pool.

poolobj = gcp('nocreate');% If no pool, do not create new one.如果是empty(poolobj) poolsize = 0;elsepoolsize = poolobj.NumWorkersend

Input Arguments

全部收缩

Size of the parallel pool, specified as a positive integer or a range specified as a 2-element vector of integers. Ifpoolsize是一个范围,在所需的范围内,生成的池的大小尽可能大。在并行首选项或并行配置文件中设置默认优先级的工人数量。

Example:Parpool('local',2)

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

开始池的资源,指定为'local','threads', 一种cluster profile name or cluster object.

Example:Parpool('local')

Example:parpool('threads')

Example:Parpool('myClusterProfile',16)

Example:c = parcluster; parpool(c)

Data Types:char|string|parallel.Cluster

Name-Value Arguments

将可选的参数对Name1=Value1,...,NameN=ValueN, whereName是the argument name and价值是the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:“ entactedfiles',{'myfun.m'}

要连接到池的文件,被指定为字符向量,字符串或字符串数​​组或字符向量的单元格数组。

With this argument pair,parpool启动一个并行池,并将确定的文件传递给池中的工人。此处指定的文件附加到附加的文件property specified in the applicable parallel profile to form the complete list of attached files. The'AttachedFiles'属性名称对案例敏感,必须如图所示。

Example:{'myFun.m','myFun2.m'}

Data Types:char|细胞

Flag to specify if user-added entries on the client path are added to path of each worker at startup, specified as a logical value.

Data Types:logical

环境变量的名称将从客户端会话复制到工人,该工人指定为字符向量,字符串或字符串数​​组或字符向量的单元格数组。此处指定的名称附加到'环境变量'在适用的并行配置文件中指定的属性以形成环境变量的完整列表。未设置的任何变量均未复制给工人。这些环境变量在平行池的持续时间内设置在工人上。

Data Types:char|细胞

Flag to specify ifspmd万博1manbetx支持在池中启用,指定为逻辑值。您只能在本地或MATLAB作业调万博1manbetx度程序群集上禁用支持。parforiterations do not involve communication between workers. Therefore, if'SpmdEnabled'false, 一种parfor循环继续,即使一个或多个工人中止during loop execution.

Data Types:logical

Time in minutes after which the pool shuts down if idle, specified as an integer greater than zero. A pool is idle if it is not running code on the workers. By default'IdleTimeout'是the same as the value in your parallel preferences. For more information on parallel preferences, see指定您的平行首选项

Example:pool = parpool('IdleTimeout',120)

Output Arguments

全部收缩

Access to parallel pool from client, returned as aParallel.Pool目的。

Tips

  • 桌面左下角的池状态指示器显示了客户端会话连接到池和池状态。单击图标以获取支持的池操作菜单。万博1manbetx

    With a pool running:With no pool running:

  • If you set your parallel preferences to automatically create a parallel pool when necessary, you do not need to explicitly call theparpoolcommand. You might explicitly create a pool to control when you incur the overhead time of setting it up, so the pool is ready for subsequent parallel language constructs.

  • 删除(poolobj)shuts down the parallel pool. Without a parallel pool,spmdparforrun as a single thread in the client, unless your parallel preferences are set to automatically start a parallel pool for them.

  • When you use the MATLAB editor to update files on the client that are attached to a parallel pool, those updates automatically propagate to the workers in the pool. (This automatic updating does not apply to Simulink®模型文件。要向工人传播更新的模型文件,请使用updateAttachedFilesfunction.)

  • If possible, the working folder on the workers is initially set to match that of the MATLAB client session. Subsequently, the following commands entered in the client Command Window also execute on all the workers in the pool:

    This behavior allows you to set the working folder and the command search path on all the workers, so that subsequent pool activities such asparfor-loops execute in the proper context.

    When changing folders or adding a path withcd或者addpathon clients with Windows®operating systems, the value sent to the workers is the UNC path for the folder if possible. For clients with Linux®operating systems, it is the absolute folder location.

    If any of these commands does not work on the client, it is not executed on the workers either. For example, ifaddpath指定客户端无法访问的文件夹addpathcommand is not executed on the workers. However, if the working folder can be set on the client, but cannot be set as specified on any of the workers, you do not get an error message returned to the client Command Window.

    Be careful of this slight difference in behavior in a mixed-platform environment where the client is not the same platform as the workers, where folders local to or mapped from the client are not available in the same way to the workers, or where folders are in a nonshared file system. For example, if you have a MATLAB client running on a Microsoft®Windows操作系统在MATLAB工人都在Linux操作系统上运行时,相同的参数addpath不能同时工作。在这种情况下,您可以使用该功能pctRunOnAllto assure that a command runs on all the workers.

    客户和工人之间的另一个区别是addpatharguments that are part of theMatlabrootfolder are not set on the workers. The assumption is that the MATLAB install base is already included in the workers’ paths. The rules foraddpathregarding workers in the pool are:

    • Subfolders of theMatlabrootfolder are not sent to the workers.

    • 第一次出现之前出现的任何文件夹Matlabrootfolder are added to the top of the path on the workers.

    • Any folders that appear after the first occurrence of aMatlabrootfolder are added after theMatlabrootgroup of folders on the workers’ paths.

    For example, suppose thatMatlabrooton the client isC:\ applications \ matlab \。With an open parallel pool, execute the following to set the path on the client and all workers:

    addpath('P1','P2','C:\Applications\matlab\T3','C:\Applications\matlab\T4','P5','C:\Applications\matlab\T6','P7','P8');

    BecauseT3,T4, 和T6are subfolders ofMatlabroot, they are not set on the workers’ paths. So on the workers, the pertinent part of the path resulting from this command is:

    P1 P2  P5 P7 P8
  • If you are usingMacintosh或Linux,并在大型平行池创建期间看到问题,请参阅Macintosh和Linux的推荐系统限制

Version History

在R2013B中引入