Commit 137ef070 authored by Jussi Enkovaara's avatar Jussi Enkovaara
Browse files

Add Python skeleton and solutions

parent 5254dd23
......@@ -4,7 +4,9 @@ Write a program that sends the highlighted elements of a 2D array
using user defined datatypes from one MPI task to another. Note the
different assignments for C and Fortran, and remember that C stores
arrays in a row-major order and Fortran in a column-major order. You can
start from skeleton codes in [C](./c) or [Fortran](./fortran)
start from skeleton codes in [C](./c), [Fortran](./fortran) or
[Python](./python). For Python use the C-patterns in a) and b) (`Note`: no
Python solutions are provided for c) and d) )
a)
......
from __future__ import print_function
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if size != 2:
raise RuntimeError("Please run with two MPI tasks")
data = np.zeros((8,8), int)
if rank == 0:
for i in range(8):
data[i,:] = np.arange(1, 9) + (i+1) * 10
if rank == 0:
print("Original data:")
print(data)
# TODO Create the custom datatype
# Note: Python integers are 64 bits
# TODO: communicate with the datatype
# Note: mpi4py requires that the input and output buffers are contiguous
# in memory. Thus, in order to send/receive from second column we need
# to create a contiguous view starting from the second element in memory
# which can be accomplished by flattening the array into 1d with ravel
if rank == 1:
print("Received data:")
print(data)
from __future__ import print_function
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if size != 2:
raise RuntimeError("Please run with two MPI tasks")
data = np.zeros((8,8), int)
if rank == 0:
for i in range(8):
data[i,:] = np.arange(1, 9) + (i+1) * 10
if rank == 0:
print("Original data:")
print(data)
# Create the custom datatype
# Note: Python integers are 64 bits
columntype = MPI.INT64_T.Create_vector(8, 1, 8)
columntype.Commit()
# mpi4py requires that the input and output buffers are contiguous
# in memory. Thus, in order to send/receive from second column we need
# to create a contiguous view starting from the second element in memory
# which can be accomplished by flattening the array into 1d with ravel
if rank == 0:
comm.Send((data.ravel()[1:], 1, columntype), dest=1)
elif rank == 1:
comm.Recv((data.ravel()[1:], 1, columntype), source=0)
if rank == 1:
print("Received data:")
print(data)
from __future__ import print_function
from mpi4py import MPI
import numpy as np
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if size != 2:
raise RuntimeError("Please run with two MPI tasks")
data = np.zeros((8,8), int)
if rank == 0:
for i in range(8):
data[i,:] = np.arange(1, 9) + (i+1) * 10
if rank == 0:
print("Original data:")
print(data)
# Create the custom datatype
# Note: Python integers are 64 bits
counts = np.arange(4, dtype=int) + 1
displacements = np.arange(4, dtype=int) * (1 + 2 * data.itemsize)
indexedtype = MPI.INT64_T.Create_indexed(counts, displacements)
indexedtype.Commit()
if rank == 0:
comm.Send((data, 1, indexedtype), dest=1)
elif rank == 1:
comm.Recv((data, 1, indexedtype), source=0)
if rank == 1:
print("Received data:")
print(data)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment