首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPI_Waitany()还是MPI_Recv()与MPI_ANY_SOURCE?

MPI_Waitany()还是MPI_Recv()与MPI_ANY_SOURCE?
EN

Stack Overflow用户
提问于 2017-05-22 00:16:36
回答 1查看 614关注 0票数 0

上下文:在我的程序中,主进程为从进程分配工作。一旦从进程完成了工作,它就会请求主进程提供更多的工作。主人给奴隶分配了更多的工作,程序继续进行。

我编写的程序是这样一种方式,即主进程使用MPI_Recv和MPI_ANY_SOURCE从从节点接收工作。

代码语言:javascript
复制
/* 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 */;
}

代码的这一部分也可以重写为

代码语言:javascript
复制
/* 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 */;
}

这两种方法在性能上是否等价?你认为使用一种比另一种更有好处吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-23 04:56:40

在Waitany版本中,您必须为主进程上的每个工作人员初始化和维护一个MPI_Request请求对象,并且必须在MPI_Waitany()调用中遍历所有这些对象。使用MPI_Recv(MPI_ANY_SOURCE),您只需处理消息队列中的下一条消息。我怀疑MPI_Recv版本会更好。

您可以尝试性能分析器来确定。对于中小型规模,在这种情况下可能不会有很大的性能差异。然而,在大规模的情况下,同时分配多个请求对象被认为是个坏主意。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44103019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档