File: ex-2.08.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,347 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 2 python ex-2.08.py

# An exchange of messages

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

import array

from mpi4py import MPI

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

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

sendbuf = array.array("d", [0] * 10)
recvbuf = array.array("d", [0] * 10)
tag = 0
status = MPI.Status()

myrank = MPI.COMM_WORLD.Get_rank()

if myrank == 0:
    sendbuf[:] = array.array("d", range(len(sendbuf)))
    MPI.COMM_WORLD.Send([sendbuf, MPI.DOUBLE], 1, tag)
    MPI.COMM_WORLD.Recv([recvbuf, MPI.DOUBLE], 1, tag, status)
elif myrank == 1:
    MPI.COMM_WORLD.Recv([recvbuf, MPI.DOUBLE], 0, tag, status)
    sendbuf[:] = recvbuf
    MPI.COMM_WORLD.Send([sendbuf, MPI.DOUBLE], 0, tag)

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

if myrank == 0:
    assert status.source == 1
    assert status.tag == tag
    assert status.error == MPI.SUCCESS
    assert status.Get_count(MPI.DOUBLE) == len(recvbuf)
    assert sendbuf == recvbuf
elif myrank == 1:
    assert status.source == 0
    assert status.tag == tag
    assert status.error == MPI.SUCCESS
    assert status.Get_count(MPI.DOUBLE) == len(recvbuf)
    assert sendbuf == recvbuf

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