t1nman

mpi_relay

May 25th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <mpi.h>
  4. #define arr_size 20
  5.  
  6. int main (int argc, char **argv)
  7. {
  8.     char message[arr_size] = {};
  9.     int rank, size;
  10.  
  11.     strcpy(message, "Hi, process");
  12.  
  13.     MPI_Status status;
  14.     MPI_Init (&argc, &argv);
  15.     MPI_Comm_rank (MPI_COMM_WORLD, &rank);
  16.     MPI_Comm_size (MPI_COMM_WORLD, &size);
  17.  
  18.     if ( (rank+1) < size)
  19.     {
  20.         MPI_Send(message, strlen(message), MPI_CHAR, rank+1, 99, MPI_COMM_WORLD);
  21.         printf("I'm %d. Sent msg to %d\n", rank, rank+1);
  22.  
  23.         MPI_Recv(message, strlen(message), MPI_CHAR, rank-1, 99, MPI_COMM_WORLD, &status);
  24.         if (rank != 0)
  25.             printf("I'm %d. Received msg from %d\n", rank, rank-1);
  26.         else
  27.             printf("I'm %d. Received msg from %d\n", rank, size-1);
  28.        
  29.     }
  30.     else
  31.     {
  32.         MPI_Send(message, strlen(message), MPI_CHAR, 0, 99, MPI_COMM_WORLD);
  33.         printf("I'm %d. Sent msg to %d\n", rank, 0);
  34.  
  35.         MPI_Recv(message, strlen(message), MPI_CHAR, rank-1, 99, MPI_COMM_WORLD, &status);
  36.         printf("I'm %d. Received msg from %d\n", rank, rank-1);
  37.     }
  38.     MPI_Finalize();
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment