主要内容

本页采用了机器翻译。点击此处可查看英文原文。

ticBytes

开始计算并行池中传输的字节数

    说明

    ticBytes 开始统计当前并行池中每个工作单元传输的字节数,以便后续 tocBytes 能够测量每个工作单元在两次调用之间传输的数据量(及相关元数据)。当前的并行池是 gcp 函数在 "noCreate" 选项下返回的 parallel.Pool 对象。 (自 R2024a 起)

    在运行 ticBytes 函数之前,必须先有一个并行池正在运行。

    • 如果在没有并行池运行时调用 ticBytesticBytes 将对一个没有工作单元的空池进行操作,因此无法统计传输的字节数。在这种情况下,当您稍后调用 tocBytes 时,MATLAB® 会返回一个空表。

    • 如果在没有运行任何池的情况下调用 ticBytes,然后启动一个并行池,那么 ticBytes 不会对新创建的池进行操作,因为在调用 ticBytes 时该池尚不存在。当您稍后调用 tocBytes 时,MATLAB 会返回一个空表。

    ticBytestocBytes 函数可配合 parforspmd 以及 parfeval 等函数,帮助您在并行执行过程中跟踪客户端与工作单元之间的数据流动。利用这些函数来了解通信开销并优化您的代码。

    示例

    ticBytes(pool) 开始统计由 ProcessPoolClusterPool 对象指定的并行池中每个工作单元传输的字节数,即 pool

    若要测量 gcp 函数返回的池以外的池中的数据,请将 ticBytes(pool)tocBytes(pool) 调用结合使用。

    示例

    startState = ticBytesstartState = ticBytes(pool) 将状态保存到一个名为 TicBytesResult 的对象(即 startState)中,这样您就可以同时记录多组 ticBytestocBytes 调用所传输的字节数。使用 startState 的值作为后续调用 tocBytes 的输入参量。

    示例

    示例

    全部折叠

    自 R2024a 起

    使用 Processes 配置文件创建一个并行池。

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

    测量每个工作单元在运行一个简单的 parfor 循环时传输的数据量。各个工作单元传输的字节数各不相同,因为每个工作单元执行的循环迭代次数不同。

    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          
    

    使用 Processes 配置文件创建一个并行池。

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

    测量由 parallel.Pool 对象所代表的并行池中的工作单元传输的数据量,即 pool。各个工作单元传输的字节数相同,因为每个工作单元执行的计算都是一样的。

    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          
    

    计算在运行多个 parfor-loop 时,每个工作单元传输的字节数的最小值和平均值。使用一对 ticBytestocBytes 调用来测量池中的工作单元在每个 parfor-loop 期间传输的字节数,并使用另一对调用来测量所有 parfor-loop 的总字节数。

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

    使用 ticBytes 开始跟踪总数据传输量。

    totalStart = ticBytes;  % ticBytes, pair 1

    使用一个 for-loop 运行多个 parfor-loop。在每个 parfor 循环中,改变数据变量 b 的大小。跟踪并统计每个工作单元在每个 parfor 循环中传输的数据量,并找出每个工作单元的最小数据传输量。

    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

    计算工作单元在运行 parfor-loops 时平均消耗的字节数。

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

    在表格中显示每个工作单元在运行 parfor-loops 时传输的字节数的最小值和平均值。

    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   
    

    输入参数

    全部折叠

    并行池,指定为 parallel.ProcessPoolparallel.ClusterPool 对象。

    要创建进程池或集群池,请使用 parpool 函数,或者使用 partition 函数从现有并行池中划分出一个池。 (自 R2025a 起)

    示例: pool = parpool("Processes");

    输出参量

    全部折叠

    作为 TicBytesResult 对象返回的初始状态。将 TicBytesResult 对象用作后续调用 tocBytes 的参量。

    示例: startState = ticBytes;

    限制

    • 基于线程的池不支持统计发送给或从工作单元接收的字节数。

    版本历史记录

    在 R2016b 中推出

    全部展开

    另请参阅

    | | | | |