Main Content

spmdPlus

Add arrays from workers in spmd block

Since R2022b. Recommended over gplus.

    Description

    C = spmdPlus(A) add the arrays A stored on each worker in the current spmd block or communicating job and stores the result C on all the workers.

    When you offload computations using parfor and parfeval, only one worker at a time runs each computation. These workers are independent and do not communicate with each other. If you apply spmdPlus to these workers, the function has no effect.

    You can use spmdPlus only when A has a compatible size on each worker.

    If source is equal to the worker index, C is equal to A.

    example

    C = spmdPlus(A,destination) adds the arrays and stores the result on only one worker. On the worker whose index is equal to destination, C is equal to the sum. On all other workers, C is []. To get the worker index, use the spmdIndex function.

    Examples

    collapse all

    This example shows how to use spmdPlus to add a scalar held on each worker in an spmd block.

    Create a parallel pool with four workers.

    parpool(4);

    When you execute an spmd block after creating a parallel pool, by default all available workers in the pool run the code inside the spmd block.

    Run spmdIndex on each worker in the spmd block and store the result in a scalar A. Use spmdPlus to add the value of A from each worker.

    Unless you specify a destination, the spmdPlus function stores the result on every worker. On the client, C is a Composite object. To get the result, index into the Composite object.

    spmd
        A = spmdIndex;
        C = spmdPlus(A);
    end
    disp(C{1})
         10

    Input Arguments

    collapse all

    Input array, specified as a scalar, vector, matrix, or multidimensional array.

    Example: magic(3)

    Index of the target worker, specified as a positive integer. The value of this input must be less than or equal to the number of workers running the current spmd block or communicating job.

    When you specify this input, the function stores the value of C on only one worker. On the worker whose index is equal to destination, C is equal to the sum. On all the other workers, C is [].

    Example: 1

    Output Arguments

    collapse all

    Sum of arrays on each worker in an spmd block or communicating job, returned as any MATLAB® variable of the same type as A.

    If you specify destination, the function stores the value of C on the worker whose index is equal to destination. On all the other workers, C is [].

    Algorithms

    This figure shows how the spmdCat function adds A to the value on other workers when you call spmdPlus(A).

    Figure shows how four workers combine arrays specified as A into a single array, C.

    Extended Capabilities

    Version History

    Introduced in R2022b