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 MPI_Request req;
25
26 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
27
28 if (rank & 0x1) {
29 /* If odd, wait for message */
30 MPI_Recv(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
31 }
32 MPI_Comm_idup(*comm2_ptr, &comm3, &req);
33 MPI_Wait(&req, MPI_STATUS_IGNORE);
34
35 MPI_Barrier(comm3);
36 MPI_Recv(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
37
38 MPI_Comm_free(&comm3);
39 /* Tell the main thread that we're done */
40 MPI_Send(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD);
41
42 return (MTEST_THREAD_RETURN_TYPE) 0;
43 }
44
main(int argc,char * argv[])45 int main(int argc, char *argv[])
46 {
47 int rank, size;
48 int provided;
49 int buffer[1];
50 MPI_Comm comm1, comm2, comm4;
51
52 MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
53
54 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
55 MPI_Comm_size(MPI_COMM_WORLD, &size);
56
57 /* Check that we're multi-threaded */
58 if (provided != MPI_THREAD_MULTIPLE) {
59 if (rank == 0) {
60 printf
61 ("MPI_Init_thread must return MPI_THREAD_MULTIPLE in order for this test to run.\n");
62 fflush(stdout);
63 }
64 return 1;
65 }
66
67 /* The test is this:
68 * The main thread on ODD processors tells the other thread to start
69 * a comm dup(on comm2), then starts a comm dup(on comm1) after a delay.
70 * The main thread on even processors starts a comm dup(on comm1)
71 *
72 * The second thread on ODD processors waits until it gets a message
73 * (from the same process) before starting the comm dup on comm2.
74 */
75
76 /* Create two communicators */
77 MPI_Comm_dup(MPI_COMM_WORLD, &comm1);
78 MPI_Comm_dup(MPI_COMM_WORLD, &comm2);
79
80 /* Start a thread that will perform a dup comm2 */
81 MTest_Start_thread(dup_thread, (void *) &comm2);
82
83 /* If we're odd, send to our new thread and then delay */
84 if (rank & 0x1) {
85 MPI_Ssend(buffer, 0, MPI_INT, rank, 0, MPI_COMM_WORLD);
86 MTestSleep(1);
87 }
88 MPI_Comm_dup(comm1, &comm4);
89
90 /* Tell the threads to exit after we've created our new comm */
91 MPI_Barrier(comm4);
92 MPI_Ssend(buffer, 0, MPI_INT, rank, 1, MPI_COMM_WORLD);
93 MPI_Recv(buffer, 0, MPI_INT, rank, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
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