File: test_grequest.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 (196 lines) | stat: -rw-r--r-- 6,585 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
import mpitestutil as testutil
import mpiunittest as unittest

from mpi4py import MPI


class GReqCtx:
    #
    source = 3
    tag = 7
    completed = False

    cancel_called = False
    free_called = False

    def query(self, status):
        status.Set_source(self.source)
        status.Set_tag(self.tag)

    def free(self):
        self.free_called = True

    def cancel(self, completed):
        self.cancel_called = True
        if completed is not self.completed:
            raise MPI.Exception(MPI.ERR_PENDING)


@unittest.skipMPI("MPI(<2.0)")
@unittest.skipMPI("openmpi(==4.1.0)")
class TestGrequest(unittest.TestCase):
    #
    def testConstructor(self):
        ctx = GReqCtx()
        greq = MPI.Grequest.Start(ctx.query, ctx.free, ctx.cancel)
        dupe = MPI.Grequest(greq)
        self.assertIs(type(dupe), MPI.Grequest)
        self.assertEqual(dupe, greq)
        dupe = MPI.Grequest.fromhandle(greq.handle)
        self.assertIs(type(dupe), MPI.Grequest)
        self.assertEqual(dupe, greq)
        if greq.toint() != -1:
            dupe = MPI.Grequest.fromint(greq.toint())
            self.assertIs(type(dupe), MPI.Grequest)
            self.assertEqual(dupe, greq)
        if greq.py2f() != -1:
            dupe = MPI.Grequest.f2py(greq.py2f())
            self.assertIs(type(dupe), MPI.Grequest)
            self.assertEqual(dupe, greq)
        dupe = MPI.Request(greq)
        self.assertIs(type(dupe), MPI.Request)
        self.assertEqual(dupe, greq)
        with self.assertRaises(TypeError):
            dupe = MPI.Prequest(greq)
        greq.Cancel()
        greq.Complete()
        greq.Wait()

    @unittest.skipMPI("openmpi")  # TODO(dalcinl): open-mpi/ompi#11681
    def testExceptionHandling(self):
        ctx = GReqCtx()

        def raise_mpi(*_args):
            raise MPI.Exception(MPI.ERR_BUFFER)

        def raise_rte(*_args):
            raise ValueError(42)

        def check_exc(exception, is_mpi, stderr):
            output = stderr.getvalue()
            header = "Traceback (most recent call last):\n"
            if is_mpi:
                chkcode = MPI.ERR_BUFFER
                excname = MPI.Exception.__name__
            else:
                chkcode = MPI.ERR_OTHER
                excname = ValueError.__name__
            ierr = exception.Get_error_class()
            self.assertEqual(ierr, chkcode)
            self.assertTrue(output.startswith(header))
            self.assertIn(excname, output)

        for raise_fn, is_mpi in (
            (raise_mpi, True),
            (raise_rte, False),
        ):
            greq = MPI.Grequest.Start(raise_fn, ctx.free, ctx.cancel)
            greq.Complete()
            with self.assertRaises(MPI.Exception) as exc_cm:
                with testutil.capture_stderr() as stderr:
                    greq.Wait()
            if greq:
                greq.Free()
            check_exc(exc_cm.exception, is_mpi, stderr)
            #
            greq = MPI.Grequest.Start(ctx.query, raise_fn, ctx.cancel)
            greq.Complete()
            with self.assertRaises(MPI.Exception) as exc_cm:
                with testutil.capture_stderr() as stderr:
                    greq.Wait()
            if greq:
                greq.Free()
            check_exc(exc_cm.exception, is_mpi, stderr)
            #
            greq = MPI.Grequest.Start(ctx.query, ctx.free, raise_fn)
            with self.assertRaises(MPI.Exception) as exc_cm:
                with testutil.capture_stderr() as stderr:
                    greq.Cancel()
            greq.Complete()
            greq.Wait()
            if greq:
                greq.Free()
            check_exc(exc_cm.exception, is_mpi, stderr)

    def testAll(self):
        ctx = GReqCtx()
        greq = MPI.Grequest.Start(ctx.query, ctx.free, ctx.cancel)
        self.assertFalse(greq.Test())
        self.assertFalse(ctx.free_called)
        greq.Cancel()
        self.assertTrue(ctx.cancel_called)
        ctx.cancel_called = False
        greq.Complete()
        ctx.completed = True
        greq.Cancel()
        self.assertTrue(ctx.cancel_called)
        status = MPI.Status()
        self.assertTrue(greq.Test(status))
        self.assertEqual(status.Get_source(), ctx.source)
        self.assertEqual(status.Get_tag(), ctx.tag)
        self.assertEqual(status.Get_error(), MPI.SUCCESS)
        greq.Wait()
        self.assertTrue(ctx.free_called)

    def testAll1(self):
        ctx = GReqCtx()
        greq = MPI.Grequest.Start(ctx.query, None, None)
        self.assertFalse(greq.Test())
        greq.Cancel()
        greq.Complete()
        status = MPI.Status()
        self.assertTrue(greq.Test(status))
        self.assertEqual(status.Get_source(), ctx.source)
        self.assertEqual(status.Get_tag(), ctx.tag)
        self.assertEqual(status.Get_error(), MPI.SUCCESS)
        self.assertFalse(status.Is_cancelled())
        greq.Wait()

    def testAll2(self):
        greq = MPI.Grequest.Start(None, None, None)
        self.assertFalse(greq.Test())
        greq.Cancel()
        greq.Complete()
        status = MPI.Status()
        self.assertTrue(greq.Test(status))
        self.assertEqual(status.Get_source(), MPI.ANY_SOURCE)
        self.assertEqual(status.Get_tag(), MPI.ANY_TAG)
        self.assertEqual(status.Get_error(), MPI.SUCCESS)
        self.assertFalse(status.Is_cancelled())
        greq.Wait()

    def testPyCompleteTest(self):
        greq = MPI.Grequest.Start()
        self.assertFalse(greq.Test())
        greq.cancel()
        greq.complete(42)
        status = MPI.Status()
        flag, result = greq.test(status)
        self.assertTrue(flag)
        self.assertEqual(result, 42)
        self.assertEqual(status.Get_source(), MPI.ANY_SOURCE)
        self.assertEqual(status.Get_tag(), MPI.ANY_TAG)
        self.assertEqual(status.Get_error(), MPI.SUCCESS)
        self.assertFalse(status.Is_cancelled())
        obj = greq.wait()
        self.assertIsNone(obj)

    def testPyCompleteWait(self):
        greq = MPI.Grequest.Start()
        self.assertFalse(greq.Test())
        greq.cancel()
        greq.complete(42)
        status = MPI.Status()
        result = greq.wait(status)
        self.assertEqual(result, 42)
        self.assertEqual(status.Get_source(), MPI.ANY_SOURCE)
        self.assertEqual(status.Get_tag(), MPI.ANY_TAG)
        self.assertEqual(status.Get_error(), MPI.SUCCESS)
        self.assertFalse(status.Is_cancelled())
        flag, obj = greq.test()
        self.assertTrue(flag)
        self.assertIsNone(obj)


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