Stream Security with MPI_Probe

I use MPI_Probeto send messages dynamically (where the recipient does not know the size of the message to be sent). My code looks something like this:

if (world_rank == 0) {
    int *buffer = ...
    int bufferSize = ...
    MPI_Send(buffer, buffersize, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
    MPI_Status status;
    MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
    int count = -1;
    MPI_Get_count(&status, MPI_INT, &count);
    int* buffer = (int*)malloc(sizeof(int) * count);
    MPI_Recv(buffer, count, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}

If I run this code in multiple threads, there is a chance that it MPI_Probeis called in one thread and MPI_recvcalled in another thread due to the scheduler interleaving the threads. Essentially, the code above is thread safe.

+4
source share
1 answer

, MPI . , , MPI, MPI_Init_thread MPI_Init.

, MPI , - - , .

MPI_Probe MPI_Recv , MPI-2: http://htor.inf.ethz.ch/publications/img/gregor-any_size-mpi3.pdf

. MPI-3 MPI_Mprobe MPI_MRecv, / . :

MPI-2 ( /):

int number_amount;
if (world_rank == 0) {
    int *buffer = ...
    int bufferSize = ...
    MPI_Send(buffer, buffersize, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
    MPI_Status status;
    int count = -1;
    /* aquire mutex/lock */
    MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
    MPI_Get_count(&status, MPI_INT, &count);
    int* buffer = (int*)malloc(sizeof(int) * count);
    MPI_Recv(buffer, count, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    /* release mutex/lock */
}

MPI-3:

int number_amount;
if (world_rank == 0) {
    int *buffer = ...
    int bufferSize = ...
    MPI_Send(buffer, buffersize, MPI_INT, 1, 0, MPI_COMM_WORLD);
} else if (world_rank == 1) {
    MPI_Status status;
    MPI_Message msg;
    int count = -1;
    MPI_Mprobe(0, 0, MPI_COMM_WORLD, &msg, &status);
    MPI_Get_count(&status, MPI_INT, &count);
    int* buffer = (int*)malloc(sizeof(int) * count);
    MPI_Mrecv(buffer, count, MPI_INT, &msg, MPI_STATUS_IGNORE);
}
+4

All Articles