[MPICH] MPI_Probe and thread

Wei-keng Liao wkliao at ece.northwestern.edu
Fri Jul 28 00:24:07 CDT 2006


I am testing if an MPI node can send message to itself, using MPI_Send and 
MPI_Probe on 2 different threads. Below is the MPI program that creates a 
2nd thread, then sends an integer to the thread. The 2nd thread calls 
MPI_Probe, receives the message, and exits.

If MPI_Probe happens before MPI_Send, I found that the program will hang 
due to the 2nd thread is blocked in MPI_Probe. That is, MPI_Probe is busy 
waiting and will not release/yield to the main thread.

If I add a pthread_yield() or sleep(2) before MPI_Probe to ensure MPI_Send 
occurs before MPI_Probe, the 2nd thread's probe will get the message and 
exit successfully.

Is there a limitation on using MPI_Probe in multi-threading environment?
I am using MPICH-2.1.0.3 which is configured with multi-threading enabled.

Wei-keng

---------------------
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <pthread.h>

int SELF;

/*----< thread_func() >---------------------------------------------*/
void* thread_func(void *args) {
     MPI_Status status;
     int        src = -1, msg;

/*
     pthread_yield();
     sleep(1);
*/
     printf("P%d (thread): thread started\n", SELF);
     MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
     src = status.MPI_SOURCE;
     printf("P%d (thread): MPI_Probe() MPI_SOURCE = %d\n", SELF, src);
     MPI_Recv(&msg, 1, MPI_INT, src, 0, MPI_COMM_WORLD, &status);
}

/*----< main() >----------------------------------------------------*/
int main(int argc, char **argv) {
     int       i, msg;
     pthread_t thread_id;

     MPI_Init(&argc, &argv);
     MPI_Comm_rank(MPI_COMM_WORLD, &SELF);

     pthread_create(&thread_id, NULL, thread_func, NULL);

     MPI_Ssend(&msg, 1, MPI_INT, SELF, 0, MPI_COMM_WORLD);

     pthread_join(thread_id, NULL);
     printf("P%d ( main ): 2nd thread joined ---- exit\n", SELF);

     MPI_Finalize();
     return 0;
}




More information about the mpich-discuss mailing list