gpt4 book ai didi

c - MPI:当预期的 MPI_Recv 数量未知时该怎么办

转载 作者:太空狗 更新时间:2023-10-29 15:35:24 26 4
gpt4 key购买 nike

我有很多从节点,它们可能会也可能不会向主节点发送消息。所以目前主节点无法知道预期有多少 MPI_Recv。出于效率原因,从节点必须向主节点发送最少数量的消息。

我设法找到了 a cool trick ,当不再需要任何消息时,它会发送一条额外的“完成”消息。不幸的是,在我的情况下它似乎不起作用,因为发件人的数量是可变的。关于如何去做的任何想法?谢谢!

if(rank == 0){ //MASTER NODE

while (1) {

MPI_Recv(&buffer, 10, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);

if (status.MPI_TAG == DONE) break;


/* Do stuff */
}

}else{ //MANY SLAVE NODES

if(some conditions){
MPI_Send(&buffer, 64, MPI_INT, root, 1, MPI_COMM_WORLD);
}

}


MPI_Barrier(MPI_COMM_WORLD);
MPI_Send(NULL, 1, MPI_INT, root, DONE, MPI_COMM_WORLD);

不工作,程序似乎还在等待 MPI_Recv

最佳答案

一个更简单、更优雅的选择是使用 MPI_IBARRIER。让每个工作人员调用它需要的所有发送,然后在完成后调用 MPI_IBARRIER。在主机上,您可以在 MPI_ANY_SOURCE 上的 MPI_IRECVMPI_IBARRIER 上循环。当 MPI_IBARRIER 完成时,您知道每个人都已完成,您可以取消 MPI_IRECV 并继续。伪代码看起来像这样:

if (master) {
/* Start the barrier. Each process will join when it's done. */
MPI_Ibarrier(MPI_COMM_WORLD, &requests[0]);

do {
/* Do the work */
MPI_Irecv(..., MPI_ANY_SOURCE, &requests[1]);

/* If the index that finished is 1, we received a message.
* Otherwise, we finished the barrier and we're done. */
MPI_Waitany(2, requests, &index, MPI_STATUSES_IGNORE);
} while (index == 1);

/* If we're done, we should cancel the receive request and move on. */
MPI_Cancel(&requests[1]);
} else {
/* Keep sending work back to the master until we're done. */
while( ...work is to be done... ) {
MPI_Send(...);
}

/* When we finish, join the Ibarrier. Note that
* you can't use an MPI_Barrier here because it
* has to match with the MPI_Ibarrier above. */
MPI_Ibarrier(MPI_COMM_WORLD, &request);
MPI_Wait(&request, MPI_STATUS_IGNORE);
}

关于c - MPI:当预期的 MPI_Recv 数量未知时该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30279457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com