Site Tools


probing-mpi

**This is an old revision of the document!**

Table of Contents

Probing for messages (MPI)

A standard recv() on a socket requires a pre-allocated buffer. If the incoming message is larger than the buffer, data gets truncated or the call fails. The POSIX escape hatch is MSG_PEEK: pass it as a flag and recv() fills your buffer but leaves the data in the socket queue, letting you inspect the size before committing to a real receive. ioctl(fd, FIONREAD, &n) goes further and tells you exactly how many bytes are waiting. MPI's MPI_Probe does the same thing: it matches an incoming message and fills an MPI_Status struct with metadata — including the element count — without consuming the message from the queue.

MPI_Status status;
MPI_Probe(src, tag, MPI_COMM_WORLD, &status);
 
int count;
MPI_Get_count(&status, MPI_DOUBLE, &count);
double *buf = malloc(count * sizeof(double));
 
MPI_Recv(buf, count, MPI_DOUBLE, src, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

MPI_Iprobe is the non-blocking variant: it sets a flag and returns immediately whether or not a matching message is available. Both functions accept MPI_ANY_SOURCE and MPI_ANY_TAG. When wildcards are used, the source rank from the MPI_Status must be passed verbatim to the following MPI_Recv to ensure the same message is consumed, not a different one that arrived in between.

probing-mpi.1781169013.txt.gz · Last modified: by 127.0.0.1