Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] ISP tests integration
[simgrid.git] / teshsuite / smpi / isp / umpire / intercomm_merge-deadlock.c
1 /* -*- Mode: C; -*- */
2 /* Creator: Bronis R. de Supinski (bronis@llnl.gov) Fri Mar  17 2000 */
3 /* no-error.c -- do some MPI calls without any errors */
4
5 #include <stdio.h>
6 #include "mpi.h"
7
8 #define buf_size 128
9
10 #define INTERCOMM_CREATE_TAG 666
11
12 int
13 main (int argc, char **argv)
14 {
15   int nprocs = -1;
16   int rank = -1;
17   char processor_name[128];
18   int namelen = 128;
19   int buf0[buf_size];
20   int buf1[buf_size];
21   MPI_Status status;
22   MPI_Comm temp, intercomm, intercomm2, comm, comm2;
23   int trank, tnprocs;
24   int drank, dnprocs, rleader;
25
26   /* init */
27   MPI_Init (&argc, &argv);
28   MPI_Comm_size (MPI_COMM_WORLD, &nprocs);
29   MPI_Comm_rank (MPI_COMM_WORLD, &rank);
30   MPI_Get_processor_name (processor_name, &namelen);
31   printf ("(%d) is alive on %s\n", rank, processor_name);
32   fflush (stdout);
33
34   MPI_Barrier (MPI_COMM_WORLD);
35
36   if (nprocs < 4) {
37     printf ("not enough tasks\n");
38   }
39   else {
40     /* need to make split communicator temporarily... */
41     MPI_Comm_split (MPI_COMM_WORLD, rank % 2, nprocs - rank, &temp);
42
43     if (temp != MPI_COMM_NULL) {
44       /* create an intercommunicator temporarily so can merge it... */
45       rleader = ((rank + nprocs) % 2) ?  nprocs - 2 : nprocs - 1;
46       MPI_Intercomm_create (temp, 0, MPI_COMM_WORLD, rleader,
47                             INTERCOMM_CREATE_TAG, &intercomm);
48       MPI_Comm_free (&temp);
49       
50       if (intercomm != MPI_COMM_NULL) {
51         /* need to make a different split communicator temporarily... */
52         MPI_Comm_split (MPI_COMM_WORLD, 
53                         rank < nprocs/2, nprocs - rank, &temp);
54
55         if (temp != MPI_COMM_NULL) {
56           /* create another intercommunicator temporarily to merge... */
57           rleader = (rank < nprocs/2) ?  nprocs - 1 : nprocs/2 - 1;
58           MPI_Intercomm_create (temp, 0, MPI_COMM_WORLD, rleader,
59                                 INTERCOMM_CREATE_TAG, &intercomm2);
60           MPI_Comm_free (&temp);
61       
62           if (intercomm2 != MPI_COMM_NULL) {
63             if (rank < nprocs/2) {
64               MPI_Intercomm_merge (intercomm2, rank < nprocs/2, &comm2);
65               MPI_Comm_free (&intercomm2);
66               MPI_Intercomm_merge (intercomm, rank % 2, &comm);
67               MPI_Comm_free (&intercomm);
68             }
69             else {
70               MPI_Intercomm_merge (intercomm, rank % 2, &comm);
71               MPI_Comm_free (&intercomm);
72               MPI_Intercomm_merge (intercomm2, rank < nprocs/2, &comm2);
73               MPI_Comm_free (&intercomm2);
74             }
75
76             if ((comm != MPI_COMM_NULL) && (comm2 != MPI_COMM_NULL)) {
77               MPI_Comm_size (comm, &dnprocs);
78               MPI_Comm_rank (comm, &drank);
79
80               if (dnprocs > 1) {
81                 if (drank == 1) {
82                   memset (buf1, 1, buf_size);
83
84                   MPI_Recv (buf0, buf_size, MPI_INT, 0, 0, comm, &status);
85
86                   MPI_Send (buf1, buf_size, MPI_INT, 0, 0, comm);
87                 }
88                 else if (drank == 0) {
89                   memset (buf0, 0, buf_size);
90         
91                   MPI_Send (buf0, buf_size, MPI_INT, 1, 0, comm);
92             
93                   MPI_Recv (buf1, buf_size, MPI_INT, 1, 0, comm, &status);
94                 }
95               }
96               else {
97                 printf ("(%d) comm too small\n", rank);
98               }
99
100               MPI_Comm_size (comm2, &dnprocs);
101               MPI_Comm_rank (comm2, &drank);
102
103               if (dnprocs > 1) {
104                 if (drank == 1) {
105                   memset (buf1, 1, buf_size);
106
107                   MPI_Recv (buf0, buf_size, MPI_INT, 0, 0, comm2, &status);
108
109                   MPI_Send (buf1, buf_size, MPI_INT, 0, 0, comm2);
110                 }
111                 else if (drank == 0) {
112                   memset (buf0, 0, buf_size);
113         
114                   MPI_Send (buf0, buf_size, MPI_INT, 1, 0, comm2);
115             
116                   MPI_Recv (buf1, buf_size, MPI_INT, 1, 0, comm2, &status);
117                 }
118               }
119               else {
120                 printf ("(%d) comm2 too small\n", rank);
121               }
122             }
123             else {
124               printf ("(%d) Got MPI_COMM_NULL\n", rank);
125             }
126           }
127           else {
128             printf ("(%d) Got MPI_COMM_NULL for intercomm2\n", rank);
129           }
130         }
131         else {
132           printf ("(%d) Second MPI_Comm_split got MPI_COMM_NULL\n", rank);
133         }
134       }
135       else {
136         printf ("(%d) Got MPI_COMM_NULL for intercomm1\n", rank);
137       }
138     }
139     else {
140       printf ("(%d) First MPI_Comm_split got MPI_COMM_NULL\n", rank);
141     }
142   }
143
144   MPI_Barrier (MPI_COMM_WORLD);
145
146   MPI_Finalize ();
147   printf ("(%d) Finished normally\n", rank);
148 }
149
150 /* EOF */