Contenido principal

tocBytes

Read how many bytes have been transferred since calling ticBytes

    Description

    tocBytes reads and displays the number of bytes transferred by each worker in the current parallel pool after the most recent execution of ticBytes. The current parallel pool is the parallel.Pool object the gcp function with the "noCreate" option returns. (since R2024a)

    The ticBytes and tocBytes functions together help you track data movement between the client and workers during parallel execution with functions like parfor, spmd, and parfeval. Use these functions to understand communication overhead and optimize your code.

    example

    tocBytes(pool) reads and displays the number of bytes transferred by each worker in the parallel pool specified by the ProcessPool or ClusterPool object, pool.

    Use ticBytes(pool) and tocBytes(pool) calls together when you want to measure data on a pool other than the pool the gcp function returns.

    example

    tocBytes(startState) and tocBytes(pool,startState) read and displays the total number of bytes transferred in the parallel pool after the start state represented by the TicBytesResult object, startState. Use the ticBytes function to generate the TicBytesResult object.

    example

    bytes = tocBytes(___) returns the number of bytes transferred to and from each of the workers in the parallel pool for any of the previous syntaxes.

    example

    Examples

    collapse all

    Since R2024a

    Create a parallel pool using the Processes profile.

    parpool("Processes",4);
    Starting parallel pool (parpool) using the 'Processes' profile ...
    Connected to parallel pool with 4 workers.
    

    Measure the data each worker transfers while running a simple parfor-loop. Workers transfer different numbers of bytes, because each worker carries out different numbers of loop iterations.

    a = 0;
    b = rand(110);
    ticBytes;
    parfor idx = 1:110
        a = a + sum(b(:,idx));
    end
    tocBytes
                 BytesSentToWorkers    BytesReceivedFromWorkers
                 __________________    ________________________
    
        1                 40543                  4702          
        2                 34503                  4064          
        3                 30103                  4064          
        4                 36143                  4702          
        Total        1.4129e+05                 17532          
    

    Create a parallel pool using the Processes profile.

    pool = parpool("Processes",4);
    Starting parallel pool (parpool) using the 'Processes' profile ...
    Connected to parallel pool with 6 workers.
    

    Measure the amounts of data transferred by workers in the parallel pool represented by the parallel.Pool object, pool. Workers transfer the same number of bytes, because each worker carries out the same computation.

    ticBytes(pool)
    spmd
       r = zeros(1,40);
       for n = 1:40
          r(n) = rank(magic(n));
       end
    end
    r = gather(r);
    tocBytes(pool)
                 BytesSentToWorkers    BytesReceivedFromWorkers
                 __________________    ________________________
    
        1              11456                     9669          
        2              11456                     9669          
        3              11456                     9669          
        4              11456                     9669          
        Total          45824                    38676          
    

    Calculate the minimum and average number of bytes each worker transfers while running multiple parfor-loops. Use a pair of ticBytes and tocBytes calls to measure the number of bytes the pool workers transfer during each parfor-loop, and use another pair to measure the total number of bytes for all the parfor-loops.

    parpool(4);
    Starting parallel pool (parpool) using the 'Processes' profile ...
    Connected to parallel pool with 4 workers.
    
    reps = 10;
    minBytes = Inf;

    Use ticBytes to start tracking the total data transfer.

    totalStart = ticBytes;  % ticBytes, pair 1

    Run multiple parfor-loops using a for-loop. Vary the size of the data variable b in each parfor-loop. Track and retrieve the amount of data each worker transfers in each parfor-loop and find the minimum data transfer for each worker.

    for r = 1:reps
        a = 0;
        b = rand(100 + randi([0,100]),100);
    
        repStart = ticBytes;  % ticBytes, pair 2
        parfor idx = 1:100
            a = a + sum(b(:,idx));
        end
        bytes = tocBytes(repStart);  % tocBytes, pair 2
        minBytes = min(bytes,minBytes);
    end

    Calculate the average number of bytes the workers when running the parfor-loops.

    totalBytes = tocBytes(totalStart);  % tocBytes, pair 1
    averageBytes = totalBytes/reps;

    Display minimum and average number of bytes each worker transfers when running the parfor-loops in a table.

    BytesSentToWorkers = table(minBytes(:,1),averageBytes(:,1), ...
        VariableNames=["MinBytes","AverageBytes"]);
    BytesReceivedFromWorkers = table(minBytes(:,2),averageBytes(:,2), ...
        VariableNames=["MinBytes","AverageBytes"]);
    t = table(BytesSentToWorkers,BytesReceivedFromWorkers, ...
        VariableNames=["BytesSentToWorkers","BytesReceivedFromWorkers"]);
    disp(t)
           BytesSentToWorkers       BytesReceivedFromWorkers
        ________________________    ________________________
    
        MinBytes    AverageBytes    MinBytes    AverageBytes
        ________    ____________    ________    ____________
                                                            
         28415         36439          2788         3234.6   
         30991         40623          3426         3681.2   
         30991         38602          2788         3489.8   
         30991         36620          2150         3298.4   
    

    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("Processes");

    Starting state, specified as a TicBytesResult object. The TicBytesResult objects is returned by the ticBytes function.

    Example: startState = ticBytes;

    Output Arguments

    collapse all

    Bytes transferred, returned as a matrix of size numWorkers-by-2.

    Each row corresponds to a worker in the parallel pool.

    • The first column is the number of bytes sent from the client to the worker.

    • The second column is the number of bytes sent from the worker to the client.

    The bytes output contains raw byte values without headings. To view a formatted summary, including worker numbers, BytesSentToWorkers and BytesReceivedFromWorkers headings, and byte values, call tocBytes without an output argument. This displays the information directly in the Command Window.

    Data Types: double

    Limitations

    • Thread-based pools do not support measuring bytes sent to or received from workers.

    Extended Capabilities

    expand all

    Thread-Based Environment
    Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

    Version History

    Introduced in R2016b

    expand all