spmdReduce - Reduce arrays on spmd workers - MATLAB (original) (raw)

Reduce arrays on spmd workers

Since R2022b

Syntax

Description

`B` = spmdReduce([fcn](#mw%5Fdf39521d-c6e3-43d9-a10b-bb4e3e83654c),[A](#mw%5Fc731155a-5dbe-4e87-9498-4af97cc13939)) uses the function fcn to reduce the array A defined on each worker running an spmd block or communicating job. The function stores the result B of reduction on every worker.

For example, spmdReduce(@plus,A) returns the sum of the arraysA defined on each worker.

MATLABĀ® uses the fcn function to reduce_A_J by calling the function N - 1 times.

To ensure that your spmd block or communicating job always produces the same results, specify fcn as an associative function.

When you use parfor, parfeval, orparfevalOnAll to run code on a parallel pool, the workers are independent and do not communicate with each other. If you usespmdReduce on these workers, the result is the same as usingspmdReduce on a client.

If one worker is running the current spmd block,B is equal to A.

example

`B` = spmdReduce([fcn](#mw%5Fdf39521d-c6e3-43d9-a10b-bb4e3e83654c),[A](#mw%5Fc731155a-5dbe-4e87-9498-4af97cc13939),[destination](#mw%5Fd4ccdf9e-996e-4ea5-9af9-ed0654adba91)) reduces A and stores the result on only one worker.

Examples

collapse all

This example shows how to use spmdReduce to calculate the maximum value of an array across all workers.

Create a parallel pool with four workers.

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

Run spmdIndex on each worker in the spmd block and store the result in A. Use spmdReduce and max to calculate the maximum value ofA from each worker.

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

spmd A = spmdIndex; B = spmdReduce(@max,A); end disp(B{1})

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, multidimensional array, table, timetable, or any MATLAB variable that supports concatenation.

Example: A = magic(3)

Reduction function, specified as a function handle. The reduction function must take two input arguments.

Example: fcn = @max

Data Types: function_handle

Index of the destination worker, specified as a positive integer or an empty array. 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 B only on the worker with this index. On the worker whose index is equal todestination, B is the result of the operation. On all other workers, B is [].

Example: 1

Algorithms

This figure shows how the spmdReduce function usesfcn when you call spmdReduce(fcn,A).

Diagram showing how four workers combine arrays specified as A into a single array, B.

Extended Capabilities

expand all

The spmdReduce function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray. For more information, see Run MATLAB Functions on a GPU.

If x is a gpuArray, data transfer between multiple GPUs in a parallel pool uses fast peer-to-peer communication, including NVLink, if available.

Version History

Introduced in R2022b