In: Computer Science
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.)
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()