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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
import os
import pathlib
import sys
import tempfile
import mpiunittest as unittest
from mpi4py import MPI
class TestErrhandler(unittest.TestCase):
#
def testPredefined(self):
self.assertFalse(MPI.ERRHANDLER_NULL)
self.assertTrue(MPI.ERRORS_ARE_FATAL)
self.assertTrue(MPI.ERRORS_RETURN)
if MPI.Get_version() >= (4, 0):
self.assertTrue(MPI.ERRORS_ABORT)
elif MPI.ERRORS_ABORT != MPI.ERRHANDLER_NULL:
self.assertTrue(MPI.ERRORS_ABORT)
else:
self.assertFalse(MPI.ERRORS_ABORT)
def testPickle(self):
from pickle import dumps, loads
for errhandler in [
MPI.ERRHANDLER_NULL,
MPI.ERRORS_ARE_FATAL,
MPI.ERRORS_RETURN,
MPI.ERRORS_ABORT,
]:
if not errhandler:
continue
errh = loads(dumps(errhandler))
self.assertIs(errh, errhandler)
errh = loads(dumps(MPI.Errhandler(errhandler)))
self.assertIsNot(errh, errhandler)
self.assertEqual(errh, errhandler)
class BaseTestErrhandler:
#
def testCreate(self):
MAX_USER_EH = 32 # max user-defined error handlers
mpiobj = self.mpiobj
index = None
called = check = False
def get_errhandler_fn(idx):
def errhandler_fn(arg, err):
nonlocal called, check
called = check = True
check &= arg == mpiobj
check &= err == MPI.ERR_OTHER
check &= idx == index
return errhandler_fn
def check_handle(eh):
cint = eh.toint()
if cint != -1:
clon = type(eh).fromint(cint)
self.assertEqual(eh, clon)
fint = eh.py2f()
if fint != -1:
clon = type(eh).f2py(fint)
self.assertEqual(eh, clon)
errhandlers = []
for index in range(MAX_USER_EH):
try:
fn = get_errhandler_fn(index)
eh = type(mpiobj).Create_errhandler(fn)
errhandlers.append(eh)
except NotImplementedError:
clsname = type(mpiobj).__name__.lower()
self.skipTest(f"mpi-{clsname}-create_errhandler")
with self.assertRaises(RuntimeError):
type(mpiobj).Create_errhandler(lambda _arg, _err: None)
for eh in errhandlers:
self.assertTrue(eh)
check_handle(eh)
with self.assertRaises(ValueError):
eh.__reduce__()
eh_orig = mpiobj.Get_errhandler()
try:
for i, eh in enumerate(errhandlers):
index = i
called = check = False
mpiobj.Set_errhandler(eh)
try:
mpiobj.Call_errhandler(MPI.SUCCESS)
mpiobj.Call_errhandler(MPI.ERR_OTHER)
except NotImplementedError:
if MPI.Get_version() >= (2, 0):
raise
else:
self.assertTrue(called)
self.assertTrue(check)
finally:
mpiobj.Set_errhandler(eh_orig)
finally:
for eh in errhandlers:
eh.Free()
def testCall(self):
mpiobj = self.mpiobj
mpiobj.Set_errhandler(MPI.ERRORS_RETURN)
try:
mpiobj.Call_errhandler(MPI.SUCCESS)
mpiobj.Call_errhandler(MPI.ERR_OTHER)
except NotImplementedError:
if MPI.Get_version() >= (2, 0):
raise
clsname = type(mpiobj).__name__.lower()
self.skipTest(f"mpi-{clsname}-call_errhandler")
def testGetFree(self):
mpiobj = self.mpiobj
errhdls = []
for _i in range(100):
e = mpiobj.Get_errhandler()
errhdls.append(e)
for e in errhdls:
e.Free()
for e in errhdls:
self.assertEqual(e, MPI.ERRHANDLER_NULL)
def _run_test_get_set(self, errhandler):
mpiobj = self.mpiobj
errhdl_1 = mpiobj.Get_errhandler()
self.assertNotEqual(errhdl_1, MPI.ERRHANDLER_NULL)
mpiobj.Set_errhandler(errhandler)
errhdl_2 = mpiobj.Get_errhandler()
self.assertEqual(errhdl_2, errhandler)
errhdl_2.Free()
self.assertEqual(errhdl_2, MPI.ERRHANDLER_NULL)
mpiobj.Set_errhandler(errhdl_1)
errhdl_1.Free()
self.assertEqual(errhdl_1, MPI.ERRHANDLER_NULL)
def testErrorsReturn(self):
self._run_test_get_set(MPI.ERRORS_RETURN)
def testErrorsFatal(self):
self._run_test_get_set(MPI.ERRORS_ARE_FATAL)
@unittest.skipUnless(MPI.ERRORS_ABORT, "mpi-errors-abort")
@unittest.skipMPI("mpich(<4.1.0)")
@unittest.skipMPI("openmpi(<5.0.0)")
@unittest.skipMPI("impi(<2021.14.0)")
def testErrorsAbort(self):
self._run_test_get_set(MPI.ERRORS_ABORT)
class TestErrhandlerComm(BaseTestErrhandler, unittest.TestCase):
#
def setUp(self):
self.mpiobj = MPI.COMM_SELF.Dup()
def tearDown(self):
self.mpiobj.Free()
@unittest.skipMPI("openmpi(<4.1.0)", sys.platform == "darwin")
class TestErrhandlerWin(BaseTestErrhandler, unittest.TestCase):
#
def setUp(self):
try:
self.mpiobj = MPI.Win.Create(
MPI.BOTTOM, 1, MPI.INFO_NULL, MPI.COMM_SELF
)
except NotImplementedError:
self.skipTest("mpi-win")
except MPI.Exception:
self.skipTest("mpi-win")
def tearDown(self):
self.mpiobj.Free()
def testCall(self):
super().testCall()
class TestErrhandlerFile(BaseTestErrhandler, unittest.TestCase):
#
def setUp(self):
rank = MPI.COMM_WORLD.Get_rank()
fd, filename = tempfile.mkstemp(prefix="mpi4py-", suffix=f"-{rank}")
os.close(fd)
amode = MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_DELETE_ON_CLOSE
try:
self.mpiobj = MPI.File.Open(
MPI.COMM_SELF, filename, amode, MPI.INFO_NULL
)
except NotImplementedError:
try:
pathlib.Path(filename).unlink()
except OSError:
pass
self.skipTest("mpi-file")
def tearDown(self):
self.mpiobj.Close()
@unittest.skipMPI("msmpi")
def testCall(self):
super().testCall()
class TestErrhandlerSession(BaseTestErrhandler, unittest.TestCase):
#
def setUp(self):
try:
self.mpiobj = MPI.Session.Init()
except NotImplementedError:
self.skipTest("mpi-session")
def tearDown(self):
self.mpiobj.Finalize()
if __name__ == "__main__":
unittest.main()
|