Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. /**
  2.  
  3. @author Pecundo, Allan Magno and Enriquez, Arvin
  4. @version 15 Feb 2020
  5.  
  6. **/
  7.  
  8.  
  9.  
  10.  
  11. #include <stdio.h>
  12. #include "mpi.h"
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16. int rank, size, next, prev, message, tag = 201;
  17.  
  18. /* Start up MPI */
  19.  
  20. MPI_Init(&argc, &argv);
  21. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  22. MPI_Comm_size(MPI_COMM_WORLD, &size);
  23.  
  24. /* Calculate the rank of the next process in the ring. Use the
  25. modulus operator so that the last process "wraps around" to
  26. rank zero. */
  27.  
  28. next = (rank + 1) % size;
  29. prev = (rank + size - 1) % size;
  30.  
  31. /* If we are the "master" process (i.e., MPI_COMM_WORLD rank 0),
  32. put the number of times to go around the ring in the
  33. message. */
  34.  
  35. if (0 == rank) {
  36. message = 10;
  37.  
  38. printf("Process 0 sending %d to %d, tag %d (%d processes in ring)\n",
  39. message, next, tag, size);
  40. MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
  41. printf("Process 0 sent to %d\n", next);
  42. }
  43.  
  44. /* Pass the message around the ring. The exit mechanism works as
  45. follows: the message (a positive integer) is passed around the
  46. ring. Each time it passes rank 0, it is decremented. When
  47. each processes receives a message containing a 0 value, it
  48. passes the message on to the next process and then quits. By
  49. passing the 0 message first, every process gets the 0 message
  50. and can quit normally.
  51.  
  52. CHALLENGE: Edit code to replace decrement at each node. */
  53.  
  54. while (1) {
  55. MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,
  56. MPI_STATUS_IGNORE);
  57.  
  58. /* Code was edited to allow decrement at each node by removing if condition where decrement only executes are rank = 0.
  59.  
  60. Addition code was added to show decrement occurring at each node
  61. */
  62.  
  63. message--;
  64. printf("Process %d decremented value: %d\n", rank, message);
  65.  
  66. MPI_Send(&message, 1, MPI_INT, next, tag, MPI_COMM_WORLD);
  67. if (0 == message) {
  68. printf("Process %d exiting\n", rank);
  69. break;
  70. }
  71. }
  72.  
  73. /* The last process does one extra send to process 0, which needs
  74. to be received before the program can exit */
  75.  
  76.  
  77. MPI_Recv(&message, 1, MPI_INT, prev, tag, MPI_COMM_WORLD,
  78. MPI_STATUS_IGNORE);
  79.  
  80.  
  81.  
  82. /* All done */
  83.  
  84. MPI_Finalize();
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement