How to receive an unknown message length with MPI_Sendrecv

  • Follow


How does one use the MPI_Sendrecv function (instead of MPI_Send and
MPI_Recv) to assure they don't have a deadlock their code when the
size of the data being received is unknown prior to the send? From my
research so far, this problem is usually solved by using MPI_Send and
MPI_Recv (or some variation of these basic functions) and preceding
the MPI_Recv with something like:

MPI_Probe(source,tag,comm,&status);
MPI_Get_count(&status,mpi_datatype,&count);
buf=new datatype[count];
MPI_Recv(buf,count,mpi_datatype,source,0,MPI_COMM_WORLD,&status);

I tried the following code example and it appeared that the code got
hung up on the MPI_Probe call (right and left refer to neighbor nodes'
id's).

if(mynode==0){
        MPI_Probe(right,right,MPI_COMM_WORLD,&status);
        MPI_Get_count(&status,MPI_DOUBLE,&count);
        receive=new double[count];
 
MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,right,mynode,&receive_right[0],count,MPI_DOUBLE,right,right,MPI_COMM_WORLD,&status);
    }
    else if(mynode==(numnodes-1)){
        MPI_Probe(left,left,MPI_COMM_WORLD,&status);
        MPI_Get_count(&status,MPI_DOUBLE,&count);
        receive=new double[count];
 
MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,left,mynode,&receive_left[0],count,MPI_DOUBLE,left,left,MPI_COMM_WORLD,&status);
    }
    else{
        MPI_Probe(right,right,MPI_COMM_WORLD,&status);
        MPI_Get_count(&status,MPI_DOUBLE,&count);
        receive=new double[count];
 
MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,right,mynode,&receive_right[0],count,MPI_DOUBLE,right,right,MPI_COMM_WORLD,&status);
        MPI_Probe(left,left,MPI_COMM_WORLD,&status);
        MPI_Get_count(&status,MPI_DOUBLE,&count);
        receive=new double[count];
 
MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,left,mynode,&receive_left[0],count,MPI_DOUBLE,left,left,MPI_COMM_WORLD,&status);
    }

Does anyone know how to find out the length of the data to be received
using the MPI_Sendrecv function without having to send two messages
(one just for the data size and the other for the actual data)? I
guess that a programmer could always provide over sized receive
buffers that would be sure to fit any possible data length but usually
people don't have the memory to waste with that solution either.
Thanks for you insights on this.

-Darcy
0
Reply plasma.simulation (1) 1/29/2008 6:09:54 PM

I would be interested where your research gave such an example.  MPI is 
such that the size argument is the MAXIMUM size, not exact size.  Just 
set size to be big enough for the biggest message (and use a buffer 
large enough), and after you receive you can use the get count routine 
to find out how big a message was actually received.

You are hanging because you are probing before a message has been sent. 
  Since you are using a sendrecv, you cannot probe for the message. 
Also, from your example code, you should know the size of the message 
because you are sending the same size as you are receiving.  I assume 
this is just because you tried to make a simple example.  Unless there 
is some inherent reason you cannot just receive into a MAX sized buffer 
and check the size after completeion, that is your best solution.  If 
this is undoable, then I don't know of any solution other than two 
messages if you are set on using sendrecv.

On a note of elegance, you may want to try something like this:
if (mynode == 0) left = MPI_PROC_NULL;
   else left = mynode - 1;
if (mynode == numnodes-1) right = MPI_RPOC_NULL;
   else right = mynode+1;

Then you don't need to check rank before doing the sendrecvs.

I hope this help.

Dave.

Darcy wrote:
> How does one use the MPI_Sendrecv function (instead of MPI_Send and
> MPI_Recv) to assure they don't have a deadlock their code when the
> size of the data being received is unknown prior to the send? From my
> research so far, this problem is usually solved by using MPI_Send and
> MPI_Recv (or some variation of these basic functions) and preceding
> the MPI_Recv with something like:
> 
> MPI_Probe(source,tag,comm,&status);
> MPI_Get_count(&status,mpi_datatype,&count);
> buf=new datatype[count];
> MPI_Recv(buf,count,mpi_datatype,source,0,MPI_COMM_WORLD,&status);
> 
> I tried the following code example and it appeared that the code got
> hung up on the MPI_Probe call (right and left refer to neighbor nodes'
> id's).
> 
> if(mynode==0){
>         MPI_Probe(right,right,MPI_COMM_WORLD,&status);
>         MPI_Get_count(&status,MPI_DOUBLE,&count);
>         receive=new double[count];
>  
> MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,right,mynode,&receive_right[0],count,MPI_DOUBLE,right,right,MPI_COMM_WORLD,&status);
>     }
>     else if(mynode==(numnodes-1)){
>         MPI_Probe(left,left,MPI_COMM_WORLD,&status);
>         MPI_Get_count(&status,MPI_DOUBLE,&count);
>         receive=new double[count];
>  
> MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,left,mynode,&receive_left[0],count,MPI_DOUBLE,left,left,MPI_COMM_WORLD,&status);
>     }
>     else{
>         MPI_Probe(right,right,MPI_COMM_WORLD,&status);
>         MPI_Get_count(&status,MPI_DOUBLE,&count);
>         receive=new double[count];
>  
> MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,right,mynode,&receive_right[0],count,MPI_DOUBLE,right,right,MPI_COMM_WORLD,&status);
>         MPI_Probe(left,left,MPI_COMM_WORLD,&status);
>         MPI_Get_count(&status,MPI_DOUBLE,&count);
>         receive=new double[count];
>  
> MPI_Sendrecv(&differ[0],size,MPI_DOUBLE,left,mynode,&receive_left[0],count,MPI_DOUBLE,left,left,MPI_COMM_WORLD,&status);
>     }
> 
> Does anyone know how to find out the length of the data to be received
> using the MPI_Sendrecv function without having to send two messages
> (one just for the data size and the other for the actual data)? I
> guess that a programmer could always provide over sized receive
> buffers that would be sure to fit any possible data length but usually
> people don't have the memory to waste with that solution either.
> Thanks for you insights on this.
> 
> -Darcy

-- 
Dr. David Cronk, Ph.D.                      phone: (865) 974-3735
Research Director                           fax: (865) 974-8296
Innovative Computing Lab                    http://www.cs.utk.edu/~cronk
University of Tennessee, Knoxville
0
Reply David 1/29/2008 6:45:04 PM


1 Replies
155 Views

(page loaded in 0.065 seconds)

Similiar Articles:













7/9/2012 2:17:50 PM


Reply: