【问题标题】:MPI functions appear to execute out of order compared to printf() [duplicate]与 printf() 相比,MPI 函数似乎无序执行 [重复]
【发布时间】:2021-10-11 01:47:33
【问题描述】:

我有一个使用 MPI 的非常奇怪的代码,其中的语句似乎以错误的顺序执行。具体来说,MPI 语句似乎在 printf 之前执行,即使它在代码中出现在它之后。

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    int numProcs, rank, data;
    MPI_Status status;
    
    // Initialize the MPI library
    MPI_Init(&argc, &argv);

    // Get entity identification
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // Do something different in each a rank
    if (rank == 0) {
        // Get the data from rank   1
    // with tag                    0
    printf("rank = %d\tGet the data from rank 1 with tag 0\n", rank);
    MPI_Recv(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &status);
    } else if (rank == 1) {
        // Send the data to rank    0
    // with tag                    0
    printf("rank = %d\tSend the data to rank 0 with tag 0\n", rank);
        MPI_Send(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
    }

    printf("rank %d finishing\n", rank);

    // Clean up the MPI library
    MPI_Finalize();

    return 0;
}

这是正在生成的输出:

$ mpirun -n 2 ./a.out
rank = 0    Get the data from rank 1 with tag 0
rank 0 finishing
rank = 1    Send the data to rank 0 with tag 0
rank 1 finishing

似乎等级 0 执行 printf,然后它从等级 1 获取数据,然后完成……然后等级 1 执行 printf?但是既然rank 1在真正将数据发送到rank 0之前必须执行printf,那么rank 0怎么可能已经拿到数据并完成了呢?

【问题讨论】:

  • 没有全局顺序,每个 MPI 任务都以自己的速度打印输出,因此您应该期望不同任务的输出是交错的。

标签: printf mpi non-deterministic


【解决方案1】:

屏幕输出被操作系统缓冲,然后必须通过 ssh 隧道到达启动 MPI 运行的进程。结果,屏幕输出可以以各种顺序到达。除了将所有文本发送到进程零并以正确的顺序从那里打印之外,基本上没有办法获得整齐有序的屏幕输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多