Ordering of cout weird: MPI_Recv before MPI_Send?

蓝咒 提交于 2019-12-13 02:13:44

问题


I have something like:

if (rank == winner) {
    ballPos[0] = rand() % 128;
    ballPos[1] = rand() % 64;
    cout << "new ball pos: " << ballPos[0] << " " << ballPos[1] << endl;
    MPI_Send(&ballPos, 2, MPI_INT, FIELD, NEW_BALL_POS_TAG, MPI_COMM_WORLD);
} else if (rank == FIELD) {
    MPI_Recv(&ballPos, 2, MPI_INT, winner, NEW_BALL_POS_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    cout << "2 new ball pos: " << ballPos[0] << " " << ballPos[1] << endl;
}

But I see in console:

new ball pos: 28 59
2 new ball pos: 28 59

Why isit that the cout after receive prints before the one before send?


回答1:


These are two different processes doing output at the same time. MPI implementations usually perform standard output redirection for all processes, but it is usually buffered in order to improve performance and to minimise network utilisation. The output from all processes is then sent to mpiexec (or to mpirun, or to whatever other command is used to launch the MPI job) and combined into its standard output. The order in which different chunks/lines from different processes end up in the output is mostly random, so you must not expect that a message from a certain rank would come up first unless some sort of process synchronisatoin is employed.

Also note that the MPI standard does not guarantee that it is possible for all ranks to write to the standard output. The standard provides the MPI_IO predefined attribute key that one can query on MPI_COMM_WORLD in order to obtain the rank of the process that is allowed to perform standard output. Most MPI implementations nowadays perform output redirection on all processes in the MPI job and thus return MPI_ANY_SOURCE for such attribute queries, but this is not guaranteed to always be the case.



来源:https://stackoverflow.com/questions/13307056/ordering-of-cout-weird-mpi-recv-before-mpi-send

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!