Guest User

Untitled

a guest
Nov 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import numpy as np
  2. import random
  3. from random import seed
  4. from mpi4py import MPI
  5. comm = MPI.COMM_WORLD
  6. size = comm.Get_size()
  7. rank = comm.Get_rank()
  8.  
  9. num = 4
  10. N = num // size + (num % size > rank)
  11.  
  12. position_x = np.zeros(num,dtype=float)
  13.  
  14. seed(10059)
  15. for i in range(num):
  16.  
  17. position_x[i] = round(random.uniform(-5,5),3)
  18.  
  19.  
  20. print(position_x)
  21.  
  22. if rank == 0:
  23. share1 = position_x
  24.  
  25. else:
  26. share1 = None
  27.  
  28.  
  29. count = []
  30. for i in range(size):
  31. if i <= num%size-1:
  32. count += [num//size + 1]
  33. else:
  34. count += [num//size]
  35.  
  36. pos1 = np.zeros(N, dtype='f')
  37.  
  38. comm.Scatterv([share1, count, MPI.FLOAT], pos1, root=0)
  39.  
  40. print(pos1,'from ',rank)
  41.  
  42. [-2.275 4.238 -1.637 0.981]
  43. [5177.344] from 1
  44. [-2.275 4.238 -1.637 0.981]
  45. [ 4.172325e-08 -2.034375e+00] from 0
  46. [-2.275 4.238 -1.637 0.981]
  47. [2.264875] from 2
Add Comment
Please, Sign In to add comment