File: test_subclass.py

package info (click to toggle)
mpi4py 4.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,540 kB
  • sloc: python: 34,465; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (461 lines) | stat: -rw-r--r-- 10,002 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import os
import pathlib
import tempfile

import mpiunittest as unittest

from mpi4py import MPI

# ---


class MyBaseComm:
    #
    def free(self):
        if self != MPI.COMM_NULL:
            MPI.Comm.Free(self)


class BaseTestBaseComm:
    #
    def setUp(self):
        self.comm = self.CommType(self.COMM_BASE)

    def testSubType(self):
        self.assertNotIn(
            type(self.comm),
            [
                MPI.Comm,
                MPI.Intracomm,
                MPI.Cartcomm,
                MPI.Graphcomm,
                MPI.Distgraphcomm,
                MPI.Intercomm,
            ],
        )
        self.assertIsInstance(self.comm, self.CommType)

    def testCloneFree(self):
        if self.COMM_BASE != MPI.COMM_NULL:
            comm = self.comm.Clone()
        else:
            comm = self.CommType()
        self.assertIsInstance(comm, MPI.Comm)
        self.assertIsInstance(comm, self.CommType)
        comm.free()

    def tearDown(self):
        self.comm.free()


# ---


class MyComm(MPI.Comm, MyBaseComm):
    #
    def __new__(cls, comm=None):
        if comm is not None:
            if comm != MPI.COMM_NULL:
                comm = comm.Clone()
        return super().__new__(cls, comm)


class BaseTestMyComm(BaseTestBaseComm):
    #
    CommType = MyComm


class TestMyCommNULL(BaseTestMyComm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_NULL


class TestMyCommSELF(BaseTestMyComm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_SELF


class TestMyCommWORLD(BaseTestMyComm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_WORLD


# ---


class MyIntracomm(MPI.Intracomm, MyBaseComm):
    #
    def __new__(cls, comm=None):
        if comm is not None:
            if comm != MPI.COMM_NULL:
                comm = comm.Dup()
        return super().__new__(cls, comm)


class BaseTestMyIntracomm(BaseTestBaseComm):
    #
    CommType = MyIntracomm


class TestMyIntracommNULL(BaseTestMyIntracomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_NULL


class TestMyIntracommSELF(BaseTestMyIntracomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_SELF


class TestMyIntracommWORLD(BaseTestMyIntracomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_WORLD


# ---


class MyCartcomm(MPI.Cartcomm, MyBaseComm):
    #
    def __new__(cls, comm=None):
        if comm is not None:
            if comm != MPI.COMM_NULL:
                dims = [comm.size]
                comm = comm.Create_cart(dims)
        return super().__new__(cls, comm)


class BaseTestMyCartcomm(BaseTestBaseComm):
    #
    CommType = MyCartcomm


class TestMyCartcommNULL(BaseTestMyCartcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_NULL


class TestMyCartcommSELF(BaseTestMyCartcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_SELF


class TestMyCartcommWORLD(BaseTestMyCartcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_WORLD


# ---


class MyGraphcomm(MPI.Graphcomm, MyBaseComm):
    #
    def __new__(cls, comm=None):
        if comm is not None:
            if comm != MPI.COMM_NULL:
                index = list(range(comm.size + 1))
                edges = list(range(comm.size))
                comm = comm.Create_graph(index, edges)
        return super().__new__(cls, comm)


class BaseTestMyGraphcomm(BaseTestBaseComm):
    #
    CommType = MyGraphcomm


class TestMyGraphcommNULL(BaseTestMyGraphcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_NULL


class TestMyGraphcommSELF(BaseTestMyGraphcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_SELF


class TestMyGraphcommWORLD(BaseTestMyGraphcomm, unittest.TestCase):
    #
    COMM_BASE = MPI.COMM_WORLD


# ---


class MyRequest(MPI.Request):
    #
    def __new__(cls, request=None):
        return super().__new__(cls, request)

    def test(self):
        return super(type(self), self).Test()

    def wait(self):
        return super(type(self), self).Wait()


class MyPrequest(MPI.Prequest):
    #
    def __new__(cls, request=None):
        return super().__new__(cls, request)

    def test(self):
        return super(type(self), self).Test()

    def wait(self):
        return super(type(self), self).Wait()

    def start(self):
        return super(type(self), self).Start()


class MyGrequest(MPI.Grequest):
    #
    def __new__(cls, request=None):
        return super().__new__(cls, request)

    def test(self):
        return super(type(self), self).Test()

    def wait(self):
        return super(type(self), self).Wait()


class BaseTestMyRequest:
    #
    def setUp(self):
        self.req = self.MyRequestType(MPI.REQUEST_NULL)

    def testSubType(self):
        self.assertIsNot(type(self.req), self.MPIRequestType)
        self.assertIsInstance(self.req, self.MPIRequestType)
        self.assertIsInstance(self.req, self.MyRequestType)
        self.req.test()


class TestMyRequest(BaseTestMyRequest, unittest.TestCase):
    #
    MPIRequestType = MPI.Request
    MyRequestType = MyRequest


class TestMyPrequest(BaseTestMyRequest, unittest.TestCase):
    #
    MPIRequestType = MPI.Prequest
    MyRequestType = MyPrequest


class TestMyGrequest(BaseTestMyRequest, unittest.TestCase):
    #
    MPIRequestType = MPI.Grequest
    MyRequestType = MyGrequest


class TestMyRequest2(TestMyRequest):
    #
    def setUp(self):
        req = MPI.COMM_SELF.Isend(
            [MPI.BOTTOM, 0, MPI.BYTE], dest=MPI.PROC_NULL, tag=0
        )
        self.req = MyRequest(req)


@unittest.skipMPI("mpich(==3.4.1)")
class TestMyPrequest2(TestMyPrequest):
    #
    def setUp(self):
        req = MPI.COMM_SELF.Send_init(
            [MPI.BOTTOM, 0, MPI.BYTE], dest=MPI.PROC_NULL, tag=0
        )
        self.req = MyPrequest(req)

    def tearDown(self):
        self.req.Free()

    def testStart(self):
        for _i in range(5):
            self.req.start()
            self.req.test()
            self.req.start()
            self.req.wait()


# ---


class MyInfo(MPI.Info):
    #
    def __new__(cls, info=None):
        return MPI.Info.__new__(cls, info)

    def free(self):
        if self != MPI.INFO_NULL:
            MPI.Info.Free(self)


class BaseTestMyInfo:
    #
    def setUp(self):
        info = MPI.Info.Create()
        self.info = MyInfo(info)

    def tearDown(self):
        self.info.free()

    def testSubType(self):
        self.assertIsNot(type(self.info), MPI.Info)
        self.assertIsInstance(self.info, MPI.Info)
        self.assertIsInstance(self.info, MyInfo)

    def testFree(self):
        self.assertTrue(self.info)
        self.info.free()
        self.assertFalse(self.info)

    def testCreateDupType(self):
        for info in (
            MyInfo.Create(),
            self.info.Dup(),
            self.info.copy(),
        ):
            self.assertIsNot(type(info), MPI.Info)
            self.assertIsInstance(info, MPI.Info)
            self.assertIsInstance(info, MyInfo)
            info.free()

    def testCreateEnvType(self):
        try:
            info = MyInfo.Create_env()
        except NotImplementedError:
            if MPI.Get_version() >= (4, 0):
                raise
            raise unittest.SkipTest("mpi-info-create-env") from None
        self.assertIsNot(type(info), MPI.Info)
        self.assertIsInstance(info, MPI.Info)
        self.assertIsInstance(info, MyInfo)

    def testPickle(self):
        from pickle import dumps, loads

        items = list(zip("abc", "123"))
        self.info.update(items)
        info = loads(dumps(self.info))
        self.assertIs(type(info), MyInfo)
        self.assertEqual(info.items(), items)
        info.free()


class TestMyInfo(BaseTestMyInfo, unittest.TestCase):
    #
    pass


try:
    MPI.Info.Create().Free()
except (NotImplementedError, MPI.Exception):
    unittest.disable(BaseTestMyInfo, "mpi-info")

# ---


class MyWin(MPI.Win):
    #
    def __new__(cls, win=None):
        return MPI.Win.__new__(cls, win)

    def free(self):
        if self != MPI.WIN_NULL:
            MPI.Win.Free(self)


class BaseTestMyWin:
    #
    def setUp(self):
        w = MPI.Win.Create(MPI.BOTTOM)
        self.win = MyWin(w)

    def tearDown(self):
        self.win.free()

    def testSubType(self):
        self.assertIsNot(type(self.win), MPI.Win)
        self.assertIsInstance(self.win, MPI.Win)
        self.assertIsInstance(self.win, MyWin)

    def testFree(self):
        self.assertTrue(self.win)
        self.win.free()
        self.assertFalse(self.win)


class TestMyWin(BaseTestMyWin, unittest.TestCase):
    #
    pass


try:
    MPI.Win.Create(MPI.BOTTOM).Free()
except (NotImplementedError, MPI.Exception):
    unittest.disable(BaseTestMyWin, "mpi-win")

# ---


class MyFile(MPI.File):
    #
    def __new__(cls, file=None):
        return MPI.File.__new__(cls, file)

    def close(self):
        if self != MPI.FILE_NULL:
            MPI.File.Close(self)


class BaseTestMyFile:
    #
    def openfile(self):
        fd, fname = tempfile.mkstemp(prefix="mpi4py")
        os.close(fd)
        fname = pathlib.Path(fname)
        amode = MPI.MODE_RDWR | MPI.MODE_CREATE | MPI.MODE_DELETE_ON_CLOSE
        try:
            self.file = MPI.File.Open(
                MPI.COMM_SELF, fname, amode, MPI.INFO_NULL
            )
        except Exception:
            fname.unlink()
            raise
        return self.file

    def setUp(self):
        f = self.openfile()
        self.file = MyFile(f)

    def tearDown(self):
        self.file.close()

    def testSubType(self):
        self.assertIsNot(type(self.file), MPI.File)
        self.assertIsInstance(self.file, MPI.File)
        self.assertIsInstance(self.file, MyFile)

    def testFree(self):
        self.assertTrue(self.file)
        self.file.close()
        self.assertFalse(self.file)


class TestMyFile(BaseTestMyFile, unittest.TestCase):
    #
    pass


try:
    BaseTestMyFile().openfile().Close()
except NotImplementedError:
    unittest.disable(BaseTestMyFile, "mpi-file")

# ---

if __name__ == "__main__":
    unittest.main()