MikeS
MikeS

Reputation: 247

Java MPI broadcast

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

Upvotes: 2

Views: 1467

Answers (1)

Eric
Eric

Reputation: 61

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..

Upvotes: 1

Related Questions