主要内容

spmd

Execute code in parallel on workers of parallel pool

Syntax

spmd语句end

描述

example

SPMD,语句, enddefines anspmd单行上的语句。MATLAB®executes thespmdbody denoted by语句同时在几个Matlab工人身上。每个工人都可以在不同的数据集或分布式数据的不同部分进行操作,并可以在执行并行计算时与其他参与的工人进行通信。这spmdstatement can be used only if you have Parallel Computing Toolbox™. To execute the statements in parallel, you must first create a pool of MATLAB workers usingparpool或者have your parallel preferences allow the automatic start of a pool.

Inside the body of thespmdstatement, each MATLAB worker has a unique value oflabindex, whilenumlabsdenotes the total number of workers executing the block in parallel. Within the body of thespmdstatement, communication functions for communicating jobs (such aslabSendlabReceive)可以在工人之间传输数据。

Values returning from the body of anspmdstatement are converted toCompositeMATLAB客户端上的对象。复合对象包含对存储在远程MATLAB工人上的值的引用,可以使用单元格索引检索这些值。工人的实际数据仍在工人身上可用spmd执行,只要该复合材料存在于客户端上并且平行池保持打开状态。

默认情况下,MATLAB使用池中的所有工人。如果没有池活动,MATLAB将创建一个池并使用该池中的所有工人。如果您的偏好不允许自动创建自动池,则MATLAB在本地执行块主体,并根据需要创建复合对象。你不能执行spmdblock if any worker is busy executing aparfeval请求,除非您使用SPMD(0)

For more information aboutspmd和Composite objects, see分发阵列并运行SPMD

Note

利用parfevalOnAllinstead ofparfor或者spmdif you want to use清除。This preserves workspace transparency. SeeEnsure Transparency in parfor-Loops or spmd Statements

example

SPMD(n),语句, end用途nto specify the exact number of MATLAB workers to evaluate语句, provided thatn工人可以从平行池中获得。如果没有足够的工人可用,则会出现错误。如果nis zero, MATLAB executes the block body locally and creates Composite objects, the same as if there is no pool available.

example

SPMD(m,n), statements, end用途a minimum ofm和a maximum ofn工人评估语句。如果没有足够的工人可用,则会出现错误。mcan be zero, which allows the block to run locally if no workers are available.

例子

全部收缩

Create a parallel pool, and perform a simple calculation in parallel usingspmd。MATLABexecutes the code inside thespmdon all workers in the parallel pool.

Parpool(3);
开始平行池(parpool)使用“本地”profile ... Connected to the parallel pool (number of workers: 3).
spmdQ =魔术(LabIndex + 2);end

Plot the results.

图子图(1,3,1),imagesc(q {1});子图(1,3,2),imagesc(q {2});子图(1,3,3),imagesc(q {3});

When you are done with computations, you can delete the current parallel pool.

delete(gcp);

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

要确定可在MATLAB中使用的GPU的数量,请使用gpudevicecountfunction.

可用= 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)使用“本地”profile ... Connected to the parallel pool (number of workers: 3).

要确定每个工人使用哪个GPU,请致电gpudevice内部spmdblock. Thespmd块运行gpudevice在每个工人上。

spmdgpudeviceend

使用平行语言功能,例如parfor或者parfeval,将您的计算分发给平行池中的工人。如果您使用gpuArrayenabled functions in your computations, these functions run on the GPU of the worker. For more information, seeRun MATLAB Functions on a GPU。For an example, seeRun MATLAB Functions on Multiple GPUs

完成计算后,关闭并行池。您可以使用gcpfunction to obtain the current parallel pool.

delete(gcp('nocreate'));

如果您想使用不同的GPU选择,则可以使用gpudevice使用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 andgpudevice使用设备索引将每个工人与要使用的GPU相关联。这labindexfunction identifies the index of each worker.

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

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

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

delete(gcp('nocreate'));

限制

提示

  • Anspmd块运行on the workers of the existing parallel pool. If no pool exists,spmd除非在您的并行首选项中禁用池的自动启动,否则将启动一个新的并行池。如果没有平行池,并且spmd无法启动一个,代码在客户端会话中串行运行。

  • 如果是AutoAttachFiles平行池的集群配置文件中的属性设置为真的,Matlab对spmdblock to determine what code files are necessary for its execution, then automatically attaches those files to the parallel pool job so that the code is available to the workers.

  • For information about restrictions and limitations when usingspmd, seeRun Single Programs on Multiple Data Sets

  • 有关性能的信息spmd和other parallel programming constructs, see在SPMD,PARFOR和PARFEVAL之间进行选择

Version History

Introduced in R2008b