3

I am relatively new to MPI (with C), and am having some trouble using MPI_Bcast to send an int to all processes.

In my code, I decide which rank is root within a for loop, where different processes are responsible for different element of the loop. Then, I want to Bcast a result from root to all processes, except all non-root processes do not know who to expect the bcast from, so do not receive it.

The code block looks something like this:

for (iX=start; iX<end; iX++) //start and end are the starting row and ending row for each processes, defined earlier for (int iY=1; iY<nn; iY++) // do some calculations if (some condition) int bcastroot = rank; // initialized above int X = 111; // initialized above else //do other calculations end end MPI_Bcast(&X, 1, MPI_INT, bcastroot, comm); // remainder of code and MPI_FINALIZE 

When I execute this code, whatever the bcastroot default (value for all non-root processes) competes with root, so X is not broadcast correctly. I do not know the value of X nor can I predict the root beforehand, so I cannot define it in advance.

I have tried initializing bcastroot = -1, then setting it for rank, but this does not work. Is there a way I can Bcast this value without setting root for all processes?

Thanks, JonZor

1
  • If you do not know the root, you need to communicate it. Maybe bcast is not the right choice here? Did you have a look into MPI_Allreduce if it could solve your problem? Commented Mar 10, 2012 at 21:21

1 Answer 1

6

There is no way to do an MPI_Bcast where the receivers don't know what the root is. If you know there will only be one root, you can first do an MPI_Allreduce to agree on it:

int root, maybe_root; int i_am_root = ...; maybe_root = (i_am_root ? rank : 0); MPI_Allreduce(&maybe_root, &root, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); 

then every rank will know the same root and you can do your broadcast.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Jeremiah, much appreciated. That works great! I actually was just starting to come up with that same solution just before I read your response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.