JunkPile77

Untitled

Apr 29th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. #include "mpi.h"
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. int main(int argc, char**argv) {
  43.  
  44.     int rank, size, i, buf[1];
  45.     const int strsize = 100;
  46.     char msg[strsize];
  47.     MPI_Status status;
  48.  
  49.     //initialize MPI
  50.     MPI_Init(&argc, &argv);
  51.  
  52.     //set the number of processes
  53.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  54.  
  55.     //set rank of all processes / nodes
  56.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  57.  
  58.     if(rank != 0 ){
  59.         sprintf( msg, "msg from node %d out of %d nodes\n", rank, size);
  60.         MPI_Send(msg, strlen(msg)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD );
  61.     }
  62.     else{
  63.         for(i=1; i<size;i++ ){
  64.             MPI_Recv(msg, strsize, MPI_CHAR, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE );
  65.             printf("%s\n", msg);
  66.         }
  67.     }
  68.     MPI_Finalize();
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment