Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mpi4py import MPI
- comm = MPI.COMM_WORLD
- rank = comm.Get_rank()
- size = comm.Get_size()
- # each rank gets a number
- val = rank * 10
- print ("Rank %d/%d has value %d" %(rank, size, val))
- # rank zero get the sum
- sum = comm.reduce(val, op=MPI.SUM, root=0)
- if rank==0:
- print ("Rank 0 got the sum, the total is %d" %sum)
- """outout
- $ mpirun -n 4 python reduce.py
- Rank 3/4 has value 30
- Rank 1/4 has value 10
- Rank 2/4 has value 20
- Rank 0/4 has value 0
- Rank 0 got the sum, the total is 60
- """
Add Comment
Please, Sign In to add comment