【发布时间】: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