Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from mppy import MPI import numpy as np # MPI setup comm = MPI.COMM _ WORLD rank = comm.Get _ rank ( ) n =

from mppy import MPI
import numpy as np
# MPI setup
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
n = comm.Get_size()
# Message types
IDLE =0
COLORED =1
TERMINATE =2
ROUND =0
UPDATE =1
ACK =2
DISCARD =3
ROVER =4
TERMINATE =5
# Spanning tree of graph
ROOT =0
A = np.array([[0,0,0,1,1,0,0,1],
[0,0,0,0,0,1,0,1],
[0,0,0,1,1,0,1,0],
[1,0,1,0,1,0,1,0],
[1,0,1,1,0,1,0,1],
[0,1,0,0,1,0,0,1],
[0,0,1,1,0,0,0,0],
[1,1,0,0,1,1,0,0]], dtype=int)
children =[[3],[],[4],[6],[7],[1],[2],[5]]
parents =[0,5,6,0,2,7,3,4]
# Process state and communication variables
state = IDLE
parent = parents[rank]
childs = set(children[rank])
neighs = indices_set = set(i for i, value in enumerate(A[rank]) if value ==1)
n_neighs = neighs.copy()
neighs_received = set()
rovers_received = set()
round_received = False
color =-1
banned_colors = set()
msg =[-1,-1, None] # sender, type, data
round =[-1, False] # round number, round over
# Function to find the smallest available color
def find_smallest_available_color():
global banned_colors
candidate_color =0
while candidate_color in banned_colors:
candidate_color +=1
return candidate_color
# Function to check if the process has the highest degree in not colored neighbors
def highest_degree_in_not_colored_neighbors():
global n_neighs
return not n_neighs or rank > max(n_neighs)
# Main loop for the algorithm
while state != TERMINATE:
round[1]= False
neighs_received.clear()
rovers_received.clear()
round_received = False
# Root process handling termination or starting a new round
if rank ==0:
if state == COLORED:
# Send termination message if already colored
term_msg = msg.copy()
term_msg[0], term_msg[1]= rank, TERMINATE
comm.send(obj=term_msg, dest=ROOT, tag=TERMINATE)
else:
# Start a new round
round[0]+=1
round_msg = msg.copy()
round_msg[0], round_msg[1], round_msg[2]= rank, ROUND, round[0]
comm.send(obj=round_msg, dest=ROOT, tag=ROUND)
# Process communication and logic during a round
while not round[1]:
sender,_type, data = comm.recv()
if _type == ROUND:
# Update round number and propagate it to children
round[0]= data
round_msg = msg.copy()
round_msg[0], round_msg[1], round_msg[2]= rank, ROUND, data
for child in childs:
comm.send(obj=round_msg, dest=child, tag=ROUND)
# If already colored, send discard messages to neighbors
if state == COLORED:
discard_msg = msg.copy()
discard_msg[0], discard_msg[1]= rank, DISCARD
for neigh in neighs:
comm.send(obj=discard_msg, dest=neigh, tag=DISCARD)
else:
# If highest degree in not colored neighbors, color the process
if highest_degree_in_not_colored_neighbors():
color = find_smallest_available_color()
state = COLORED
update_msg = msg.copy()
update_msg[0], update_msg[1], update_msg[2]= rank, UPDATE, color
for neigh in n_neighs:
comm.send(obj=update_msg, dest=neigh, tag=UPDATE)
else:
# Otherwise, send discard messages to neighbors
discard_msg = msg.copy()
discard_msg[0], discard_msg[1]= rank, DISCARD
for neigh in neighs:
comm.send(obj=discard_msg, dest=neigh, tag=DISCARD)
round_received = True
# Handling various message types
elif _type == UPDATE:
neighs_received.add(sender)
ack_msg = msg.copy()
ack_msg[0], ack_msg[1]= rank, ACK
comm.send(obj=ack_msg, dest=sender, tag=ACK)
if state != COLORED:
n_neighs.remove(sender)
banned_colors.add(data)
elif _type == DISCARD:
neighs_received.add(sender)
elif _type == ACK:
neighs_received.add(sender)
elif _type == ROVER:
rovers_received.add(sender)
elif _type == TERMINATE:
# Terminate and propagate termination message to children
if childs:
for child in childs:
term_msg = msg.copy()
term_msg[0], term_msg[1]= rank, TERMINATE
comm.send(obj=term_msg, dest=child, tag=TERMINATE)
state = TERMINATE
break
# Check conditions for round completion
This is the code and i need description of high level design with FSM diagrams and FSM table.Either give the pseudocode of actions or the pseudocode of the whole algorithm. It should be something like i added below.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions