Advertisement
pexea12

circle

Feb 26th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.   int rc;
  9.   rc = MPI_Init(&argc, &argv);
  10.   if (rc != MPI_SUCCESS) {
  11.     printf("MPI initialization failed\n");
  12.     exit(1);
  13.   }
  14.  
  15.   int ntasks, id;
  16.   MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
  17.   MPI_Comm_rank(MPI_COMM_WORLD, &id);
  18.  
  19.   if (ntasks < 2) {
  20.     printf("Must run with more than 2 processors\n");
  21.     exit(1);
  22.   }
  23.  
  24.   srand(1); // Seed 1
  25.  
  26.   int message[3];
  27.   message[0] = id; // sender
  28.   message[1] = id + 1; // receiver
  29.   message[2] = rand(); // random number
  30.  
  31.  
  32.   if (id < ntasks - 1) {
  33.     MPI_Send(
  34.       message,
  35.       3,
  36.       MPI_INT,
  37.       id + 1,
  38.       100, // tag
  39.       MPI_COMM_WORLD
  40.     );
  41.     printf("Sender %d -> Receiver %d with message(%d, %d, %d)", id, id + 1, message[0], message[1], message[2]);
  42.   }
  43.  
  44.   if (id > 0) {
  45.     MPI_Recv(
  46.       message,
  47.       3,
  48.       MPI_INT,
  49.       id - 1,
  50.       100, // tag
  51.       MPI_COMM_WORLD,
  52.     );
  53.  
  54.     printf("Receive %d <- Sender %d with message(%d, %d, %d)", id, id - 1, message[0], message[1], message[2]);
  55.   }
  56.  
  57.   MPI_Finalize();
  58.  
  59.   return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement