Python, numpy parallel programthat creates a numpy array of size 12 randomly gen
ID: 670507 • Letter: P
Question
Python, numpy parallel programthat creates a numpy array of size 12 randomly generated values , range 0-100. The purpose of this code is to divide up the task of summing the array into equal numbers / processor so that each processor will sum their values in the array and send the sum back to the master.
I could not write the sum of their values code in the array? the function maybe : np.sum(values of the array or name of array in here). I am not sure. Can someone help to finish this?
code in numpy python:
passRandom12.py
import numpy as np
from mpi4py import MPI
from mpi4py.MPI import ANY_SOURCE
# Initializations and preliminaries
comm = MPI.COMM_WORLD
# get MPI communicator object
size = comm.Get_size()
# total number of processes
rank = comm.Get_rank()
# rank of this process
name = MPI.Get_processor_name()
status = MPI.Status()
# get MPI status object
randNum = np.zeros(1)
rnk= -1
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
return type('Enum',(),enums)
tags = enum('READY','DONE', 'EXIT','START')
if rank == 0:
# Master process executes code below
tasks = range(3*size)
task_index = 0
num_workers = size - 1
closed_workers = 0
print("Master starting with {} workers".format(num_workers))
while closed_workers < num_workers:
randNum = np.random.randint(101, size=12)
data = comm.recv(source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
source = status.Get_source()
tag = status.Get_tag()
if tag == tags.READY:
# Worker is ready, so send it a task
if task_index < len(tasks):
comm.send(tasks[task_index], dest=source, tag=tags.START)
print("Sending task {} to worker {}".format(task_index, source))
task_index += 1
else:
comm.send(None, dest=source, tag=tags.EXIT)
elif tag == tags.DONE:
results = data
print("Got data from worker {}".format(source), randNum[0])
elif tag == tags.EXIT:
print("Worker {} exited.".format(source))
closed_workers += 1
print("Master finishing, the sum of 12 random values between 0 and 100 is:", randNum[0])
else:
# Worker processes execute code below
randNum = np.random.randint(101, size=12)
name= MPI.Get_processor_name()
print("I am a worker with rank {} on {}.".format(rank, name))
while True:
comm.send(None, dest=0, tag=tags.READY)
task = comm.recv(source=0, tag=MPI.ANY_SOURCE, status=status)
tag = status.Get_tag()
if tag == tags.START:
# Do the work here
result = task**2
comm.send(result, dest=0, tag=tags.DONE)
elif tag == tags.EXIT:
break
comm.send(None, dest=0, tag=tags.EXIT)
Explanation / Answer
# Define MPI message tags tags = enum('READY', 'DONE', 'EXIT', 'START') # Initializations and preliminaries comm = MPI.COMM_WORLD # get MPI communicator object size = comm.size # total number of processes rank = comm.rank # rank of this process status = MPI.Status() # get MPI status object if rank == 0: # Master process executes code below tasks = range(2*size) task_index = 0 num_workers = size - 1 closed_workers = 0 print("Master starting with %d workers" % num_workers) while closed_workersRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.