Answered step by step
Verified Expert Solution
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.COMMWORLD
rank comm.Getrank
n comm.Getsize
# Message types
IDLE
COLORED
TERMINATE
ROUND
UPDATE
ACK
DISCARD
ROVER
TERMINATE
# Spanning tree of graph
ROOT
A nparray
dtypeint
children
parents
# Process state and communication variables
state IDLE
parent parentsrank
childs setchildrenrank
neighs indicesset seti for i value in enumerateArank if value
nneighs neighs.copy
neighsreceived set
roversreceived set
roundreceived False
color
bannedcolors set
msg None # sender type, data
round False # round number, round over
# Function to find the smallest available color
def findsmallestavailablecolor:
global bannedcolors
candidatecolor
while candidatecolor in bannedcolors:
candidatecolor
return candidatecolor
# Function to check if the process has the highest degree in not colored neighbors
def highestdegreeinnotcoloredneighbors:
global nneighs
return not nneighs or rank maxnneighs
# Main loop for the algorithm
while state TERMINATE:
round False
neighsreceived.clear
roversreceived.clear
roundreceived False
# Root process handling termination or starting a new round
if rank :
if state COLORED:
# Send termination message if already colored
termmsg msgcopy
termmsg termmsg rank, TERMINATE
comm.sendobjtermmsg destROOT, tagTERMINATE
else:
# Start a new round
round
roundmsg msgcopy
roundmsg roundmsg roundmsg rank, ROUND, round
comm.sendobjroundmsg destROOT, tagROUND
# Process communication and logic during a round
while not round:
sendertype, data comm.recv
if type ROUND:
# Update round number and propagate it to children
round data
roundmsg msgcopy
roundmsg roundmsg roundmsg rank, ROUND, data
for child in childs:
comm.sendobjroundmsg destchild, tagROUND
# If already colored, send discard messages to neighbors
if state COLORED:
discardmsg msgcopy
discardmsg discardmsg rank, DISCARD
for neigh in neighs:
comm.sendobjdiscardmsg destneigh, tagDISCARD
else:
# If highest degree in not colored neighbors, color the process
if highestdegreeinnotcoloredneighbors:
color findsmallestavailablecolor
state COLORED
updatemsg msgcopy
updatemsg updatemsg updatemsg rank, UPDATE, color
for neigh in nneighs:
comm.sendobjupdatemsg destneigh, tagUPDATE
else:
# Otherwise, send discard messages to neighbors
discardmsg msgcopy
discardmsg discardmsg rank, DISCARD
for neigh in neighs:
comm.sendobjdiscardmsg destneigh, tagDISCARD
roundreceived True
# Handling various message types
elif type UPDATE:
neighsreceived.addsender
ackmsg msgcopy
ackmsg ackmsg rank, ACK
comm.sendobjackmsg destsender tagACK
if state COLORED:
nneighs.removesender
bannedcolors.adddata
elif type DISCARD:
neighsreceived.addsender
elif type ACK:
neighsreceived.addsender
elif type ROVER:
roversreceived.addsender
elif type TERMINATE:
# Terminate and propagate termination message to children
if childs:
for child in childs:
termmsg msgcopy
termmsg termmsg rank, TERMINATE
comm.sendobjtermmsg destchild, tagTERMINATE
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started