File: ex-3.09.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 (39 lines) | stat: -rw-r--r-- 1,055 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
from mpi4py import MPI

try:
    import numpy
except ImportError:
    raise SystemExit from None

# transpose a matrix a into b

a = numpy.empty((100, 100), dtype=float, order="f")
b = numpy.empty((100, 100), dtype=float, order="f")
a.flat = numpy.arange(a.size, dtype=float)

lb, sizeofdouble = MPI.DOUBLE.Get_extent()

# create datatype dor one row
# (vector with 100 double entries and stride 100)
row = MPI.DOUBLE.Create_vector(100, 1, 100)

# create datatype for matrix in row-major order

# (one hundred copies of the row datatype, strided one word
#  apart; the successive row datatypes are interleaved)
xpose = row.Create_hvector(100, 1, sizeofdouble)
xpose.Commit()

# send matrix in row-major order and receive in column major order
abuf = (a, xpose)
bbuf = (b, MPI.DOUBLE)
myrank = MPI.COMM_WORLD.Get_rank()
status = MPI.Status()
MPI.COMM_WORLD.Sendrecv(abuf, myrank, 0, bbuf, myrank, 0, status)

assert numpy.allclose(a, b.transpose())
assert status.Get_count(xpose) == 1
assert status.Get_count(MPI.DOUBLE) == b.size

row.Free()
xpose.Free()