1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
## mpiexec -n 2 python ex-2.01.py
# Process 0 sends a message to process 1
# --------------------------------------------------------------------
from mpi4py import MPI
import array
if MPI.COMM_WORLD.Get_size() < 2:
raise SystemExit
# --------------------------------------------------------------------
s = b"Hello there"
msg = array.array('B', b'\0'*20)
tag = 99
status = MPI.Status()
myrank = MPI.COMM_WORLD.Get_rank()
if myrank == 0:
msg[:len(s)] = array.array('B', s)
MPI.COMM_WORLD.Send([msg, len(s)+1, MPI.CHAR], 1, tag)
elif myrank == 1:
MPI.COMM_WORLD.Recv([msg, 20, MPI.CHAR], 0, tag, status)
# --------------------------------------------------------------------
if myrank == 1:
assert list(msg[:len(s)]) == list(s)
assert msg[len(s)] == 0
assert status.source == 0
assert status.tag == tag
assert status.error == MPI.SUCCESS
assert status.Get_count(MPI.CHAR) == len(s)+1
# --------------------------------------------------------------------
|