package be.ac.vub.ir.mpi; import java.util.Random; import mpi.MPI; public class MPITest { //======================== PROGRAM ===========================// public static void main(String[] args) throws InterruptedException { // ** Start up ** MPI.Init(args); final int MY_TAG=1; if (MPI.COMM_WORLD.Size() != 2 ){ System.err.println("Only use this program by running 2 processes and not "+MPI.COMM_WORLD.Size()); MPI.Finalize(); // Don't forget!! return; } int myRank = MPI.COMM_WORLD.Rank(); // ** Master process ** if (myRank == 0){ int random = new Random().nextInt(); System.out.println("Process "+myRank+" sends random number "+random+" to my brother process."); MPI.COMM_WORLD.Send(random, 0, 1, MPI.INT, 1, MY_TAG); } // ** Slave process ** else { int buffer[] = new int[1]; MPI.COMM_WORLD.Recv(buffer, 0, 1, MPI.CHAR, 0, MY_TAG); System.out.println("Process "+myRank+" received random number: "+buffer[0]); } // ** Finalize ** MPI.Finalize(); // Don't forget!! } }