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 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.
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.
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...
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) =...
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) = 1/1! + 2/2! + . . . + n/n!. ALGORITHM S (n) //Input: A positive integer n // Procedure: fact(n) returns the factorial of the number passed as parameter //Output: The sum of the series: S(n) = 1/1! + 2/2! + . . . + n/n! if n = 1 return 1 else return S(n − 1) + n/fact(n) Set up and solve a...
Consider the relation R on N such that xRy if and only if the sum of...
Consider the relation R on N such that xRy if and only if the sum of the digits of x and y coincide. (i) Prove or disprove R is an equivalence relation. (ii) What are the equivalence classes of R.
Find the following values for a lump sum assuming annual compounding: Consider an uneven cash flow...
Find the following values for a lump sum assuming annual compounding: Consider an uneven cash flow stream: Find the following values assuming a regular, or ordinary, annuity: The present value of $400 per year for ten years at 10 percent The future value of $400 per year for ten years at 10 percent The present value of $200 per year for five years at 5 percent The future value of $200 per year for five years at 5 percent The...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first...
Prime Sum C program !! Dynamically allocated memory Let P(n) denote the sum of the first n prime numbers. For example, P(1) = 2 and P(3) = 10, since the first three prime numbers are 2, 3 and 5, respectively. Write a program to determine the value of the function P(n) for different values of n. The first few prime sums are 2, 5, 10, 17, 28, 41, 58 and 77. Input The first line of the input file contains...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
in C please! Write a code that asks user for input N and calculates the sum...
in C please! Write a code that asks user for input N and calculates the sum of the first N odd integers. There should be a "pure" method that calculates the sum separately that Main method should call when printing the output.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT