Skip to content

Instantly share code, notes, and snippets.

@bmorris3
Last active April 13, 2016 18:48
Show Gist options
  • Save bmorris3/4c51755352a23dd032afa1fb21a84ae5 to your computer and use it in GitHub Desktop.
Save bmorris3/4c51755352a23dd032afa1fb21a84ae5 to your computer and use it in GitHub Desktop.
How do you send numpy arrays with metadata between processes using mpi?
"""
Run with
mpiexec -n 11 mpi_goose_chase.py
"""
from __future__ import print_function
from mpi4py import MPI
import numpy as np
import time
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
#initializing variables. mpi4py requires that we pass numpy objects.
dims = (2048, 2048)
dtype = np.complex128
n_reconstructions = 10
# Create tag for each process, create memory buffer
tag = [rank + 0*1j]
recv_buffer = np.zeros(dims[0]*dims[1] + len(tag), dtype=dtype)
if rank == size - 1:
reconstruction_cube = np.zeros((n_reconstructions, dims[0], dims[1]),
dtype=dtype)
for i in range(0, size-1):
comm.Recv(recv_buffer, MPI.ANY_SOURCE)
process_tag = recv_buffer[:len(tag)]
process_image = recv_buffer[len(tag):].reshape(dims)
image_index = int(np.real(process_tag))
reconstruction_cube[image_index, ...] = process_image
else:
# Generate science image
local_image = np.array(rank+np.zeros(dims) + np.zeros(dims)* 1j, dtype=dtype)
# Ravel, add tags to the beginning
local_tagged_image = np.concatenate([tag, local_image.ravel()])
# all other process send their result
comm.Send(local_tagged_image, dest=size-1)
if rank == size-1:
print(reconstruction_cube)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment