On Thu, Mar 21, 2013 at 8:29 PM, Pavan Balaji <balaji@mcs.anl.gov> wrote:
You'll still need to create a datatype for MPI_Pack.  This is the
structure you mentioned:

struct A
{
    int size;
    int *lptr;
};

I'm assuming you want to send the size and the data associated with lptr
in a single message.  In this case, the only approaches I can think of are:

1. Create a datatype every time you want to send this information and
free it once you are done.

2. Send two messages, one with the size parameter and the second with
the data pointed to by lptr.

Someone else might have a better way to do this.

If the receiver does not know the size in advance (or at least an upper bound), then they would have to allocate before receiving anyway. If you have to do that, you may as well either send two messages or use MPI_Probe/MPI_Iprobe and allocate once you know the message size.

If the receiver knows an upper bound in advance, just MPI_Recv that upper bound and use MPI_Get_count to find out how much you received.

If you have many of these, I would send all the sizes in one message and all the data in another message, unless the receiver knows the sizes in advance.

Once you know all the sizes, you could make an MPI_Type_create_hindexed and send with respect to MPI_BOTTOM to send all those separate arrays without explicit packing. I would only bother with that in a memory-constrained environment or after exhausting the simpler options.