上下文:在我的程序中,主进程为从进程分配工作。一旦从进程完成了工作,它就会请求主进程提供更多的工作。主人给奴隶分配了更多的工作,程序继续进行。
我编写的程序是这样一种方式,即主进程使用MPI_Recv和MPI_ANY_SOURCE从从节点接收工作。
/* Allot some work to all the slaves (seed) */
while (istheremorework()) {
/* Master receives slaves work*/
MPI_Status status;
MPI_Recv(recvbuf, width + 1, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int slave = status.MPI_SOURCE;
cout << "Master recieved data from slave " << slave << endl;
/* store the data */
/* Master sends new work to the slave */
int* sendbuf;
/* populate sendbuf */
MPI_Send(&sendbuf, 2, MPI_INT, slave, MSG_TAG_DATA, MPI_COMM_WORLD);
/* update remaining work information */;
}代码的这一部分也可以重写为
/* Allot some work to all the slaves (seed) */
/* Open a channel with all the slaves to receive their work. */
for (int k = 1; k < i; k++) {
MPI_Irecv(&recbuf[k], BUFFER_LENGTH, MPI_DOUBLE, k, MSG_TAG, MPI_COMM_WORLD, &requests[k - 1]);
/* Each slave sends the results to master */
}
while (istheremorework()) {
/* Master receives slaves work*/
MPI_Waitany(np-1, requests, &index, statuses);
/* Using index to decide which slave sent the result */
cout << "Master received data from slave " << slave << endl;
/* store the data */
/* Master sends new work to the slave */
int* sendbuf;
/* populate sendbuf */
MPI_Send(&sendbuf, 2, MPI_INT, slave, MSG_TAG_DATA, MPI_COMM_WORLD);
/* update remaining work information */;
}这两种方法在性能上是否等价?你认为使用一种比另一种更有好处吗?
发布于 2017-05-23 04:56:40
在Waitany版本中,您必须为主进程上的每个工作人员初始化和维护一个MPI_Request请求对象,并且必须在MPI_Waitany()调用中遍历所有这些对象。使用MPI_Recv(MPI_ANY_SOURCE),您只需处理消息队列中的下一条消息。我怀疑MPI_Recv版本会更好。
您可以尝试性能分析器来确定。对于中小型规模,在这种情况下可能不会有很大的性能差异。然而,在大规模的情况下,同时分配多个请求对象被认为是个坏主意。
https://stackoverflow.com/questions/44103019
复制相似问题