File: ex-2.29.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 (47 lines) | stat: -rw-r--r-- 1,211 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
# mpiexec -n 3 python ex-2.29.py

# Use a blocking probe to wait for an incoming message

# --------------------------------------------------------------------

import array

from mpi4py import MPI

if MPI.COMM_WORLD.Get_size() < 3:
    raise SystemExit from None

# --------------------------------------------------------------------

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
    i = array.array("i", [7] * 5)
    comm.Send([i, MPI.INT], 2, 0)
elif rank == 1:
    x = array.array("f", [7] * 5)
    comm.Send([x, MPI.FLOAT], 2, 0)
elif rank == 2:
    i = array.array("i", [0] * 5)
    x = array.array("f", [0] * 5)
    status = MPI.Status()
    for _j in range(2):
        comm.Probe(MPI.ANY_SOURCE, 0, status)
        if status.Get_source() == 0:
            comm.Recv([i, MPI.INT], 0, 0, status)
        else:
            comm.Recv([x, MPI.FLOAT], 1, 0, status)

# --------------------------------------------------------------------

if rank == 2:
    for v in i:
        assert v == 7
    for v in x:
        assert v == 7
    assert status.source in (0, 1)
    assert status.tag == 0
    assert status.error == 0

# --------------------------------------------------------------------