Main Content

ticBytes

Start counting bytes transferred within parallel pool

    Description

    ticBytes starts counting the number of bytes transferred by each worker in the current parallel pool, so that later tocBytes can measure the amount of data (and associated metadata) each worker transfers between the two calls. The current parallel pool is the parallel.Pool object the gcp function with the "noCreate" option returns. (since R2024a)

    You must have a parallel pool running before you run the ticBytes function.

    • If you call ticBytes when no parallel pool is running, ticBytes operates on an empty pool with no workers and cannot count bytes transferred. In this case, when you call tocBytes later, MATLAB® returns an empty table.

    • If you call ticBytes when no pool is running and then start a parallel pool, ticBytes does not operate on the newly created pool, because the pool did not exist when you called ticBytes. When you call tocBytes later, MATLAB returns an empty table.

    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

    ticBytes(pool) starts counting 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

    startState = ticBytes and startState = ticBytes(pool) saves the state to a TicBytesResult object, startState, so that you can simultaneously record the number of bytes transferred for multiple pairs of ticBytes and tocBytes calls. Use the value of startState as an input argument for a subsequent call to tocBytes.

    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 the parpool function or partition a pool from an existing parallel pool using the partition function. (since R2025a)

    Example: pool = parpool("Processes");

    Output Arguments

    collapse all

    Starting state returned as a TicBytesResult object. Use the TicBytesResult object as an input argument for a subsequent call to tocBytes.

    Example: startState = ticBytes;

    Limitations

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

    Version History

    Introduced in R2016b

    expand all