Token Ring-Like Program in MPI

Home / Token Ring-Like Program in MPI

Token Ring-Like Program in MPI

December 9, 2015 | Uncategorized | No Comments

In this article, we will discuss sample send and receive mechanism in MPI inclusively we will imitate the concept of token ring topology. In this article we use ten processes with rank from 0 to 9. Starting from 0, the nth process will pass token to (n+1) th process, except 9 which will send to 0. When the token comes back to rank 0, the program will be terminated.

This sample code is using logical topology implemented on program.

In this article I assume you have installed MPICH, OpenMPI, or other MPI implementation.

Source Code

Create a file mpi_token_rin.c and write this:

#include <mpi.h>
#include <stdio.h>

int main(int argc, char* argv[]) {
   // Initialize the MPI Environment
   MPI_Init(&argc, &argv);

   // Get the number of process
   int size;
   MPI_Comm_size( MPI_COMM_WORLD, &size );

   // Get the rank of process
   int rank;
   MPI_Comm_rank( MPI_COMM_WORLD, &rank );

   // The token
   int token;

   if( rank != 0 ) {
      MPI_Recv( &token, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );

      printf("Process %d received token %d from process %d\n", rank, token, rank-1);
   } else {
      // Initialize the token value
      token = 100;
   }
   MPI_Send( &token, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD );

   if( rank == 0 ) {
      MPI_Recv( &token, 1, MPI_INT, size - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );

      printf("Process 0 received token %d from process %d. End of communication\n", token, size-1 );
   }

   // Finalize the MPI environment
   MPI_Finalize();

   return 0;
}

Compile & Run

To compile:

mpicc mpi_token_ring.c -o mpi_token_ring

To Run:

mpirun -n 10 mpi_token_ring

Result

Process 1 received token 100 from process 0
Process 2 received token 100 from process 1
Process 3 received token 100 from process 2
Process 4 received token 100 from process 3
Process 5 received token 100 from process 4
Process 6 received token 100 from process 5
Process 7 received token 100 from process 6
Process 8 received token 100 from process 7
Process 9 received token 100 from process 8
Process 0 received token 100 from process 9. End of communication

Explanation

Now let’s discuss all the component we have written.

Process zero will assig token with value 100. The value is passed around every single process. Each process except zero is stand by and waiting for token passing. The system will terminate when process zero has received the token back.

Now, let see what if we modify the code like this:

#include <mpi.h>
#include <stdio.h>

#define LIMIT 50

int main(int argc, char** argv) {
   int size, rank;

   MPI_Init( &argc, &argv );
   MPI_Comm_size( MPI_COMM_WORLD, &size );
   MPI_Comm_rank( MPI_COMM_WORLD, &rank );

   int token = 0;

   do {
      if( rank != 0  && token < LIMIT ) {
         MPI_Recv( &token, 1, MPI_INT, rank-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
         printf("Process %d received token %d from process %d\n", rank, token, rank-1);
         token++;
      } else {
         token++;
      }

      MPI_Send( &token, 1, MPI_INT, (rank+1) % size, 0, MPI_COMM_WORLD);
      token += size;

      if( rank == 0 && token < LIMIT ) {
         MPI_Recv( &token, 1, MPI_INT, size-1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
      }
   } while( token < LIMIT );

   MPI_Finalize();

   return 0;
}

What is the behavior? 😉

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial