fork download
  1. # Exercise 2
  2.  
  3.  
  4. # 1. Read n and t as user input (maybe from a command line or as a data stream), where n is
  5. # the size of the square matrix, t is the number of threads to create, and n > t
  6. # 2. Create a zero n x n square matrix M. Assigned a randomized non-zero value to grid
  7. # points divisible by 10 such (0,0), (0,10), (10,0), (20,0),(10,10) ...... You can use a
  8. # function for this but the running time of this will not be considered in the time_elapsed
  9. # 3. Divide your M into t submatrices, m1
  10. # ,m2
  11. # ,...., mt
  12. # ; You can add an additional filter if the
  13. # matrix size n is not divisible t such as if n=10 while t =3, then the input values cannot be
  14. # processed because there is excess column or row.
  15. # 4. Take note of the system time_before ;
  16. # 5. Create t threads, to interpolate the values for each submatrix. (IMPORTANT)
  17. # 6. Take note of the system time time_after;
  18. # 7. Obtain the elapsed time_elapsed = time_after − time_before;
  19. # 8. Output the time_elapsed
  20. # 9. (Optional) You can output the resulting matrix.
  21.  
  22. import random
  23. import threading
  24. import time
  25. import os
  26. import multiprocessing as mp
  27. import numpy as np
  28.  
  29. # will hold the matrix with solved values
  30. final = []
  31.  
  32.  
  33. #function to create the zero matrix with randomized values on points divisivble by 10
  34. def createMatrix(n):
  35. matrix = []
  36. for i in range(n):
  37. x = []
  38. for j in range(n):
  39. x.append(0)
  40. matrix.append(x)
  41. for row in range(n):
  42. for col in range(n):
  43. if row % 10 == 0 and col %10 == 0 :
  44. matrix[row][col] = random.randint(0,1000)
  45.  
  46. return matrix
  47.  
  48. def interpolate(params):
  49. first_row = params[0]
  50. sub_size = params[1]
  51. global final
  52.  
  53. # solve for left first then right then in between
  54. for cur in range(first_row, first_row + sub_size):
  55. # leftmost
  56. if final[cur][0] == 0:
  57. # fcc interpolation
  58. # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1
  59. 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]) )
  60.  
  61. # solving for right
  62. for right in range(1, len(final)//10 + 1):
  63. if final[cur][right*10] == 0:
  64. # fcc interpolation
  65. try:
  66. # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1
  67. 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 ]))
  68. except:
  69. print()
  70. # solving for yung pagitan
  71. for center in range((right-1)*10, right*10):
  72. # fcc interpolation
  73. # the nearest top non zero value (y1) ( x - x1 ) / ( x2 - x1 ) * y2 - y1
  74. 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])
  75.  
  76. return [final, first_row, sub_size]
  77. # print(tabulate(final))
  78.  
  79.  
  80.  
  81.  
  82. def create_threads(matrix, threads_num, size):
  83. global final
  84. final = matrix
  85. #divided the matrix into submatrix
  86. sub_size = size //threads_num
  87. excess = size % threads_num
  88. #create threads
  89. threads = []
  90. first_row = 0
  91.  
  92.  
  93. divided_matrix = []
  94. for t in range(threads_num):
  95. if t == (threads_num -1):
  96. sub_size += excess
  97. temp = []
  98. temp.append(first_row)
  99. temp.append(sub_size)
  100. divided_matrix.append(temp)
  101. first_row += sub_size
  102.  
  103. with mp.Pool(os.cpu_count()-1) as pool:
  104. for result in pool.map(interpolate, divided_matrix):
  105. for i in range(result[1], result[1] + result[2]):
  106. final[i] = result[0][i]
  107. print(final)
  108. # print(tabulate(final))
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115. def main():
  116. # size = 8001
  117. # threads = [8001,2,4,8,16,32,64]
  118. # for t in threads:
  119. # for i in range(3):
  120. # print(str(size) + ", " + str(t) + " threads : ")
  121. # matrix= createMatrix(size)
  122. # start = time.time()
  123. # create_threads(matrix, t, size)
  124. # end = time.time()
  125.  
  126. # elapsed = end - start
  127. # print("Time elapsed: " + str(elapsed) + " seconds")
  128. # break
  129. size = int(input("MATRIX SIZE: "))
  130. t = int(input("THREAD NUM: "))
  131. matrix = createMatrix(size)
  132. start = time.time()
  133. create_threads(matrix, t, size)
  134. end = time.time()
  135. elapsed = end - start
  136. print("Time elapsed: " + str(elapsed) + " seconds")
  137.  
  138.  
  139. if __name__ == '__main__':
  140. main()
Success #stdin #stdout 0.23s 30000KB
stdin
11
2
stdout
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