awips2/pythonPackages/scientific/Examples/mpi.py
root e2ecdcfe33 Initial revision of AWIPS2 11.9.0-7p5
Former-commit-id: a02aeb236c [formerly 9f19e3f712] [formerly a02aeb236c [formerly 9f19e3f712] [formerly 06a8b51d6d [formerly 64fa9254b946eae7e61bbc3f513b7c3696c4f54f]]]
Former-commit-id: 06a8b51d6d
Former-commit-id: 8e80217e59 [formerly 3360eb6c5f]
Former-commit-id: 377dcd10b9
2012-01-06 08:55:05 -06:00

61 lines
1.5 KiB
Python
Executable file

from Scientific import MPI
from Scientific import N
import sys
communicator = MPI.world.duplicate()
# Send and receive
if communicator.rank == 0:
data = 1.*N.arange(10)
communicator.send(data, 1, 0)
print "%d sent:\n%s" % (communicator.rank, str(data))
data, source, tag = communicator.receiveString(1, None)
print "%d received string from %d: %s" % (communicator.rank, source, data)
elif communicator.rank == 1:
data, source, tag, count = \
communicator.receive(N.Float, None, None)
print "%d received from %d:\n%s" \
% (communicator.rank, source, str(data))
communicator.send("Hello world", 0, 42)
else:
print "%d idle" % communicator.rank
# Barrier
print "%d waiting at barrier" % communicator.rank
sys.stdout.flush()
communicator.barrier()
print "%d passed barrier" % communicator.rank
sys.stdout.flush()
# Broadcast
data = N.zeros((5,), N.Float)
if communicator.rank == 0:
data[:] = 42.
communicator.broadcast(data, 0)
print "%d has:\n %s" % (communicator.rank, str(data))
sys.stdout.flush()
communicator.barrier()
# Reduce
data_send = (communicator.rank+1) * N.arange(3)
data_recv = N.zeros_st(3, data_send)
communicator.reduce(data_send, data_recv, MPI.sum, 0)
print "%d has:\n %s" % (communicator.rank, str(data_recv))
# Share
to_share = (communicator.rank+1)*N.arange(5)
all = N.zeros_st((communicator.size,)+to_share.shape, to_share)
communicator.share(to_share, all)
print "%d has:\n%s" % (communicator.rank, str(all))