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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
import mpiunittest as unittest
from mpi4py import MPI
_basic = [
None,
True,
False,
-7,
0,
7,
-(2**63) + 1,
2**63 - 1,
-2.17,
0.0,
+1.62,
1 + 2j,
2 - 3j,
"mpi4py",
]
messages = list(_basic)
messages += [
list(_basic),
tuple(_basic),
{f"k{k}": v for k, v in enumerate(_basic)},
]
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, "mpi-message")
class TestMessage(unittest.TestCase):
#
def testMessageNull(self):
null = MPI.MESSAGE_NULL
self.assertFalse(null)
null2 = MPI.Message()
self.assertEqual(null, null2)
null3 = MPI.Message(null)
self.assertEqual(null, null3)
def testMessageNoProc(self):
#
noproc = MPI.MESSAGE_NO_PROC
self.assertTrue(noproc)
noproc.recv()
self.assertTrue(noproc)
noproc.irecv().wait()
self.assertTrue(noproc)
#
noproc2 = MPI.Message(MPI.MESSAGE_NO_PROC)
self.assertTrue(noproc2)
self.assertEqual(noproc2, noproc)
self.assertNotEqual(noproc, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
message.recv()
self.assertEqual(message, MPI.MESSAGE_NULL)
#
message = MPI.Message(MPI.MESSAGE_NO_PROC)
request = message.irecv()
self.assertEqual(message, MPI.MESSAGE_NULL)
self.assertNotEqual(request, MPI.REQUEST_NULL)
request.wait()
self.assertEqual(request, MPI.REQUEST_NULL)
#
comm = MPI.COMM_SELF
message = comm.mprobe(MPI.PROC_NULL)
self.assertNotEqual(message, MPI.MESSAGE_NULL)
self.assertEqual(message, MPI.MESSAGE_NO_PROC)
noproc = comm.improbe(MPI.PROC_NULL)
self.assertNotEqual(message, MPI.MESSAGE_NULL)
self.assertEqual(message, MPI.MESSAGE_NO_PROC)
@unittest.skipIf(MPI.MESSAGE_NULL == MPI.MESSAGE_NO_PROC, "mpi-message")
class BaseTestP2PMatched:
#
COMM = MPI.COMM_NULL
def testIMProbe(self):
comm = self.COMM.Dup()
try:
m = comm.improbe()
self.assertIsNone(m)
m = comm.improbe(MPI.ANY_SOURCE)
self.assertIsNone(m)
m = comm.improbe(MPI.ANY_SOURCE, MPI.ANY_TAG)
self.assertIsNone(m)
status = MPI.Status()
m = comm.improbe(MPI.ANY_SOURCE, MPI.ANY_TAG, status)
self.assertIsNone(m)
self.assertEqual(status.source, MPI.ANY_SOURCE)
self.assertEqual(status.tag, MPI.ANY_TAG)
self.assertEqual(status.error, MPI.SUCCESS)
m = MPI.Message.iprobe(comm)
self.assertIsNone(m)
s = comm.isend(None, comm.rank, 0)
r = comm.mprobe(comm.rank, 0).irecv()
MPI.Request.waitall([s, r])
finally:
comm.Free()
def testProbeRecv(self):
comm = self.COMM
size = comm.Get_size()
rank = comm.Get_rank()
for smsg in messages:
if size == 1:
sr = comm.isend(smsg, 0, 0)
m = comm.mprobe(0, 0)
self.assertIsInstance(m, MPI.Message)
self.assertTrue(m)
rr = m.irecv()
self.assertFalse(m)
self.assertTrue(sr)
self.assertTrue(rr)
MPI.Request.Waitall([sr, rr])
self.assertFalse(sr)
self.assertFalse(rr)
#
r = comm.isend(smsg, 0, 0)
m = MPI.Message.probe(comm, 0, 0)
self.assertIsInstance(m, MPI.Message)
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
r.wait()
elif rank == 0:
comm.send(smsg, 1, 0)
m = comm.mprobe(1, 0)
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
#
comm.send(smsg, 1, 1)
m = None
while not m:
m = MPI.Message.iprobe(comm, 1, 1)
rmsg = m.irecv().wait()
elif rank == 1:
m = comm.mprobe(0, 0)
self.assertTrue(m)
rmsg = m.recv()
self.assertFalse(m)
comm.send(rmsg, 0, 0)
#
m = None
while not m:
m = MPI.Message.iprobe(comm, 0, 1)
rmsg = m.irecv().wait()
comm.send(smsg, 0, 1)
else:
rmsg = smsg
self.assertEqual(smsg, rmsg)
class TestP2PMatchedSelf(BaseTestP2PMatched, unittest.TestCase):
#
COMM = MPI.COMM_SELF
class TestP2PMatchedWorld(BaseTestP2PMatched, unittest.TestCase):
#
COMM = MPI.COMM_WORLD
@unittest.skipMPI("openmpi(<1.8.5)", MPI.COMM_WORLD.Get_size() > 1)
class TestP2PMatchedSelfDup(TestP2PMatchedSelf):
#
def setUp(self):
self.COMM = MPI.COMM_SELF.Dup()
def tearDown(self):
self.COMM.Free()
@unittest.skipMPI("openmpi(<1.8.5)", MPI.COMM_WORLD.Get_size() > 1)
class TestP2PMatchedWorldDup(TestP2PMatchedWorld):
#
def setUp(self):
self.COMM = MPI.COMM_WORLD.Dup()
def tearDown(self):
self.COMM.Free()
if __name__ == "__main__":
unittest.main()
|