Main Content

tocBytes

Read how many bytes have been transferred since calling ticBytes

    Description

    example

    tocBytes(pool) reads how many bytes have been transferred since calling ticBytes. The function displays the total number of bytes transferred to and from each of the workers in a parallel pool after the most recent execution of ticBytes.

    Use the ticBytes (pool) and tocBytes (pool) functions together to measure how much data is transferred to and from the workers in a parallel pool. You can use ticBytes and tocBytes while executing parallel language constructs and functions, such as parfor, spmd, or parfeval. Use ticBytes and tocBytes to pass around less data and optimize your code.

    example

    bytes = tocBytes(pool) returns the number of bytes transferred to and from each of the workers in the parallel pool.

    example

    tocBytes(pool,startState) displays the total number of bytes transferred in the parallel pool after the ticBytes command that generated startState.

    example

    bytes = tocBytes(pool,startState) returns the number of bytes transferred to and from each of the workers in the parallel pool after the ticBytes command that generated startState.

    Examples

    collapse all

    Use tocBytes(gcp,startS) to measure the amount of data transferred.

    a = 0;
    b = rand(100);
    startS = ticBytes(gcp);
    parfor i = 1:100
        a = a + sum(b(:, i));
    end
    tocBytes(gcp,startS)
    Starting parallel pool (parpool) using the 'local' profile ... 
    connected to 4 workers.
    
                 BytesSentToWorkers    BytesReceivedFromWorkers
                 __________________    ________________________
    
        1            42948              7156                   
        2            36548              7156                   
        3            27500              4500                   
        4            27500              4500                   
        Total    1.345e+05             23312                   
    

    Workers might transfer different numbers of bytes, because each worker might carry out different numbers of loop iterations.

    Use bytes = tocBytes(gcp) to measure the amount of data transferred.

    ticBytes(gcp);
    spmd
        rand(100);
    end
    bytes = tocBytes(gcp)
    bytes =
    
           13448        1208
           13448        1208
           13448        1208
           13448        1208          				   

    Workers transfer the same number of bytes, because each worker carries out the same number of loop iterations.

    Measure the minimum and average number of bytes transferred while running a parfor loop nested in a for loop.

    REPS = 10;   
    minBytes = Inf;   
    ticBytes(gcp);  % ticBytes, pair 1
    
    for ii=1:REPS
       a = 0;
       b = rand(100);
       startS = ticBytes(gcp)  % ticBytes, pair 2  
       parfor i = 1:100
           a = a + sum(b(:, i));
       end
       bytes = tocBytes(gcp, startS)  % tocBytes, pair 2  
       minBytes = min(bytes, minBytes)
    end
    
    averageBytes = tocBytes(gcp)/REPS  % tocBytes, pair 1 
              

    Note that nesting a parfor-loop in a for-loop can be slow due to overhead, see Convert Nested for-Loops to parfor-Loops.

    Input Arguments

    collapse all

    Parallel pool, specified as a parallel.ProcessPool or parallel.ClusterPool object.

    To create a process pool or cluster pool, use parpool.

    Example: pool = parpool('local');

    Starting state returned by ticBytes(pool).

    Example: startState = ticBytes(gcp);

    Output Arguments

    collapse all

    Bytes transferred, returned as a matrix of size numWorkers x 2. This matrix contains the number of bytes transferred to and from each of the workers in the parallel pool. bytes returns values in bytes without headings. Use tocBytes(pool) without an output argument to get Sent and Received headings, worker numbers, and values in bytes in the Command Window output.

    Example: bytes = tocBytes(pool);

    Introduced in R2016b