I am programming in MPI. I want to send something to another processor and receive it there, but I don't know how many messages I will send. In fact, the number of messages which send to the other processor depends on the file which I am reading it during the program, so I don't know how many receives I should write on the other side. Which method and which function should I use?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can still use sends and receives, but you would also add a new kind of message that tells the receiving process that there will be no new messages. Usually this is handled by sending with a different tag. So you program would look something like this:
if (sender) {
while (data_to_send == true) {
MPI_Send(data, size, datatype, receiving_rank, 0, MPI_COMM_WORLD);
}
for (i = 0; i < size; i++) {
MPI_Send(NULL, 0, MPI_INT, i, 1, MPI_COMM_WORLD);
}
} else {
while (1) {
MPI_Recv(data, size, datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == 1) break;
/* Do processing */
}
}
There is a better way that works if you have non-blocking collectives (from MPI-3). Before you start receiving data, you post a non-blocking barrier. Then you start posting non-blocking receives. Instead of waiting only on the receives, you use a waitany on both requests and when the barrier is done, you know here won't be any more data. On the sender side, you just keep sending data until there's no more, then do a non-blocking barrier to finish things off.