// SimpleMPI.java
 

package dogma.examples.mpij;
 

import mpij.*;
 

public class SimpleMPI extends MPIApplication {
    public void MPIMain(String[] args) {
        try {
            int  totalProcesses;
            int  myRank;
            int[] msgBuf;
            Status status;

            msgBuf = new int[1];
            status = new Status();

            MPI.init();

            totalProcesses = MPI.COMM_WORLD.size();
            myRank = MPI.COMM_WORLD.rank();
            out.println("Total processes: "+ totalProcesses);
            out.println("My rank: "+ myRank);

            msgBuf[0] = myRank;
            if (myRank == 0) {
                for(int i = 1; i < totalProcesses; i++) {
                    // Note that this receive could also
                    // specify a start offset in the buffer
                    // as well as a count.
                    // See the send below.
                    MPI.COMM_WORLD.recv(msgBuf, MPI.INT,
                        MPI.ANY_SOURCE, MPI.ANY_TAG, status);

                    // Note that out is preferred over System.out
                    out.println("received from source: "+ msgBuf[0]);
                }
            } else {
                // Note that this send could have omitted
                // the start offset and the count.
                // See the receive above.
                MPI.COMM_WORLD.send(msgBuf, 0, 1, MPI.INT, 0, myRank);
                out.println("sent to: "+ 0);
            }

            MPI.finalize();
        } catch (MPIException me) {
            me.printStackTrace();
        }
    }
}