MPI stands for Message Passing Interface. It is essentially a library of routines. There are bindings / implementations in Python, Fortran, C , C++. I am using Open MPI for my examples. Blog for the installation of OpenMPI is here

MPI allows processes to communicate to exchange messages. Typically the processes are members of communications. In C++ the communicator shared by call is MPI::COMM_WORLD.

Each process has unique id within the communicator. Typically there is master process and a number of slave processes.

Example 1: Sending a Random Number from Master Process to the slave process
Code Snippet 1.1

 
 int main(int argc, char **argv)
{

	// Initialize MPI and determine our process number
	MPI::Init(argc, argv);
	const int iMyProc = MPI::COMM_WORLD.Get_rank();
	
	
	if (iMyProc == 0){
		master();
	}else{
		worker(iMyProc);
	}

	MPI::Finalize();

	return 0;
}

MPI::COMM_WORLD.Get_rank() gives the rank of the current process. The rank of the master process generally is set as ‘0’ and the workers have process rank >0.

1.2 Code Snippet for the master process

 void master() {
   //printf("Hello from Master\n");
   const int nProcs = MPI::COMM_WORLD.Get_size();
   
   /* initialize random seed: */
   srand ( time(NULL) );

  /* generate secret number: */
  


   // send a random number to each worker
    for(int i=1; i<nProcs; i++){
        unsigned r = rand();
	printf("Hello from master to worker number %d : sending random r %d\n", i, r);	
        MPI::COMM_WORLD.Send(&r, sizeof(unsigned), MPI::BYTE, i, msgID);
    }
}

The master sends a random number to its slave processes. Every MPI:COMM_WORLD.Send() on the master side is matched with MPI:COMM_WORLD.Recv() on the slave end. The message Id (msgId) uniquely identify a message being transmitted.

1.3 Code Snippet for worker process

void worker(int iMyProc) {
   unsigned r;
   MPI::COMM_WORLD.Recv(&r, sizeof(unsigned), MPI::BYTE, iMasterProc, msgID);
   printf("Hello from worker %d : received random r %d\n", iMyProc, r);
	
}

The worker code is reciprocal of the master code. MPI::COMM_WORLD.Recv on the worker side receives the message from the master.

Hope this helps folks Getting Started with MPI.

Advertisements