Advertisement
Guest User

Untitled

a guest
May 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. from mpi4py import MPI
  2. import time
  3. import argparse
  4.  
  5. class PhilosophersN:
  6.  
  7. def __init__(self):
  8.  
  9. self.local_counter = 0
  10. comm = MPI.COMM_WORLD
  11. rank = comm.Get_rank()
  12. size = comm.Get_size()
  13.  
  14.  
  15. if rank == 0:
  16. if (self.local_counter == 0):
  17.  
  18. print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
  19. time.sleep(0.4)
  20. L = False
  21. send0_L = comm.send("vilica", dest = (size - 1)) #L
  22. R = False
  23. send0_R = comm.send("vilica", dest = (rank+1)) #D
  24.  
  25. else:
  26. req_N = comm.recv(source = 1)
  27. if(req_N == "vilica"):
  28. L = True
  29. #self.local_counter += 1
  30.  
  31. req_1 = comm.recv(source = (size-1))
  32. if(req_1 == "vilica"):
  33. R = True
  34.  
  35. self.local_counter += 1 # povecavam br ulaza u rank = 0
  36.  
  37. if (L and R):
  38. print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
  39. time.sleep(0.4)
  40. L = False
  41. send0_L = comm.send("vilica", dest = (size - 1)) #L
  42. R = False
  43. send0_R = comm.send("vilica", dest = (rank+1)) #D
  44.  
  45. elif rank == (size - 1):
  46. if(self.local_counter == 1):
  47. L = False
  48. R = False
  49. if not L:
  50. req0 = comm.recv(source = 0)
  51. if(req0 == "vilica"):
  52. L = True
  53. if not R:
  54. req_N_1 = comm.recv(source = (rank - 1))
  55. if(req_N_1 == "vilica"):
  56. R = True
  57.  
  58. if(L and R):
  59. print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
  60.  
  61. time.sleep(0.4)
  62. L = False
  63. send0_L = comm.send("vilica", dest = (size - 2)) #L
  64. R = False
  65. send0_R = comm.send("vilica", dest = 0) #D
  66.  
  67. else:
  68. if(self.local_counter == 1):
  69. R = True
  70. L = False
  71.  
  72. req_x = comm.recv("vilica", dest = rank-1 )
  73. if(req_x == "vilica"):
  74. L = True
  75.  
  76. if(L and R):
  77. print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
  78. time.sleep(0.4)
  79. L = False
  80. send_x = comm.send("vilica", dest = (rank - 1)) #L
  81. R = False
  82. send_y= comm.send("vilica", dest = (rank + 1)) #D
  83. else:
  84. if not L:
  85. rec_1 = comm.recv(source = (rank-1))
  86. L = True
  87.  
  88. if not R:
  89. rec_1plus = comm.recv(source = (rank + 1))
  90. r = True
  91.  
  92. else:
  93.  
  94. if not L:
  95. rec_1 = comm.recv(source = (rank-1))
  96. L = True
  97.  
  98. if not R:
  99. rec_1plus = comm.recv(source = (rank + 1))
  100. r = True
  101.  
  102. if(L and R):
  103. print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
  104. time.sleep(0.4)
  105. L = False
  106. send_x = comm.send("vilica", dest = (rank - 1)) #L
  107. R = False
  108. send_y= comm.send("vilica", dest = (rank + 1)) #D
  109.  
  110.  
  111.  
  112. if __name__== "__main__":
  113. obj = PhilosophersN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement