Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4381566
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:39:55+00:00 2026-05-21T12:39:55+00:00

How do I do an MPI_Bcast in the case where the broadcaster is decided

  • 0

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 );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:39:56+00:00Added an answer on May 21, 2026 at 12:39 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run this MPI Fortran code. There are several problems: 1) when
I am trying to run some tests using OPENmpi processing data in an array
I am trying to pass a dynamic 2d array with bcast to all ranks.
I'm actually writing an MPI program. This is a basic client / server pattern.
What would be the corresponding MPI receive routine of the broadcast routine, MPI_Bcast. Namely,
I'm on Windows XP2 32-bit machine. I'm trying to install MPICH2 & mpi4py. I've
I am trying to write a program in c using the MPI library. In
I installed Linpack on a 2-Node cluster with Xeon processors. Sometimes if I start
I have an MPI program where each node sends some values to some other
I'm trying to use MPI_Comm_spawn to start a second process. Just for demonstration pourposes.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.