运行时的“MPI_Bcast”

发布于 2024-11-02 07:31:34 字数 264 浏览 1 评论 0原文

在运行时决定广播者的情况下如何进行 MPI_Bcast?在这种情况下如何指定根节点?

我正在尝试在数组中搜索不同的数字。如果节点找到该号码,则它应该向所有其他节点广播其位置。但是,由于我事先不知道查找器,“root”值应该在哪里:

int MPI_Bcast ( void *buffer, int count, MPI_Datatype datatype, 
                int root, MPI_Comm comm );

How do I do an MPI_Bcast in the case where the broadcaster is decided at runtime? How do I specify the root node in this case?

I'm trying to search for a distinct number in an array. If a node finds the number, then it should broadcast its location to all other nodes. However, as I don't know the finder beforehand, what should the "root" value be in:

int MPI_Bcast ( void *buffer, int count, MPI_Datatype datatype, 
                int root, MPI_Comm comm );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水微香 2024-11-09 07:31:35

每个人在进入集体之前都必须就“根”达成一致,因此事先必须进行一些协作。这是一种简单的方法 - 每个人都发送一个标志,指示他们是否拥有相关数据,然后每个人都可以同意从谁那里接收数据。这使您可以处理有多个可能的发件人或没有发件人的情况。

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char **argv) {
    int rank, size, ierr;
    int bcaster;
    int bcasterflag, *allflags;
    int consensus;
    int globaldata, mydata;

    ierr = MPI_Init(&argc, &argv);
    ierr|= MPI_Comm_size(MPI_COMM_WORLD,&size);
    ierr|= MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    if (argc != 2) {
        if (rank == 0) fprintf(stderr,"Usage: %s rank-to-broadcast\n",argv[0]);
        MPI_Abort(MPI_COMM_WORLD,1);
    }

    bcaster = atoi(argv[1]);
    if (bcaster < 0    ) bcaster = 0;
    if (bcaster >= size) bcaster = size-1;

    /* pretend the processes didn't all know the above and had to
     * rely solely on local info to decide the broadcaster
     */

    bcasterflag = 0;     /* not the broadcaster yet */

    mydata = rank*rank;   /* the local data */
    if (mydata == bcaster*bcaster) {
        bcasterflag = 1;
        globaldata = mydata;
    }


    /* collect these local decisions */

    allflags = (int *)malloc(size * sizeof(int));
    ierr = MPI_Allgather(&bcasterflag, 1, MPI_INT,
                         allflags, 1, MPI_INT, MPI_COMM_WORLD);

    consensus = -1;
    for (int i=0; i<size; i++)
        if (allflags[i] != 0) consensus = i;

    if (consensus == -1) {
       if (rank == 0) {
          fprintf(stderr,"Error: no one found to do the broadcast.\n");
       }
    } else {
        ierr = MPI_Bcast(&globaldata, 1, MPI_INT, consensus, MPI_COMM_WORLD);
    }

    printf("%d: Received data %d from %d\n",
            rank, globaldata, consensus);

    return 0;
}

Everyone has to agree on "root" before going into the collective, so there has to be some collaboration before hand. Here's one straightforward approach - everyone sends a flag indicating whether or not they have the relevant data, and then everyone can agree who to receive from. This allows you to handle the cases of when there's multiple possible senders or none.

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char **argv) {
    int rank, size, ierr;
    int bcaster;
    int bcasterflag, *allflags;
    int consensus;
    int globaldata, mydata;

    ierr = MPI_Init(&argc, &argv);
    ierr|= MPI_Comm_size(MPI_COMM_WORLD,&size);
    ierr|= MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    if (argc != 2) {
        if (rank == 0) fprintf(stderr,"Usage: %s rank-to-broadcast\n",argv[0]);
        MPI_Abort(MPI_COMM_WORLD,1);
    }

    bcaster = atoi(argv[1]);
    if (bcaster < 0    ) bcaster = 0;
    if (bcaster >= size) bcaster = size-1;

    /* pretend the processes didn't all know the above and had to
     * rely solely on local info to decide the broadcaster
     */

    bcasterflag = 0;     /* not the broadcaster yet */

    mydata = rank*rank;   /* the local data */
    if (mydata == bcaster*bcaster) {
        bcasterflag = 1;
        globaldata = mydata;
    }


    /* collect these local decisions */

    allflags = (int *)malloc(size * sizeof(int));
    ierr = MPI_Allgather(&bcasterflag, 1, MPI_INT,
                         allflags, 1, MPI_INT, MPI_COMM_WORLD);

    consensus = -1;
    for (int i=0; i<size; i++)
        if (allflags[i] != 0) consensus = i;

    if (consensus == -1) {
       if (rank == 0) {
          fprintf(stderr,"Error: no one found to do the broadcast.\n");
       }
    } else {
        ierr = MPI_Bcast(&globaldata, 1, MPI_INT, consensus, MPI_COMM_WORLD);
    }

    printf("%d: Received data %d from %d\n",
            rank, globaldata, consensus);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文