Search code examples
pythonmpimpi4pymemory-layout

Python: Sending discontinuous data with mpi4py


I have a C-ordered matrix of dimensions (N,M)

mat = np.random.randn(N, M)

of which I want to send a column through a persistent MPI request to another node. However, using mpi4py,

sreq = MPI.COMM_WORLD.Send_Init((mat[:,idx], MPI.DOUBLE), send_id, tag)

fails on account of the slice being non-contiguous. Can someone suggest a way of going about this? I believe in C that MPI_Type_vector allows for one to specify a stride when creating a type. How can I accomplish this with mpi4py?


Solution

  • create a sendbuffer! look at this example:

      1 #!/usr/bin/python2
      2 # -*- coding: utf-8 -*-
      3
      4 from mpi4py import MPI
      5 import numpy as np
      6
      7 comm = MPI.COMM_WORLD
      8 rank = comm.Get_rank()
      9
     10 matrix = np.empty((5, 10), dtype='f')
     11 for y in xrange(len(matrix)):
     12     for x in xrange(len(matrix[0])):
     13         matrix[y,x] = rank * 10 + x * y
     14
     15 sendbuf = np.empty(5, dtype='f')
     16
     17 #column 1
     18 sendbuf[:] = matrix[:,1]
     19
     20 result = comm.gather(sendbuf, root=0)
     21
     22 if rank == 0:
     23     for res in result:
     24         print res
    

    this will give you:

    $ mpirun -np 4 column.py
    [ 0.  1.  2.  3.  4.]
    [ 10.  11.  12.  13.  14.]
    [ 20.  21.  22.  23.  24.]
    [ 30.  31.  32.  33.  34.]