Question

In: Computer Science

Consider the following code section of a parallel program that computes the sum of n values...

Consider the following code section of a parallel program that computes the sum of n values using p processors:

my_sum = 0;

my_first_i = ... ;

my_last_i = ... ;

for(my_i = my_first_i; my_i < my_last_i; my_i++) {

my_x = Compute_next_value(...);

my_sum += my_x;

}

Here the prefix my_ indicates that each core is using its own, private variables, and
each core can execute this block of code independently of the other cores.

Devise formulas for the functions that calculate my_first_i and my_last_i in the global sum example. Remember that each core should be assigned roughly the same number of elements of computations in the loop. (Hint: First consider the case when n is evenly divisible by p.)

Solutions

Expert Solution

Please find the code in python:
import multiprocessing as mp        #Needed for multiprocessing
import numpy as np                  #Needed for Random Variables


def sumNums(sno, total):                            #Defined new sum function as generic one will-
                                                    # only accept int values, but iteration of int isn't possible
    np.random.RandomState(99)                       #Generating 16 random number sets
    arr = np.random.randint(-1, 10, size=16)
    data = arr.tolist()
    data[:15]
    print(sno + 1, "\t   ", data)
    for d in data:
        total = total + int(d)
    return total                                    #Sending the total of individual sets


def main():
    # print("Number of processors: ", mp.cpu_count())        #I have 4 processors
    n = 16                                          # For testing purpose, I have chosen 16 as 16 is divisible by 4 processors
    total = 0
    serial = [0] * n
    for i in range(n):                              #Just for keeping a count of sets printed later-on
        serial[i] = i

    pool = mp.Pool(mp.cpu_count())                  # For implementing parallelizing

    print("Sno.\t Set of numbers")
    total = pool.starmap(sumNums, [(x, total) for x in serial])
    pool.close()

    print("\n\nFinally, sum of each set above is displayed below:\n")
    print(total)


if __name__ == '__main__':                          #'If' condition is required, otherwise the              processors will keep on generating the function
    main()

Related Solutions

Write a Python program that computes certain values such as sum, product, max, min and average...
Write a Python program that computes certain values such as sum, product, max, min and average of any 5 given numbers along with the following requirements. Define a function that takes 5 numbers, calculates and returns the sum of the numbers. Define a function that takes 5 numbers, calculates and returns the product of the numbers. Define a function that takes 5 numbers, calculates and returns the average of the numbers. Must use the function you defined earlier to find...
Write a Little Man program that outputs the sum of n number of input values.
Write a Little Man program that outputs the sum of n number of input values.
Write a c program that prints the final sum of all values from 1 to n...
Write a c program that prints the final sum of all values from 1 to n only when n is a positive value. The program is to print "Poor!" when the final sum is less than 70, print "Good" when the sum is between 71 and 90. or "Great!" when the sum is 91 or better.
Create a program that asks the user to input three numbers and computes their sum. This...
Create a program that asks the user to input three numbers and computes their sum. This sounds simple, but there's a constraint. You should only use two variables and use combined statements. You can use the output below as a guide. Please enter the first number: 4 Please enter the second number: 2 Please enter the third number: 9 The sum of the three numbers is: 15.
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input.
1. Write a program that computes the smallest and largest of a set of values that...
1. Write a program that computes the smallest and largest of a set of values that are stored in an array. Ask the user for the set size (and hence the array size). Populate the array with user input. (Java language)
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) =...
Consider the following recursive algorithm for computing the sum of the first n squares: S(n) = 12 +22 +32 +...+n2 . Algorithm S(n) //Input: A positive integer n //Output: The sum of the first n squares if n = 1 return 1 else return S(n − 1) + n* n a. Set up and solve a recurrence relation for the number of times the algorithm’s basic operation is executed. b. How does this algorithm compare with the straightforward non-recursive algorithm...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT