1 /*
2  * Copyright (C) by Argonne National Laboratory
3  *     See COPYRIGHT in top-level directory
4  */
5 
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "mpitest.h"
10 #include "mpithreadtest.h"
11 
12 /*
13 static char MTEST_Descrip[] = "Creating communications concurrently in different threads";
14 */
15 
16 MTEST_THREAD_RETURN_TYPE dup_thread(void *);
17 
dup_thread(void * p)18 MTEST_THREAD_RETURN_TYPE dup_thread(void *p)
19 {
20     int rank;
21     int buffer[1];
22     MPI_Comm *comm2_ptr = (MPI_Comm *) p;
23     MPI_Comm comm3;
24 
25     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26 
27     if (rank & 0x1) {
28         /* If odd, wait for message */
29         MPI_Recv(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
30     }
31     MPI_Comm_dup(*comm2_ptr, &comm3);
32 
33     MPI_Barrier(comm3);
34     MPI_Recv(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
35 
36     MPI_Comm_free(&comm3);
37     /* Tell the main thread that we're done */
38     MPI_Send(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD);
39 
40     return (MTEST_THREAD_RETURN_TYPE) 0;
41 }
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45     int rank, size;
46     int provided;
47     int buffer[1];
48     MPI_Comm comm1, comm2, comm4;
49 
50     MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
51 
52     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
53     MPI_Comm_size(MPI_COMM_WORLD, &size);
54 
55     /* Check that we're multi-threaded */
56     if (provided != MPI_THREAD_MULTIPLE) {
57         if (rank == 0) {
58             printf
59                 ("MPI_Init_thread must return MPI_THREAD_MULTIPLE in order for this test to run.\n");
60             fflush(stdout);
61         }
62         return 1;
63     }
64 
65     /* The test is this:
66      * The main thread on ODD processors tells the other thread to start
67      * a comm dup(on comm2), then starts a comm dup(on comm1) after a delay.
68      * The main thread on even processors starts a comm dup(on comm1)
69      *
70      * The second thread on ODD processors waits until it gets a message
71      * (from the same process) before starting the comm dup on comm2.
72      */
73 
74     /* Create two communicators */
75     MPI_Comm_dup(MPI_COMM_WORLD, &comm1);
76     MPI_Comm_dup(MPI_COMM_WORLD, &comm2);
77 
78     /* Start a thread that will perform a dup comm2 */
79     MTest_Start_thread(dup_thread, (void *) &comm2);
80 
81     /* If we're odd, send to our new thread and then delay */
82     if (rank & 0x1) {
83         MPI_Ssend(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD);
84         MTestSleep(1);
85     }
86     MPI_Comm_dup(comm1, &comm4);
87 
88     /* Tell the threads to exit after we've created our new comm */
89     MPI_Barrier(comm4);
90     MPI_Ssend(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD);
91     MPI_Recv(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
92 
93     MTest_Join_threads();
94 
95     MPI_Comm_free(&comm4);
96     MPI_Comm_free(&comm1);
97     MPI_Comm_free(&comm2);
98 
99     MTest_Finalize(0);
100     return 0;
101 }
102