File: ex-2.01.py

package info (click to toggle)
mpi4py 1.3%2Bhg20120611-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,020 kB
  • sloc: python: 9,503; ansic: 6,296; makefile: 571; f90: 158; sh: 146; cpp: 103
file content (39 lines) | stat: -rw-r--r-- 1,007 bytes parent folder | download | duplicates (6)
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
## mpiexec -n 2 python ex-2.01.py

# Process 0 sends a message to process 1

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

from mpi4py import MPI
import array

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

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

s = "Hello there"

msg = array.array('c', '\0'*20)
tag = 99
status = MPI.Status()

myrank = MPI.COMM_WORLD.Get_rank()

if myrank == 0:
    msg[:len(s)] = array.array('c', s)
    MPI.COMM_WORLD.Send([msg, len(s)+1, MPI.CHAR], 1, tag)
elif myrank == 1:
    MPI.COMM_WORLD.Recv([msg, 20, MPI.CHAR], 0, tag, status)

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

if myrank == 1:
    assert list(msg[:len(s)]) == list(s)
    assert msg[len(s)] == '\0'
    assert status.source == 0
    assert status.tag == tag
    assert status.error == MPI.SUCCESS
    assert status.Get_count(MPI.CHAR) == len(s)+1

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