MPI_Send和MPI_Recv的行为 [英] Behavior of MPI_Send and MPI_Recv
本文介绍了MPI_Send和MPI_Recv的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这些代码行:
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);
}
}
给出这个结果吗?
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...
不是每个进程都有它的"msg"副本吗?
Doesn't each process have its copy of 'msg' ?
推荐答案
strlen()
不包含空终止符,因此不会将其发送给主服务器.从等级3接收消息不会覆盖字符串的后半部分,因此它仍然显示.您应该使用strlen(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.
这篇关于MPI_Send和MPI_Recv的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文