Search code examples
python-2.7openmpimpi4py

mpi4py - MPI_ERR_TRUNCATE: message truncated


I converted the ring_c.c code from OPENMPI examples in python to experiment with mpi4py. Here is my code.

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

next_proc = (rank + 1) % size
prev_proc = (rank + size - 1) % size

tag = 2
message = 10

if 0 == rank:
    comm.send(message, dest=next_proc, tag=tag)

while(1):
    message = comm.recv(message, source=prev_proc, tag=tag)  
    comm.Recv_init

    if 0 == rank:
        message = message - 1
        print "Process %d decremented value: %d\n" %(rank, message)

    comm.send(message, dest=next_proc, tag=tag)

    if 0 == message:
        print "Process %d exiting\n" %(rank)
        break;

if 0 == rank:
    message = comm.recv(message, source=prev_proc, tag=tag)

When I run it via mpiexec for any number of processes, for example

mpiexec -n 10 python ring_py.py

It gives the following output and error:

Process 0 decremented value: 9

Process 0 decremented value: 8

Process 0 decremented value: 7

Process 0 decremented value: 6

Process 0 decremented value: 5

Process 0 decremented value: 4

Traceback (most recent call last):
  File "ring_py.py", line 20, in <module>
    message = comm.recv(message, source=prev_proc, tag=tag)  
  File "MPI/Comm.pyx", line 1192, in mpi4py.MPI.Comm.recv (src/mpi4py.MPI.c:106889)
  File "MPI/msgpickle.pxi", line 287, in mpi4py.MPI.PyMPI_recv (src/mpi4py.MPI.c:42965)
mpi4py.MPI.Exception: MPI_ERR_TRUNCATE: message truncated

A couple of observations

  • If I change the message to say 6 or 50, it always throws out the same error at Process 0 decremented value: 4
  • If I change the message value to say 4 or less, it throws out the same error without doing anything else.
  • The number of processes has no effect on the output of the code, other than the execution time.

A few details about my system.

  • I am using MacBook Air 2012 model with macOS Sierra and Intel i7 core processor.
  • I installed mpi4py via PIP in python 2.7 and its version is 2.0.0

Can someone please help me understand what is going on with my code.

Thank you, Jayant


Solution

  • I tried your code and I got an error at:

    message = comm.recv(message, source=prev_proc, tag=tag) 
    

    Stating:

    TypeError: expected a writeable buffer object

    Following the tutorial of mpi4py or MPI4Py causes error on send/recv , I successfully tried:

    message = comm.recv( source=prev_proc, tag=tag)