Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] ISP tests integration
[simgrid.git] / teshsuite / smpi / isp / umpire / no-error-derived-comms.c
1 /* -*- Mode: C; -*- */
2 /* Creator: Bronis R. de Supinski (bronis@llnl.gov) Thu Nov 30 2000 */
3 /* no-error-derived-comms.c -- do some MPI calls without any errors */
4
5 #include <stdio.h>
6 #include "mpi.h"
7
8
9 #define RUN_COMM_DUP
10 #define RUN_COMM_CREATE
11 #define RUN_INTERCOMM_CREATE
12 #define RUN_CART_CREATE
13 #define RUN_GRAPH_CREATE
14 #define RUN_CART_SUB
15 #define RUN_INTERCOMM_MERGE
16
17
18 #define buf_size 128
19 #define DCOMM_CALL_COUNT  7 /* MPI_Cart_create; MPI_Cart_sub;
20                                MPI_Comm_create; MPI_Comm_dup;
21                                MPI_Comm_split; MPI_Graph_create;
22                                and MPI_Intercomm_merge; store 
23                                MPI_Intercomm_create separately... */
24 #define TWOD     2
25 #define GRAPH_SZ 4
26 #define INTERCOMM_CREATE_TAG 666
27
28
29 int
30 main (int argc, char **argv)
31 {
32   int nprocs = -1;
33   int rank = -1;
34   int i, j;
35   int *granks;
36   char processor_name[128];
37   int namelen = 128;
38   int buf[buf_size];
39   MPI_Status status;
40   MPI_Comm temp;
41   MPI_Comm intercomm = MPI_COMM_NULL;
42   MPI_Comm dcomms[DCOMM_CALL_COUNT];
43   MPI_Group world_group, dgroup;
44   int intersize, dnprocs[DCOMM_CALL_COUNT], drank[DCOMM_CALL_COUNT];
45   int dims[TWOD], periods[TWOD], remain_dims[TWOD];
46   int graph_index[] = { 2, 3, 4, 6 };
47   int graph_edges[] = { 1, 3, 0, 3, 0, 2 };
48
49   /* init */
50   MPI_Init (&argc, &argv);
51   MPI_Comm_size (MPI_COMM_WORLD, &nprocs);
52   MPI_Comm_rank (MPI_COMM_WORLD, &rank);
53   MPI_Get_processor_name (processor_name, &namelen);
54   printf ("(%d) is alive on %s\n", rank, processor_name);
55   fflush (stdout);
56
57   MPI_Barrier (MPI_COMM_WORLD);
58
59   /* probably want number to be higher... */
60   if (nprocs < 4) {
61       printf ("not enough tasks\n");
62   }
63   else {
64     if (DCOMM_CALL_COUNT > 0) {
65 #ifdef RUN_COMM_DUP
66       /* create all of the derived communicators... */
67       /* simplest is created by MPI_Comm_dup... */
68       MPI_Comm_dup (MPI_COMM_WORLD, &dcomms[0]);
69 #else
70       dcomms[0] = MPI_COMM_NULL;
71 #endif
72     }
73
74     if (DCOMM_CALL_COUNT > 1) {
75 #ifdef RUN_COMM_CREATE
76       /* use subset of MPI_COMM_WORLD group for MPI_Comm_create... */
77       MPI_Comm_group (MPI_COMM_WORLD, &world_group);
78       granks = (int *) malloc (sizeof(int) * (nprocs/2));
79       for (i = 0; i < nprocs/2; i++)
80         granks [i] = 2 * i;
81       MPI_Group_incl (world_group, nprocs/2, granks, &dgroup);
82       MPI_Comm_create (MPI_COMM_WORLD, dgroup, &dcomms[1]);
83       MPI_Group_free (&world_group);
84       MPI_Group_free (&dgroup);
85       free (granks);
86 #else
87       dcomms[1] = MPI_COMM_NULL;
88 #endif
89     }
90
91     if (DCOMM_CALL_COUNT > 2) {
92 #ifdef RUN_COMM_SPLIT
93       /* split into thirds with inverted ranks... */
94       MPI_Comm_split (MPI_COMM_WORLD, rank % 3, nprocs - rank, &dcomms[2]);
95 #else
96       dcomms[2] = MPI_COMM_NULL;
97 #endif
98     }
99
100 #ifdef RUN_INTERCOMM_CREATE
101     if ((DCOMM_CALL_COUNT < 2) || (dcomms[2] == MPI_COMM_NULL)) {
102       MPI_Comm_split (MPI_COMM_WORLD, rank % 3, nprocs - rank, &temp);
103     }
104     else {
105       temp = dcomms[2];
106     }
107     if (rank % 3) {
108       MPI_Intercomm_create (temp, 0, MPI_COMM_WORLD, 
109                             (((nprocs % 3) == 2) && ((rank % 3) == 2)) ?
110                             nprocs - 1 : nprocs - (rank % 3) - (nprocs % 3),
111                             INTERCOMM_CREATE_TAG, &intercomm);
112     }
113     if ((DCOMM_CALL_COUNT < 2) || (dcomms[2] == MPI_COMM_NULL)) {
114       MPI_Comm_free (&temp);
115     }
116 #endif
117
118     if (DCOMM_CALL_COUNT > 3) {
119 #ifdef RUN_CART_CREATE
120       /* create a 2 X nprocs/2 torus topology, allow reordering */
121       dims[0] = 2;
122       dims[1] = nprocs/2;
123       periods[0] = periods[1] = 1;
124       MPI_Cart_create (MPI_COMM_WORLD, TWOD, dims, periods, 1, &dcomms[3]);
125 #else
126       dcomms[3] = MPI_COMM_NULL;
127 #endif
128     }
129
130     if (DCOMM_CALL_COUNT > 4) {
131 #ifdef RUN_GRAPH_CREATE
132       /* create the graph on p.268 MPI: The Complete Reference... */
133       MPI_Graph_create (MPI_COMM_WORLD, GRAPH_SZ, 
134                         graph_index, graph_edges, 1, &dcomms[4]);
135 #else
136       dcomms[4] = MPI_COMM_NULL;
137 #endif
138     }
139
140     if (DCOMM_CALL_COUNT > 5) {
141 #ifdef RUN_CART_SUB
142 #ifndef RUN_CART_CREATE
143       /* need to make cartesian communicator temporarily... */
144       /* create a 2 X nprocs/2 torus topology, allow reordering */
145       dims[0] = 2;
146       dims[1] = nprocs/2;
147       periods[0] = periods[1] = 1;
148       MPI_Cart_create (MPI_COMM_WORLD, TWOD, dims, periods, 1, &dcomms[3]);
149 #endif
150       if (dcomms[3] != MPI_COMM_NULL) {
151         /* create 2 1 X nprocs/2 topologies... */
152         remain_dims[0] = 0;
153         remain_dims[1] = 1;
154         MPI_Cart_sub (dcomms[3], remain_dims, &dcomms[5]);
155 #ifndef RUN_CART_CREATE
156         /* free up temporarily created cartesian communicator... */
157         MPI_Comm_free (&dcomms[3]);
158 #endif
159       }
160       else {
161         dcomms[5] = MPI_COMM_NULL;
162       }
163 #else
164       dcomms[5] = MPI_COMM_NULL;
165 #endif
166     }
167
168     if (DCOMM_CALL_COUNT > 6) {
169 #ifdef RUN_INTERCOMM_MERGE
170 #ifndef RUN_INTERCOMM_CREATE
171 #ifndef RUN_COMM_SPLIT
172       /* need to make split communicator temporarily... */
173       /* split into thirds with inverted ranks... */
174       MPI_Comm_split (MPI_COMM_WORLD, rank % 3, nprocs - rank, &dcomms[2]);
175 #endif
176 #endif
177       /* create an intercommunicator and merge it... */
178       if (rank % 3) {
179 #ifndef RUN_INTERCOMM_CREATE
180         MPI_Intercomm_create (dcomms[2], 0, MPI_COMM_WORLD, 
181                               (((nprocs % 3) == 2) && ((rank % 3) == 2)) ?
182                               nprocs - 1 : nprocs - (rank % 3) - (nprocs % 3),
183                               INTERCOMM_CREATE_TAG, &intercomm);
184 #endif
185
186         MPI_Intercomm_merge (intercomm, ((rank % 3) == 1), &dcomms[6]);
187
188 #ifndef RUN_INTERCOMM_CREATE
189         /* we are done with intercomm... */
190         MPI_Comm_free (&intercomm);
191 #endif
192       }
193       else {
194         dcomms[6] = MPI_COMM_NULL;
195       }    
196 #ifndef RUN_INTERCOMM_CREATE
197 #ifndef RUN_COMM_SPLIT
198       if (dcomms[2] != MPI_COMM_NULL)
199         /* free up temporarily created split communicator... */
200         MPI_Comm_free (&dcomms[2]);
201 #endif
202 #endif
203 #else
204       dcomms[6] = MPI_COMM_NULL;
205 #endif
206     }
207
208     /* get all of the sizes and ranks... */
209     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
210       if (dcomms[i] != MPI_COMM_NULL) {
211         MPI_Comm_size (dcomms[i], &dnprocs[i]);
212         MPI_Comm_rank (dcomms[i], &drank[i]);
213       }
214       else {
215         dnprocs[i] = 0;
216         drank[i] = -1;
217       }
218     }
219
220 #ifdef RUN_INTERCOMM_CREATE
221     /* get the intercomm remote size... */
222     if (rank % 3) {
223       MPI_Comm_remote_size (intercomm, &intersize);
224     }
225 #endif
226
227     /* do some point to point on all of the dcomms... */
228     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
229       if (dnprocs[i] > 1) {
230         if (drank[i] == 0) {
231           for (j = 1; j < dnprocs[i]; j++) {
232             MPI_Recv (buf, buf_size, MPI_INT, j, 0, dcomms[i], &status);
233           }
234         }
235         else {
236           memset (buf, 1, buf_size);
237
238           MPI_Send (buf, buf_size, MPI_INT, 0, 0, dcomms[i]);
239         }
240       }
241     }
242
243 #ifdef RUN_INTERCOMM_CREATE
244     /* do some point to point on the intercomm... */
245     if ((rank % 3) == 1) {
246       for (j = 0; j < intersize; j++) {
247         MPI_Recv (buf, buf_size, MPI_INT, j, 0, intercomm, &status);
248       }
249     }
250     else if ((rank % 3) == 2) {
251       for (j = 0; j < intersize; j++) {
252         memset (buf, 1, buf_size);
253
254         MPI_Send (buf, buf_size, MPI_INT, j, 0, intercomm);
255       }
256     }
257 #endif
258
259     /* do a bcast on all of the dcomms... */
260     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
261       /* IBM's implementation gets error with comm over MPI_COMM_NULL... */
262       if (dnprocs[i] > 0)
263         MPI_Bcast (buf, buf_size, MPI_INT, 0, dcomms[i]);
264     }
265
266     /* use any source receives... */
267     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
268       if (dnprocs[i] > 1) {
269         if (drank[i] == 0) {
270           for (j = 1; j < dnprocs[i]; j++) {
271             MPI_Recv (buf, buf_size, MPI_INT, 
272                       MPI_ANY_SOURCE, 0, dcomms[i], &status);
273           }
274         }
275         else {
276           memset (buf, 1, buf_size);
277
278           MPI_Send (buf, buf_size, MPI_INT, 0, 0, dcomms[i]);
279         }
280       }
281     }
282
283 #ifdef RUN_INTERCOMM_CREATE
284     /* do any source receives on the intercomm... */
285     if ((rank % 3) == 1) {
286       for (j = 0; j < intersize; j++) {
287         MPI_Recv (buf, buf_size, MPI_INT, 
288                   MPI_ANY_SOURCE, 0, intercomm, &status);
289       }
290     }
291     else if ((rank % 3) == 2) {
292       for (j = 0; j < intersize; j++) {
293         memset (buf, 1, buf_size);
294
295         MPI_Send (buf, buf_size, MPI_INT, j, 0, intercomm);
296       }
297     }
298 #endif
299
300     /* do a barrier on all of the dcomms... */
301     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
302       /* IBM's implementation gets with communication over MPI_COMM_NULL... */
303       if (dnprocs[i] > 0)
304         MPI_Barrier (dcomms[i]);
305     }
306
307     /* free all of the derived communicators... */
308     for (i = 0; i < DCOMM_CALL_COUNT; i++) {
309       /* freeing MPI_COMM_NULL is explicitly defined as erroneous... */
310       if (dnprocs[i] > 0)
311         MPI_Comm_free (&dcomms[i]);
312     }
313
314 #ifdef RUN_INTERCOMM_CREATE
315     if (rank % 3)
316       /* we are done with intercomm... */
317       MPI_Comm_free (&intercomm);
318 #endif
319   }
320
321   MPI_Barrier (MPI_COMM_WORLD);
322
323   MPI_Finalize ();
324   printf ("(%d) Finished normally\n", rank);
325 }
326
327 /* EOF */