Advertisement
Sawy3R11

MPI_LAB_1_scanf

Apr 17th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. // Author: Wes Kendall
  2. // Copyright 2011 www.mpitutorial.com
  3. // This code is provided freely with the tutorials on mpitutorial.com. Feel
  4. // free to modify it for your own use. Any distribution of the code must
  5. // either provide a link to www.mpitutorial.com or keep this header intact.
  6. //
  7. // MPI_Send, MPI_Recv example. Communicates the number -1 from process 0
  8. // to process 1.
  9. //
  10. #include <mpi.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define length 100
  15.  
  16. int main(int argc, char** argv) {
  17.   // Initialize the MPI environment
  18.   MPI_Init(NULL, NULL);
  19.   // Find out rank, size
  20.   int world_rank;
  21.   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  22.   int world_size;
  23.   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  24.  
  25.   // We are assuming at least 2 processes for this task
  26.   if (world_size < 2) {
  27.     fprintf(stderr, "World size must be greater than 1 for %s\n", argv[0]);
  28.     MPI_Abort(MPI_COMM_WORLD, 1);
  29.   }
  30.  
  31.   if (world_rank == 0) {
  32.     printf("give message");
  33.     char data0[length];
  34.     scanf("%s", data0);
  35.     MPI_Send(
  36.       /* data         = */ data0,
  37.       /* count        = */ length,
  38.       /* datatype     = */ MPI_CHAR,
  39.       /* destination  = */ 1,
  40.       /* tag          = */ 0,
  41.       /* communicator = */ MPI_COMM_WORLD);
  42.  
  43.   } else if( world_rank==1 ) {
  44.     char data[length];
  45.    
  46.         MPI_Recv(
  47.       /* data         = */ data,
  48.       /* count        = */ length,
  49.       /* datatype     = */ MPI_CHAR,
  50.       /* source       = */ MPI_ANY_SOURCE,
  51.       /* tag          = */ MPI_ANY_TAG,
  52.       /* communicator = */ MPI_COMM_WORLD,
  53.       /* status       = */ MPI_STATUS_IGNORE);
  54.  
  55.         printf("message: %s", data);
  56.  
  57.   }
  58.   MPI_Finalize();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement