t1nman

mpi_pingpong

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