#include #include #include #define NUM_THREADS 2 #define MPI_MSG_TAG_NEW_CONN 0 #define MPI_MSG_TAG_SHUTDOWN 1 char port_name[] = "test_port"; void *thread1(void *threadid) { long tid; tid = (long) threadid; printf("thread %ld\n", tid); do { MPI_Comm newComm; MPI_Comm_accept(port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF, &newComm); printf("Accepted a connection\n"); int buf = 0; MPI_Status status; printf("start MPI_recv\n"); MPI_Recv(&buf, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, newComm, &status); printf("end MPI_recv\n"); if (status.MPI_TAG == MPI_MSG_TAG_SHUTDOWN) { printf("Shutdown\n"); printf("Disconnect\n"); break; } else printf("Unmatched Receive\n"); } while (1); pthread_exit(NULL); } void *thread2(void *threadid) { long tid; tid = (long) threadid; printf("thread %ld\n", tid); MPI_Comm newComm; MPI_Comm_connect(port_name, MPI_INFO_NULL, 0, MPI_COMM_SELF, &newComm); printf("Connect to Self\n"); int val = 0; MPI_Request req; printf("start MPI_Send\n"); MPI_Send(&val, 1, MPI_INT, 0, MPI_MSG_TAG_SHUTDOWN, newComm); printf("Successful\n"); printf("Complete\n"); pthread_exit(NULL); } int main(int argc, char *argv[]) { int provided; MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided); MPI_Open_port(MPI_INFO_NULL, port_name); pthread_t threads[NUM_THREADS]; pthread_create(&threads[0], NULL, thread1, (void *) 0); pthread_create(&threads[1], NULL, thread2, (void *) 1); pthread_exit(NULL); MPI_Finalize(); }