Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mpi4py import MPI
- import time
- import argparse
- class PhilosophersN:
- def __init__(self):
- self.local_counter = 0
- comm = MPI.COMM_WORLD
- rank = comm.Get_rank()
- size = comm.Get_size()
- if rank == 0:
- if (self.local_counter == 0):
- print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
- time.sleep(0.4)
- L = False
- send0_L = comm.send("vilica", dest = (size - 1)) #L
- R = False
- send0_R = comm.send("vilica", dest = (rank+1)) #D
- else:
- req_N = comm.recv(source = 1)
- if(req_N == "vilica"):
- L = True
- #self.local_counter += 1
- req_1 = comm.recv(source = (size-1))
- if(req_1 == "vilica"):
- R = True
- self.local_counter += 1 # povecavam br ulaza u rank = 0
- if (L and R):
- print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
- time.sleep(0.4)
- L = False
- send0_L = comm.send("vilica", dest = (size - 1)) #L
- R = False
- send0_R = comm.send("vilica", dest = (rank+1)) #D
- elif rank == (size - 1):
- if(self.local_counter == 1):
- L = False
- R = False
- if not L:
- req0 = comm.recv(source = 0)
- if(req0 == "vilica"):
- L = True
- if not R:
- req_N_1 = comm.recv(source = (rank - 1))
- if(req_N_1 == "vilica"):
- R = True
- if(L and R):
- print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
- time.sleep(0.4)
- L = False
- send0_L = comm.send("vilica", dest = (size - 2)) #L
- R = False
- send0_R = comm.send("vilica", dest = 0) #D
- else:
- if(self.local_counter == 1):
- R = True
- L = False
- req_x = comm.recv("vilica", dest = rank-1 )
- if(req_x == "vilica"):
- L = True
- if(L and R):
- print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
- time.sleep(0.4)
- L = False
- send_x = comm.send("vilica", dest = (rank - 1)) #L
- R = False
- send_y= comm.send("vilica", dest = (rank + 1)) #D
- else:
- if not L:
- rec_1 = comm.recv(source = (rank-1))
- L = True
- if not R:
- rec_1plus = comm.recv(source = (rank + 1))
- r = True
- else:
- if not L:
- rec_1 = comm.recv(source = (rank-1))
- L = True
- if not R:
- rec_1plus = comm.recv(source = (rank + 1))
- r = True
- if(L and R):
- print("Jedem jer imama 2 vilice --- moj Rank = " + str(rank))
- time.sleep(0.4)
- L = False
- send_x = comm.send("vilica", dest = (rank - 1)) #L
- R = False
- send_y= comm.send("vilica", dest = (rank + 1)) #D
- if __name__== "__main__":
- obj = PhilosophersN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement