Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. from mpi4py import MPI
  2. import numpy as np
  3. from time import time
  4.  
  5. comm = MPI.COMM_WORLD
  6. rank = comm.Get_rank()
  7. size = comm.Get_size()
  8.  
  9. Start = time()
  10.  
  11. '''
  12. Approximation of the function f(x) using the extended trapezoid method, distributed on more processes.
  13. It is required to send the parameter of intervals to number of intervals + 1 because we require the rank 0 process
  14. to distribute the data and intervals to other processes using point to point blocking communication.
  15.  
  16.  
  17. Parameters
  18. ----------
  19. f : function
  20. Vectorized function of a single variable
  21. a , b : numbers
  22. Interval of integration [a,b]
  23. N : integer
  24. Number of sub intervals of [a,b]
  25. x : Array
  26. Return evenly spaced numbers over a specified interval.
  27. numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
  28. N+1 points make N sub intervals
  29. arrayX : Array
  30. Converts array x into a dtype=np.float64.
  31. dx : float
  32. Lambda, calculates the required length of each array.
  33. k : Array
  34. Used to temporary store received date from other processes.
  35. T : Array
  36. Approximation of the function using sums of each result from the other processes.
  37. y : Array
  38. Inputs f into array y.
  39. a : Array
  40. Temporary array used to store intervals and their data.
  41. y_left : Array
  42. Left endpoints.
  43. y_right: Array
  44. Right endpoints.
  45. G : float
  46. Function used to calculate the approximation using the trapezoid method.
  47. r : Dictionary
  48. set of key(i): value pairs(receive massage)
  49. s : Dictionary
  50. set of key(i): value pairs(sent massage)
  51. r1 : Dictionary
  52. set of key(rank): value pairs(receive massage)
  53. s1 : Dictionary
  54. set of key(rank): value pairs(sent massage)
  55. Start: float
  56. at the start of program function time() returns the time in seconds since the epoch as a floating point number.
  57. End: float
  58. at the end of program function time() returns the time in seconds since the epoch as a floating point number.
  59.  
  60. Returns
  61. -------
  62. Array
  63. Approximation of the function of f(x) from a to b using the
  64. trapezoid rule with N sub intervals of equal length.
  65. '''
  66. a = 0
  67. b = 6
  68. N = size - 1
  69. f = np.square
  70. x = np.linspace(a, b, N + 1)
  71. arrayX = np.array(x, dtype=np.float64)
  72. dx = (b - a) / N
  73.  
  74. r = {}
  75. s = {}
  76.  
  77. r1 = {}
  78. s1 = {}
  79.  
  80. #ts = {}
  81. #tr = {}
  82.  
  83. if rank == 0:
  84. print(f(arrayX))
  85. k = np.zeros(1, dtype=np.float64)
  86. T = np.zeros(1, dtype=np.float64)
  87. for i in range(0, size - 1):
  88. y = f(arrayX[i:i + 2])
  89. print(f(arrayX[i:i + 2]))
  90. s[i] = comm.Isend(y, dest=i + 1)
  91. #s[i].Wait()
  92. for i in range(0, size -1):
  93. r[i] = comm.irecv(k, source=i + 1)
  94. r[i].Wait()
  95. T = T + k
  96.  
  97. End = time()
  98. print("Result: ", T[0], "Time: ", End - Start)
  99. #print("Timing starts at: ", Start, "and ends at: ", End))
  100.  
  101. elif rank != 0:
  102. #ts[rank] = time()
  103. a = np.zeros(2, dtype=np.float64)
  104. r[rank] = comm.irecv(a, source=0)
  105. r[rank].Wait()
  106. y_right = a[0:1] # right endpoint
  107. y_left = a[1:2] # left endpoint
  108. G = (dx / 2) * np.sum(y_right + y_left)
  109. s[rank] = comm.Isend(G, dest=0)
  110. #s[rank].Wait()
  111. #tr[rank] = time()
  112. #print("Rank ", rank, " Start: ", ts[rank], " End: ", tr[rank], " Time: ", tr[rank]-ts[rank])
  113. else:
  114. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement