2

Just getting to grips with parallel programming using the Java interface for MPI. Just wondering if someone can explain very simply how the broadcast works?

I have the following:

if (me ==0) { // This is the master process
   int bvalue = 4;
   MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);
}
else { // The worker processes
   MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);
}

So I know the worker process has to call the bcast to receive the bvalue.. How would I go about using this value in the workers section?

If I do:

int workerb = MPI.COMM_WORLD.Bcast(bvalue, 0, 4, MPI.INT, 0);

I get an incompatible type error, void can't be converted into int.

Any help would be greatly appreciated. Thanks, Mike

1 Answer 1

1

I believe you might be getting the parameters wrong here. The method call to Bcast() has the following method signature (taken from here):

public void Bcast(java.lang.Object buf, int offset, int count, Datatype type, int root)

The first parameter usually describes an array of something (in this case maybe an array of integers). The second parameter describes the offset into this array from which the broadcast would start. The third parameter, count, describes how many elements from the offset to send. And the final parameter describes the rank of the sender (0 is the master node).

You are getting that error because the Bcast() method call does not return anything (returns void). Also, I believe that the call to Bcast is a blocking call, so you could essentially rewrite your code above to:

int[] bvalue = new int[1];

if (me == 0){ //this is the master process 
     bvalue[0] = 4;
}

//the master node will broadcast the value '4' and
//workers will block here waiting for the broadcast 
//to complete
MPI.COMM_WORLD.Bcast(bvalue, 0, 1, MPI.INT, 0);

//output the contents of bvalue
System.out.println("bvalue is " + bvalue[0]);

This should accomplish the behavior you are expecting I believe. Hope this helps..

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

4 Comments

It's been a while since I've dusted off my Java, but I believe you need to allocate the array on all processes since the master node isn't the only one who will be using the array bvalue.
Oops! overlooked that...thanks! I updated my answer to reflect that
Thank you very much for your comment! It explains alot! I'm still a little mythed about where the Bcast goes though. Within the if (me == 0) block, I have the Bcast there and in the worker block, I have the println(bvalue) however, when I run the program, it just hangs during the master block when it reaches the Bcast. Any thoughts? Thanks again.
You have to make sure that both master and worker processes are all calling the Bcast() method otherwise you would see the program hang. For example, you could either do something similar to the example I posted above or just have another Bcast call in the worker block like you had before. Personally, having just one MPI method call makes my debugging life easier :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.