Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help with this code? I am not able to complete each sum of the array

ID: 670886 • Letter: C

Question

Can someone help with this code? I am not able to complete each sum of the array properly in Parallel programming

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(12)
#**Why do you need the above line?? you create tasks below

     task_index = 0
     num_workers = size - 1
     closed_workers = 0

     randNum = np.random.randint(101, size=12)
     a = np.array(randNum)
     tasks = np.zeros(4) #create an empty array called tasks
   # tasks=[a[x:x+4] for x in xrange(0,len(a),4)] do this down in READY Tag so you can control it
   # b=np.sum(tasks)
#don't sum yet but you can use this as a test (above line)

     print("Array generated randomly: ",a)


     print("Master starting with {} workers".format(num_workers))
     while closed_workers < num_workers:
         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(a): #this should not be tasks it should be the random array a
                #**right here (below) is the line that sends the array sections to the individual
                #workers - you are sending just one value tasks[0] or tasks[1] right now
                #so - you want to use the splice here so you can control it:
                 tasks = a[task_index:task_index + 4]
                #the above line splices the a array into four smaller arrays and distributes to the workers
                #based on the READY tag so now send just tasks to the worker:
                # comm.send( tasks[task_index], dest=source, tag=tags.START)
                 comm.send (tasks, dest=source, tag=tags.START)
                 print("Sending task {} to worker {}".format(task_index, source),tasks)

                 task_index += 4 #*** and then count by 4 not one
             else:
                 comm.send(None, dest=source, tag=tags.EXIT)
         elif tag == tags.DONE:
             #once you get the data -- sum all the worker sums in this section.
             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

     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
             randNum[0:4:3]
             #Now ... you have to get the workers to do their work
             #and make sure they are sending back the sum of the 4 element
             #array you sent here:
             result = task**3
             comm.send(result, dest=0, tag=tags.DONE)
         elif tag == tags.EXIT:
             break

     comm.send(None, dest=0, tag=tags.EXIT)

Explanation / Answer

Basic of C programming :

#include <stdio.h>
#include<conio.h>
#include <malloc.h>

void main()

{
    int i, n, sum = 0;

    int *a;

    printf("Enter the size of array A ");

    scanf("%d", &n);

    a = (int *) malloc(n * sizeof(int));

    printf("Enter Elements of First List ");

    for (i = 0; i < n; i++)

    {
        scanf("%d", a + i);
    }
    for (i = 0; i < n; i++)

    {

        sum = sum + *(a + i);

    }

    printf("Sum of all elements in array = %d ", sum);

}

Parellel programming :

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote