# Exercise 2 # 1. Read n and t as user input (maybe from a command line or as a data stream), where n is # the size of the square matrix, t is the number of threads to create, and n > t # 2. Create a zero n x n square matrix M. Assigned a randomized non-zero value to grid # points divisible by 10 such (0,0), (0,10), (10,0), (20,0),(10,10) ...... You can use a # function for this but the running time of this will not be considered in the time_elapsed # 3. Divide your M into t submatrices, m1 # ,m2 # ,...., mt # ; You can add an additional filter if the # matrix size n is not divisible t such as if n=10 while t =3, then the input values cannot be # processed because there is excess column or row. # 4. Take note of the system time_before ; # 5. Create t threads, to interpolate the values for each submatrix. (IMPORTANT) # 6. Take note of the system time time_after; # 7. Obtain the elapsed time_elapsed = time_after − time_before; # 8. Output the time_elapsed # 9. (Optional) You can output the resulting matrix. import random import threading import time import os import multiprocessing as mp import numpy as np # will hold the matrix with solved values final = [] #function to create the zero matrix with randomized values on points divisivble by 10 def createMatrix(n): matrix = [] for i in range(n): x = [] for j in range(n): x.append(0) matrix.append(x) for row in range(n): for col in range(n): if row % 10 == 0 and col %10 == 0 : matrix[row][col] = random.randint(0,1000) return matrix def interpolate(params): first_row = params[0] sub_size = params[1] global final # solve for left first then right then in between for cur in range(first_row, first_row + sub_size): # leftmost if final[cur][0] == 0: # fcc interpolation # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1 final[cur][0] = (final[((cur//10)*10)][0]) + ( (cur - ((cur//10)*10)) / (((cur//10)*10)+10 - ((cur//10)*10)) ) * ((final[((cur//10)*10) + 10][0]) - (final[((cur//10)*10)][0]) ) # solving for right for right in range(1, len(final)//10 + 1): if final[cur][right*10] == 0: # fcc interpolation try: # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1 final[cur][right*10] = (final[((cur//10)*10)][right* 10 ]) + ( (cur - ((cur//10)*10)) / (((cur//10)*10)+10 - ((cur//10)*10)) ) * ((final[((cur//10)*10) + 10][right* 10 ]) - (final[((cur//10)*10)][right* 10 ])) except: print() # solving for yung pagitan for center in range((right-1)*10, right*10): # fcc interpolation # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1 final[cur][center] = (final[cur][(right-1)*10]) + ( (center -((right-1)*10)) / ( (right*10) - ((right-1)*10))) * ((final[cur][right*10]) - final[cur][(right-1)*10]) return [final, first_row, sub_size] # print(tabulate(final)) def create_threads(matrix, threads_num, size): global final final = matrix #divided the matrix into submatrix sub_size = size //threads_num excess = size % threads_num #create threads threads = [] first_row = 0 divided_matrix = [] for t in range(threads_num): if t == (threads_num -1): sub_size += excess temp = [] temp.append(first_row) temp.append(sub_size) divided_matrix.append(temp) first_row += sub_size with mp.Pool(os.cpu_count()-1) as pool: for result in pool.map(interpolate, divided_matrix): for i in range(result[1], result[1] + result[2]): final[i] = result[0][i] print(final) # print(tabulate(final)) def main(): # size = 8001 # threads = [8001,2,4,8,16,32,64] # for t in threads: # for i in range(3): # print(str(size) + ", " + str(t) + " threads : ") # matrix= createMatrix(size) # start = time.time() # create_threads(matrix, t, size) # end = time.time() # elapsed = end - start # print("Time elapsed: " + str(elapsed) + " seconds") # break size = int(input("MATRIX SIZE: ")) t = int(input("THREAD NUM: ")) matrix = createMatrix(size) start = time.time() create_threads(matrix, t, size) end = time.time() elapsed = end - start print("Time elapsed: " + str(elapsed) + " seconds") if __name__ == '__main__': main()
11 2
MATRIX SIZE: THREAD NUM: [[609.0, 571.1, 533.2, 495.3, 457.4, 419.5, 381.6, 343.7, 305.8, 267.9, 230], [553.0, 525.36, 497.72, 470.08000000000004, 442.44, 414.8, 387.16, 359.52000000000004, 331.88, 304.24, 276.6], [497.0, 479.62, 462.24, 444.86, 427.48, 410.1, 392.72, 375.34000000000003, 357.96, 340.58, 323.2], [441.0, 433.88, 426.76, 419.64, 412.52, 405.4, 398.28, 391.15999999999997, 384.03999999999996, 376.91999999999996, 369.79999999999995], [385.0, 388.14, 391.28, 394.42, 397.56, 400.7, 403.84, 406.97999999999996, 410.12, 413.26, 416.4], [329.0, 342.4, 355.8, 369.2, 382.6, 396.0, 409.4, 422.8, 436.2, 449.6, 463.0], [273.0, 296.65999999999997, 320.32, 343.98, 367.64, 391.29999999999995, 414.96, 438.62, 462.28, 485.93999999999994, 509.59999999999997], [217.0, 250.92000000000002, 284.84000000000003, 318.76, 352.68000000000006, 386.6, 420.52, 454.44000000000005, 488.36000000000007, 522.28, 556.2], [161.0, 205.18, 249.36, 293.53999999999996, 337.72, 381.9, 426.08, 470.25999999999993, 514.44, 558.6199999999999, 602.8], [105.0, 159.44, 213.88000000000002, 268.32000000000005, 322.76000000000005, 377.20000000000005, 431.64000000000004, 486.08000000000004, 540.5200000000001, 594.96, 649.4000000000001], [49.0, 113.7, 178.4, 243.1, 307.8, 372.5, 437.2, 501.9, 566.6, 631.3000000000001, 696]] Time elapsed: 0.05282235145568848 seconds