Behavior of MPI_Send and MPI_Recv

做~自己de王妃 提交于 2020-01-11 14:30:11

问题


Why these lines of code:

if(my_rank != 0) {
    sprintf(msg, "Hello from %d of %d...", my_rank, comm_sz);
    if(my_rank == 2) {
        sleep(2);
        sprintf(msg, "Hello from %d of %d, I have slept 2 seconds...", my_rank, comm_sz);
    }
    MPI_Send(msg, strlen(msg), MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
else {
    printf("Hello from the chosen Master %d\n", my_rank);
    for(i = 1; i < comm_sz; i++) {
        MPI_Recv(msg, MAX_STRING, MPI_CHAR, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        printf("%s\n", msg);
    }
}

give this result?

Hello from the chosen Master 0  
Hello from 1 of 5...  
Hello from 2 of 5, I have slept 2 seconds...  
Hello from 3 of 5... have slept 2 seconds...  
Hello from 4 of 5... have slept 2 seconds...

Doesn't each process have its copy of 'msg' ?


回答1:


strlen() does not include the null terminator, hence it will not be sent to the master. Receiving the message from rank 3 will not overwrite the later part of the string, so it is still displayed. You should use strlen(msg) + 1 as send count.



来源:https://stackoverflow.com/questions/44167437/behavior-of-mpi-send-and-mpi-recv

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