主要内容

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

tocBytes

读取自调用 ticBytes 以来已传输了多少字节

    说明

    tocBytes 读取并显示自上次执行 ticBytes 以来,当前并行池中每个工作单元传输的字节数。当前的并行池是 gcp 函数在 "noCreate" 选项下返回的 parallel.Pool 对象。 (自 R2024a 起)

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

    示例

    tocBytes(pool) 读取并显示由 ProcessPoolClusterPool 对象指定的并行池中的每个工作单元传输的字节数,即 pool

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

    示例

    tocBytes(startState)tocBytes(pool,startState) 读取并显示在由 TicBytesResult 对象(即 startState)表示的起始状态之后,并行池中传输的总字节数。使用 ticBytes 函数生成 TicBytesResult 对象。

    示例

    bytes = 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

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

    初始状态,指定为一个 TicBytesResult 对象。TicBytesResult 对象是由 ticBytes 函数返回的。

    示例: startState = ticBytes;

    输出参量

    全部折叠

    传输的字节数,以大小为 numWorkers-by-2 的矩阵形式返回。

    每一行对应并行池中的一个工作单元。

    • 第一列是客户端发送给工作单元的字节数。

    • 第二列是从工作单元发送到客户端的字节数。

    bytes 的输出包含未带标头的原始字节值。若要查看包含工作单元人数、BytesSentToWorkersBytesReceivedFromWorkers 标题以及字节值的格式化摘要,请在不带输出参量的情况下调用 tocBytes。这会将信息直接显示在命令行窗口中。

    数据类型: double

    限制

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

    扩展功能

    全部展开

    基于线程的环境
    使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

    版本历史记录

    在 R2016b 中推出

    全部展开

    另请参阅

    | | | | |