Guest User

Untitled

a guest
Oct 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. from mpi4py import MPI
  2.  
  3. comm = MPI.COMM_WORLD
  4. rank = comm.Get_rank()
  5. size = comm.Get_size()
  6.  
  7. # each rank gets a number
  8. val = rank * 10
  9. print ("Rank %d/%d has value %d" %(rank, size, val))
  10.  
  11. # rank zero get the sum
  12. sum = comm.reduce(val, op=MPI.SUM, root=0)
  13. if rank==0:
  14. print ("Rank 0 got the sum, the total is %d" %sum)
  15.  
  16.  
  17. """outout
  18. $ mpirun -n 4 python reduce.py
  19. Rank 3/4 has value 30
  20. Rank 1/4 has value 10
  21. Rank 2/4 has value 20
  22. Rank 0/4 has value 0
  23. Rank 0 got the sum, the total is 60
  24. """
Add Comment
Please, Sign In to add comment