Main Content

spmdSendReceive

Simultaneously send and receive data on worker in spmd block

Since R2022b

    Description

    B = spmdSendReceive(destination,source,A) sends data A from the current worker in an spmd block or communicating job to the destination, and receives data from the source. The array A is sent from the current worker to the worker whose index is equal to destination. The current worker receives data B sent to the current worker from the worker whose index is equal to source.

    When you use this syntax, the computation is equivalent to the worker sending and receiving data by running these lines of code simultaneously:

    spmdSend(A,destination);
    B = spmdReceive(source);
    

    example

    B = spmdSendReceive(___,tag) sends and receives data with the tag tag. When you use spmdSendReceive to send data between workers, multiple items of data can wait to be collected. When you send multiple items of data to a worker, add a tag to each item to distinguish between the items.

    Examples

    collapse all

    This example shows how to use spmdSendReceive to send data between workers in an spmd block or communicating job.

    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.

    Create an spmd block. Use mod and spmdSendReceive to send and receive data in a chain of workers.

    Use spmdSendReceive to send data to the worker whose index is greater by one than the index of the current worker, modulo the number of workers running the current spmd block. Receive data from the worker with an index that is less by one than the index of the current worker, modulo the number of workers running the current spmd block.

    When you use modulo division, the worker whose index is equal to 1 receives data from the worker whose index is equal to the number of workers running the current spmd block.

    spmd
        A = 2*spmdIndex;
        
        destination = 1 + mod((spmdIndex+1)-1, spmdSize);
        source = 1 + mod((spmdIndex-1)-1, spmdSize);
        
        A = spmdSendReceive(destination,source,A)
    end
    Worker 1: 
      A =
          12
    Worker 2: 
      A =
           2
    Worker 3: 
      A =
           4
    Worker 4: 
      A =
           6
    Worker 5: 
      A =
           8
    Worker 6: 
      A =
          10
      

    Input Arguments

    collapse all

    Index of the destination worker, specified as a positive integer or an empty array. The destination worker receives data from the current worker. This input must be less than or equal to the number of workers running the current spmd block or communicating job.

    If this argument is an empty array, the function does not send any data.

    Example: 2

    Index of the source worker, specified as a positive integer or an empty array. The current worker waits until it receives data from the source worker. This value must be less than the number of workers running the current spmd block or communicating job.

    If this argument is an empty array, the function does not receive any data.

    Example: 1

    Data to send from the current worker, specified as a scalar, vector, matrix, multidimensional array, table, timetable, or any other MATLAB variable.

    Example: magic(3)

    Message tag, specified as a nonnegative integer. When you specify this input, spmdSendReceive sends data with the tag tag from the current worker, and returns data sent with the tag tag to the current worker.

    Example: 314159

    Tips

    Tags have many uses, for example:

    • Use tags to save memory by only loading arrays on workers when you need the data.

    • Use tags to create code that does not depend on the index of the sending worker.

    Extended Capabilities

    Version History

    Introduced in R2022b