Main Content

spmdProbe

Determine whether data is available to current worker in spmd block

    Description

    example

    tf = spmdProbe tests whether data is available to the current worker in an spmd block or communicating job. If it is 1, the worker can receive the data using spmdReceive.

    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 spmdProbe to these workers, the function has no effect.

    If the data is available, spmdProbe returns a logical 1 (true). Otherwise, spmdProbe returns a logical 0 (false).

    To use spmdProbe, the number of workers running the current spmd block must be greater than 1.

    tf = spmdProbe(source) tests whether data is available for the current worker to receive from the worker whose index is equal to source.

    tf = spmdProbe('any') tests whether the sent data is available for the current worker to receive from any worker.

    tf = spmdProbe('any',tag) tests whether data sent with the tag tag is available for the current worker to receive from any worker.

    tf = spmdProbe(source,tag) tests whether data sent with the tag tag is available for the current worker to receive from the worker whose index is equal to source.

    [tf,sourceOut,tagOut] = spmdProbe(___) tests whether data is available for the current worker to receive, returns the index of the worker that is sending data as sourceOut, and returns the tag of available data as tagOut. If multiple items of data are available, spmdProbe returns the index and tag of the first matching message.

    Examples

    collapse all

    This example shows how to determine whether data is available for workers in an spmd block or communicating job to receive.

    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. On the worker whose index is equal to 1, create an array. Use spmdSend to send the array to the worker whose index is equal to 2.

    Use spmdBarrier to synchronise the workers after the worker whose index is equal to 1 sends the data. Then, use spmdProbe to test whether the data is available for each worker to receive. Use spmdReceive to collect the available data.

    spmd
        switch spmdIndex
            case 1
                A = magic(3);
                spmdSend(A,2);
        end
        
        spmdBarrier;
    
        tf = spmdProbe
        
        if tf
            spmdReceive;
        end
    end
    Worker 1: 
      
      tf =
      
        logical
      
         0
      
    Worker 2: 
      
      tf =
      
        logical
      
         1
      
    Worker 3: 
      
      tf =
      
        logical
      
         0
      
    Worker 4: 
      
      tf =
      
        logical
      
         0
      

    Input Arguments

    collapse all

    Index of the source worker, specified as a positive integer or 'any'. 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 as a positive integer, spmdProbe returns a logical 1 (true) if data is available for the current worker to receive from the worker whose index is equal to source.

    When you specify this input as 'any', spmdProbe returns a logical 1 (true) if the data is available for the current worker to receive from any worker.

    When you do not specify this input, spmdProbe returns a logical 1 (true) if the data is available for the current worker to receive from any worker.

    Example: 1

    Message tag, specified as a nonnegative integer. When you specify this input, spmdProbe returns a logical 1 (true) if the data sent using spmdSend with the tag equal to tag is available for the current worker to receive.

    Example: 314159

    Output Arguments

    collapse all

    Index of the worker sending data that the current worker is yet to receive, returned as a positive integer or an empty array. The value is equal to index of the worker that sent data.

    If no data is available to be received, sourceOut is [].

    Message tag of the data that the current worker has not yet received, returned as a positive integer or an empty array. If no data is available to be received, tagOut is [].

    Extended Capabilities

    Version History

    Introduced in R2022b