File: test_cco_buf_inter.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 (278 lines) | stat: -rw-r--r-- 10,837 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
import arrayimpl
import mpiunittest as unittest

from mpi4py import MPI


def skip_op(typecode, op):
    if typecode == "?":
        return True
    if typecode in "FDG":
        if op in (MPI.MAX, MPI.MIN):
            return True
    return False


def maxvalue(a):
    try:
        typecode = a.typecode
    except AttributeError:
        typecode = a.dtype.char
    if typecode == ("f"):
        return 1e30
    elif typecode == ("d"):
        return 1e300
    else:
        return 2 ** (a.itemsize * 7) - 1


@unittest.skipMPI("openmpi(<1.6.0)")
@unittest.skipMPI("msmpi", MPI.COMM_WORLD.Get_size() >= 3)
@unittest.skipMPI("MPICH1")
@unittest.skipIf(MPI.ROOT == MPI.PROC_NULL, "mpi-root")
@unittest.skipIf(MPI.COMM_WORLD.Get_size() < 2, "mpi-world-size<2")
class BaseTestCCOBufInter:
    #
    BASECOMM = MPI.COMM_NULL
    INTRACOMM = MPI.COMM_NULL
    INTERCOMM = MPI.COMM_NULL

    def setUp(self):
        size = self.BASECOMM.Get_size()
        rank = self.BASECOMM.Get_rank()
        if rank < size // 2:
            self.COLOR = 0
            self.LOCAL_LEADER = 0
            self.REMOTE_LEADER = size // 2
        else:
            self.COLOR = 1
            self.LOCAL_LEADER = 0
            self.REMOTE_LEADER = 0
        self.INTRACOMM = self.BASECOMM.Split(self.COLOR, key=0)
        Create_intercomm = MPI.Intracomm.Create_intercomm
        self.INTERCOMM = Create_intercomm(
            self.INTRACOMM,
            self.LOCAL_LEADER,
            self.BASECOMM,
            self.REMOTE_LEADER,
        )

    def tearDown(self):
        self.INTRACOMM.Free()
        self.INTERCOMM.Free()

    def testBarrier(self):
        self.INTERCOMM.Barrier()

    def testBcast(self):
        comm = self.INTERCOMM
        rank = comm.Get_rank()
        size = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for color in (0, 1):
                if self.COLOR == color:
                    for root in range(size):
                        if root == rank:
                            buf = array(root, typecode, root + color)
                            comm.Bcast(buf.as_mpi(), root=MPI.ROOT)
                        else:
                            comm.Bcast(None, root=MPI.PROC_NULL)
                else:
                    for root in range(rsize):
                        buf = array(-1, typecode, root + color)
                        comm.Bcast(buf.as_mpi(), root=root)
                        check = arrayimpl.scalar(root)
                        for value in buf:
                            self.assertEqual(value, check)

    def testGather(self):
        comm = self.INTERCOMM
        rank = comm.Get_rank()
        size = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for color in (0, 1):
                if self.COLOR == color:
                    for root in range(size):
                        if root == rank:
                            rbuf = array(-1, typecode, (rsize, root + color))
                            comm.Gather(None, rbuf.as_mpi(), root=MPI.ROOT)
                            check = arrayimpl.scalar(root)
                            for value in rbuf.flat:
                                self.assertEqual(value, check)
                        else:
                            comm.Gather(None, None, root=MPI.PROC_NULL)
                else:
                    for root in range(rsize):
                        sbuf = array(root, typecode, root + color)
                        comm.Gather(sbuf.as_mpi(), None, root=root)

    def testScatter(self):
        comm = self.INTERCOMM
        rank = comm.Get_rank()
        size = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for color in (0, 1):
                if self.COLOR == color:
                    for root in range(size):
                        if root == rank:
                            sbuf = array(root, typecode, (rsize, root + color))
                            comm.Scatter(sbuf.as_mpi(), None, root=MPI.ROOT)
                        else:
                            comm.Scatter(None, None, root=MPI.PROC_NULL)
                else:
                    for root in range(rsize):
                        rbuf = array(root, typecode, root + color)
                        comm.Scatter(None, rbuf.as_mpi(), root=root)
                        check = arrayimpl.scalar(root)
                        for value in rbuf:
                            self.assertEqual(value, check)

    def testAllgather(self):
        comm = self.INTERCOMM
        comm.Get_rank()
        size = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for color in (0, 1):
                if self.COLOR == color:
                    for n in range(size):
                        sbuf = array(n, typecode, color)
                        rbuf = array(-1, typecode, (rsize, n + color))
                        comm.Allgather(sbuf.as_mpi(), rbuf.as_mpi())
                        check = arrayimpl.scalar(n)
                        for value in rbuf.flat:
                            self.assertEqual(value, check)
                else:
                    for n in range(rsize):
                        sbuf = array(n, typecode, n + color)
                        rbuf = array(-1, typecode, (rsize, color))
                        comm.Allgather(sbuf.as_mpi(), rbuf.as_mpi())
                        check = arrayimpl.scalar(n)
                        for value in rbuf.flat:
                            self.assertEqual(value, check)

    def testAlltoall(self):
        comm = self.INTERCOMM
        comm.Get_rank()
        size = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for color in (0, 1):
                if self.COLOR == color:
                    for n in range(size):
                        sbuf = array(n, typecode, (rsize, (n + 1) * color))
                        rbuf = array(-1, typecode, (rsize, n + 3 * color))
                        comm.Alltoall(sbuf.as_mpi(), rbuf.as_mpi())
                        check = arrayimpl.scalar(n)
                        for value in rbuf.flat:
                            self.assertEqual(value, check)
                else:
                    for n in range(rsize):
                        sbuf = array(n, typecode, (rsize, n + 3 * color))
                        rbuf = array(-1, typecode, (rsize, (n + 1) * color))
                        comm.Alltoall(sbuf.as_mpi(), rbuf.as_mpi())
                        check = arrayimpl.scalar(n)
                        for value in rbuf.flat:
                            self.assertEqual(value, check)

    @unittest.skipMPI("mvapich", MPI.COMM_WORLD.Get_size() > 2)
    def testReduce(self):
        comm = self.INTERCOMM
        rank = comm.Get_rank()
        lsize = comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
                if skip_op(typecode, op):
                    continue
                for color in (0, 1):
                    if self.COLOR == color:
                        for root in range(lsize):
                            if root == rank:
                                rbuf = array(-1, typecode, rsize)
                                comm.Reduce(
                                    None,
                                    rbuf.as_mpi(),
                                    op=op,
                                    root=MPI.ROOT,
                                )
                                max_val = maxvalue(rbuf)
                                for i, value in enumerate(rbuf):
                                    if op == MPI.SUM:
                                        if (i * rsize) < max_val:
                                            self.assertAlmostEqual(
                                                value, i * rsize
                                            )
                                    elif op == MPI.PROD:
                                        if (i**rsize) < max_val:
                                            self.assertAlmostEqual(
                                                value, i**rsize
                                            )
                                    elif op == MPI.MAX:
                                        self.assertEqual(value, i)
                                    elif op == MPI.MIN:
                                        self.assertEqual(value, i)
                            else:
                                comm.Reduce(
                                    None,
                                    None,
                                    op=op,
                                    root=MPI.PROC_NULL,
                                )
                    else:
                        for root in range(rsize):
                            sbuf = array(range(lsize), typecode)
                            comm.Reduce(
                                sbuf.as_mpi(),
                                None,
                                op=op,
                                root=root,
                            )

    def testAllreduce(self):
        comm = self.INTERCOMM
        comm.Get_rank()
        comm.Get_size()
        rsize = comm.Get_remote_size()
        for array, typecode in arrayimpl.loop():
            for op in (MPI.SUM, MPI.PROD, MPI.MAX, MPI.MIN):
                if skip_op(typecode, op):
                    continue
                sbuf = array(range(5), typecode)
                rbuf = array([-1] * 5, typecode)
                comm.Allreduce(sbuf.as_mpi(), rbuf.as_mpi(), op)
                max_val = maxvalue(rbuf)
                for i, value in enumerate(rbuf):
                    if op == MPI.SUM:
                        if (i * rsize) < max_val:
                            self.assertAlmostEqual(value, i * rsize)
                    elif op == MPI.PROD:
                        if (i**rsize) < max_val:
                            self.assertAlmostEqual(value, i**rsize)
                    elif op == MPI.MAX:
                        self.assertEqual(value, i)
                    elif op == MPI.MIN:
                        self.assertEqual(value, i)


class TestCCOBufInter(BaseTestCCOBufInter, unittest.TestCase):
    #
    BASECOMM = MPI.COMM_WORLD


class TestCCOBufInterDup(TestCCOBufInter):
    #
    def setUp(self):
        self.BASECOMM = self.BASECOMM.Dup()
        super().setUp()

    def tearDown(self):
        self.BASECOMM.Free()
        super().tearDown()


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