Documentation

spmd

执行代码在票面价值allel on workers of parallel pool

Syntax

SPMD,语句,结束
spmd(n), statements, end
spmd(m,n), statements, end

Description

The general form of anspmd(single program, multiple data) statement is:

SPMD语句结束

SPMD,语句,结束defines anspmdstatement on a single line. MATLAB®executes thespmdbody denoted by语句on several MATLAB workers simultaneously. Thespmdstatement can be used only if you have Parallel Computing Toolbox. To execute the statements in parallel, you must first open 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) can transfer data between the workers.

Values returning from the body of anspmdstatement are converted toCompositeobjects on the MATLAB client. A Composite object contains references to the values stored on the remote MATLAB workers, and those values can be retrieved using cell-array indexing. The actual data on the workers remains available on the workers for subsequentspmdexecution, so long as the Composite exists on the client and the parallel pool remains open.

By default, MATLAB uses all workers in the pool. When there is no pool active, MATLAB will create a pool and use all the workers from that pool. If your preferences do not allow automatic pool creation, MATLAB executes the block body locally and creates Composite objects as necessary. You cannot execute anspmdblock if any worker is busy executing aparfeval请求,除非您使用spmd(0)

spmd(n), statements, end用途nto specify the exact number of MATLAB workers to evaluate语句, provided thatnworkers are available from the parallel pool. If there are not enough workers available, an error is thrown. Ifnis zero, MATLAB executes the block body locally and creates Composite objects, the same as if there is no pool available.

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.

For more information aboutspmd和Composite objects, seeDistribute Arrays and Run SPMD

不te

UseparfevalOnAllinstead ofparfor或者spmdif you want to use清除。This preserves workspace transparency. See确保Parfor-Loops的透明度

Examples

Perform a simple calculation in parallel, and plot the results:

Parpool(3)spmd% build magic squares in parallelQ =魔术(LabIndex + 2);endforii=1:length(q)%绘制每个魔术广场figure, imagesc(q{ii});enddelete(gcp)

Tips

  • Anspmdblock runs on the workers of the existing parallel pool. If no pool exists,spmdwill start a new parallel pool, unless the automatic starting of pools is disabled in your parallel preferences. If there is no parallel pool andspmdcannot start one, the code runs serially in the client session.

  • If theAutoAttachFiles平行池的集群配置文件中的属性设置为真的,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

Introduced in R2008b

这个话题有帮助吗?