Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
intercept correctly calls with unitialized communicators
[simgrid.git] / src / smpi / smpi_comm.c
1 /* Copyright (c) 2010-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8
9 #include "private.h"
10 #include "smpi_mpi_dt_private.h"
11 #include "limits.h"
12 #include "simix/smx_private.h"
13 #include "xbt/replay.h"
14 #include "colls/colls.h"
15
16 extern int is_replay_active ;
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
19                                 "Logging specific to SMPI (comm)");
20
21
22
23 /* Support for cartesian topology was added, but there are 2 other types of
24  * topology, graph et dist graph. In order to support them, we have to add a
25  * field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
26
27 typedef struct s_smpi_mpi_communicator {
28   MPI_Group group;
29   MPIR_Topo_type topoType; 
30   MPI_Topology topo; // to be replaced by an union
31   int refcount;
32   MPI_Comm leaders_comm;//inter-node communicator
33   MPI_Comm intra_comm;//intra-node communicator . For MPI_COMM_WORLD this can't be used, as var is global.
34   //use an intracomm stored in the process data instead
35   int* leaders_map; //who is the leader of each process
36   int is_uniform;
37   int* non_uniform_map; //set if smp nodes have a different number of processes allocated
38   int is_blocked;// are ranks allocated on the same smp node contiguous ?
39 } s_smpi_mpi_communicator_t;
40
41 static int smpi_compare_rankmap(const void *a, const void *b)
42 {
43   const int* x = (const int*)a;
44   const int* y = (const int*)b;
45
46   if (x[1] < y[1]) {
47     return -1;
48   }
49   if (x[1] == y[1]) {
50     if (x[0] < y[0]) {
51       return -1;
52     }
53     if (x[0] == y[0]) {
54       return 0;
55     }
56     return 1;
57   }
58   return 1;
59 }
60
61 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
62 {
63   MPI_Comm comm;
64
65   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
66   comm->group = group;
67   smpi_group_use(comm->group);
68   comm->refcount=1;
69   comm->topoType = -1;
70   comm->topo = topo;
71   comm->intra_comm = MPI_COMM_NULL;
72   comm->leaders_comm = MPI_COMM_NULL;
73   comm->is_uniform=1;
74   comm->non_uniform_map = NULL;
75   comm->leaders_map = NULL;
76   comm->is_blocked=0;
77   return comm;
78 }
79
80 void smpi_comm_destroy(MPI_Comm comm)
81 {
82   if (comm == MPI_COMM_UNINITIALIZED)
83     comm = smpi_process_comm_world();
84   smpi_group_unuse(comm->group);
85   smpi_topo_destroy(comm->topo); // there's no use count on topos
86   smpi_comm_unuse(comm);
87 }
88
89 MPI_Group smpi_comm_group(MPI_Comm comm)
90 {
91   if (comm == MPI_COMM_UNINITIALIZED)
92     comm = smpi_process_comm_world();
93
94   return comm->group;
95 }
96
97 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
98   if (comm != MPI_COMM_NULL)
99     return comm->topo;
100   return NULL;
101 }
102
103 int smpi_comm_size(MPI_Comm comm)
104 {
105   if (comm == MPI_COMM_UNINITIALIZED)
106     comm = smpi_process_comm_world();
107
108   return smpi_group_size(smpi_comm_group(comm));
109 }
110
111 int smpi_comm_rank(MPI_Comm comm)
112 {
113   if (comm == MPI_COMM_UNINITIALIZED)
114     comm = smpi_process_comm_world();
115   return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
116 }
117
118 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
119 {
120   if (comm == MPI_COMM_UNINITIALIZED)
121     comm = smpi_process_comm_world();
122   if(comm == MPI_COMM_WORLD) {
123     strcpy(name, "WORLD");
124     *len = 5;
125   } else {
126     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
127   }
128 }
129
130 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
131   if (comm == MPI_COMM_UNINITIALIZED)
132     comm = smpi_process_comm_world();
133   comm->leaders_comm=leaders;
134 }
135
136 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
137   if (comm == MPI_COMM_UNINITIALIZED)
138     comm = smpi_process_comm_world();
139   comm->intra_comm=leaders;
140 }
141
142 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
143   if (comm == MPI_COMM_UNINITIALIZED)
144     comm = smpi_process_comm_world();
145   return comm->non_uniform_map;
146 }
147
148 int* smpi_comm_get_leaders_map(MPI_Comm comm){
149   if (comm == MPI_COMM_UNINITIALIZED)
150     comm = smpi_process_comm_world();
151   return comm->leaders_map;
152 }
153
154 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
155   if (comm == MPI_COMM_UNINITIALIZED)
156     comm = smpi_process_comm_world();
157   return comm->leaders_comm;
158 }
159
160 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
161   if (comm == MPI_COMM_UNINITIALIZED)
162     comm = smpi_process_comm_world();
163   if(comm==MPI_COMM_WORLD) return smpi_process_get_comm_intra();
164   else return comm->intra_comm;
165 }
166
167 int smpi_comm_is_uniform(MPI_Comm comm){
168   if (comm == MPI_COMM_UNINITIALIZED)
169     comm = smpi_process_comm_world();
170   return comm->is_uniform;
171 }
172
173 int smpi_comm_is_blocked(MPI_Comm comm){
174   if (comm == MPI_COMM_UNINITIALIZED)
175     comm = smpi_process_comm_world();
176   return comm->is_blocked;
177 }
178
179 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
180 {
181   if (comm == MPI_COMM_UNINITIALIZED)
182     comm = smpi_process_comm_world();
183   int system_tag = 123;
184   int index, rank, size, i, j, count, reqs;
185   int* sendbuf;
186   int* recvbuf;
187   int* rankmap;
188   MPI_Group group, group_root, group_out;
189   MPI_Request* requests;
190
191   group_root = group_out = NULL;
192   group = smpi_comm_group(comm);
193   rank = smpi_comm_rank(comm);
194   size = smpi_comm_size(comm);
195   /* Gather all colors and keys on rank 0 */
196   sendbuf = xbt_new(int, 2);
197   sendbuf[0] = color;
198   sendbuf[1] = key;
199   if(rank == 0) {
200     recvbuf = xbt_new(int, 2 * size);
201   } else {
202     recvbuf = NULL;
203   }
204   smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
205   xbt_free(sendbuf);
206   /* Do the actual job */
207   if(rank == 0) {
208     rankmap = xbt_new(int, 2 * size);
209     for(i = 0; i < size; i++) {
210       if(recvbuf[2 * i] == MPI_UNDEFINED) {
211         continue;
212       }
213       count = 0;
214       for(j = i + 1; j < size; j++)  {
215         if(recvbuf[2 * i] == recvbuf[2 * j]) {
216           recvbuf[2 * j] = MPI_UNDEFINED;
217           rankmap[2 * count] = j;
218           rankmap[2 * count + 1] = recvbuf[2 * j + 1];
219           count++;
220         }
221       }
222       /* Add self in the group */
223       recvbuf[2 * i] = MPI_UNDEFINED;
224       rankmap[2 * count] = i;
225       rankmap[2 * count + 1] = recvbuf[2 * i + 1];
226       count++;
227       qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
228       group_out = smpi_group_new(count);
229       if(i == 0) {
230         group_root = group_out; /* Save root's group */
231       }
232       for(j = 0; j < count; j++) {
233         //increment refcounter in order to avoid freeing the group too quick before copy
234         index = smpi_group_index(group, rankmap[2 * j]);
235         smpi_group_set_mapping(group_out, index, j);
236       }
237       requests = xbt_new(MPI_Request, count);
238       reqs = 0;
239       for(j = 0; j < count; j++) {
240         if(rankmap[2 * j] != 0) {
241           requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
242           reqs++;
243         }
244       }
245       smpi_mpi_startall(reqs, requests);
246       smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
247       xbt_free(requests);
248     }
249     xbt_free(recvbuf);
250     group_out = group_root; /* exit with root's group */
251   } else {
252     if(color != MPI_UNDEFINED) {
253       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
254       if(group_out){
255         group_out=smpi_group_copy(group_out);
256       }
257     } /* otherwise, exit with group_out == NULL */
258   }
259   return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
260 }
261
262 void smpi_comm_use(MPI_Comm comm){
263   if (comm == MPI_COMM_UNINITIALIZED)
264     comm = smpi_process_comm_world();
265   comm->refcount++;
266 }
267
268 void smpi_comm_unuse(MPI_Comm comm){
269   if (comm == MPI_COMM_UNINITIALIZED)
270     comm = smpi_process_comm_world();
271   comm->refcount--;
272   if(comm->refcount==0){
273     if(comm->intra_comm != MPI_COMM_NULL)
274       smpi_comm_unuse(comm->intra_comm);
275     if(comm->leaders_comm != MPI_COMM_NULL)
276       smpi_comm_unuse(comm->leaders_comm);
277     if(comm->non_uniform_map !=NULL)
278       xbt_free(comm->non_uniform_map);
279     if(comm->leaders_map !=NULL)
280       xbt_free(comm->leaders_map);
281     xbt_free(comm);
282   }
283 }
284
285 static int
286 compare_ints (const void *a, const void *b)
287 {
288   const int *da = (const int *) a;
289   const int *db = (const int *) b;
290
291   return (*da > *db) - (*da < *db);
292 }
293
294 void smpi_comm_init_smp(MPI_Comm comm){
295   int leader = -1;
296
297   if (comm == MPI_COMM_UNINITIALIZED)
298     comm = smpi_process_comm_world();
299
300   int comm_size =smpi_comm_size(comm);
301   
302   // If we are in replay - perform an ugly hack  
303   // say to SimGrid that we are not in replay for a while, because we need 
304   // the buffers to be copied for the following calls
305   int replaying = 0; //cache data to set it back again after
306   if(_xbt_replay_is_active()){
307     replaying = 1;
308     is_replay_active = 0 ;
309   }
310
311   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
312      smpi_switch_data_segment(smpi_process_index());
313    }
314   //identify neighbours in comm
315   //get the indexes of all processes sharing the same simix host
316   xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
317   int intra_comm_size = 0;
318   //only one process/node, disable SMP support and return
319 //  if(intra_comm_size==1){
320 //      smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
321 //      //smpi_comm_set_leaders_comm(comm, comm);
322 //      smpi_process_set_comm_intra(MPI_COMM_SELF);
323 //      return;
324 //  }
325
326
327   int i =0;
328   int min_index=INT_MAX;//the minimum index will be the leader
329   msg_process_t process = NULL;
330   xbt_swag_foreach(process, process_list) {
331     //is_in_comm=0;
332     int index = SIMIX_process_get_PID(process) -1;
333
334     if(smpi_group_rank(smpi_comm_group(comm),  index)!=MPI_UNDEFINED){
335         intra_comm_size++;
336       //the process is in the comm
337       if(index < min_index)
338         min_index=index;
339       i++;
340     }
341   }
342   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
343   MPI_Group group_intra = smpi_group_new(intra_comm_size);
344   i=0;
345   process = NULL;
346   xbt_swag_foreach(process, process_list) {
347     //is_in_comm=0;
348     int index = SIMIX_process_get_PID(process) -1;
349     if(smpi_group_rank(smpi_comm_group(comm),  index)!=MPI_UNDEFINED){
350       smpi_group_set_mapping(group_intra, index, i);
351       i++;
352     }
353   }
354
355
356   MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
357   //MPI_Comm shmem_comm = smpi_process_comm_intra();
358   //int intra_rank = smpi_comm_rank(shmem_comm);
359
360
361   //if(smpi_process_index()==min_index)
362   leader=min_index;
363
364   int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
365   int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
366   for(i=0; i<comm_size; i++){
367       leader_list[i]=-1;
368   }
369
370   smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
371
372   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
373      smpi_switch_data_segment(smpi_process_index());
374    }
375    
376   if(!comm->leaders_map){
377     comm->leaders_map= leaders_map;
378   }else{
379     xbt_free(leaders_map);
380   }
381   int j=0;
382   int leader_group_size = 0;
383   for(i=0; i<comm_size; i++){
384       int already_done=0;
385       for(j=0;j<leader_group_size; j++){
386         if(comm->leaders_map[i]==leader_list[j]){
387             already_done=1;
388         }
389       }
390       if(!already_done){
391         leader_list[leader_group_size]=comm->leaders_map[i];
392         leader_group_size++;
393       }
394   }
395   qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
396
397   MPI_Group leaders_group = smpi_group_new(leader_group_size);
398
399
400   MPI_Comm leader_comm = MPI_COMM_NULL;
401   if(comm!=MPI_COMM_WORLD){
402     //create leader_communicator
403     for (i=0; i< leader_group_size;i++)
404       smpi_group_set_mapping(leaders_group, leader_list[i], i);
405     leader_comm = smpi_comm_new(leaders_group, NULL);
406     smpi_comm_set_leaders_comm(comm, leader_comm);
407     smpi_comm_set_intra_comm(comm, comm_intra);
408
409     //create intracommunicator
410    // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
411   }else{
412     for (i=0; i< leader_group_size;i++)
413       smpi_group_set_mapping(leaders_group, leader_list[i], i);
414
415     leader_comm = smpi_comm_new(leaders_group, NULL);
416     if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL)
417       smpi_comm_set_leaders_comm(comm, leader_comm);
418     smpi_process_set_comm_intra(comm_intra);
419   }
420
421   int is_uniform = 1;
422
423   // Are the nodes uniform ? = same number of process/node
424   int my_local_size=smpi_comm_size(comm_intra);
425   if(smpi_comm_rank(comm_intra)==0) {
426     int* non_uniform_map = xbt_malloc(sizeof(int)*leader_group_size);
427     smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
428         non_uniform_map, 1, MPI_INT, leader_comm);
429     for(i=0; i < leader_group_size; i++) {
430       if(non_uniform_map[0] != non_uniform_map[i]) {
431         is_uniform = 0;
432         break;
433       }
434     }
435     if(!is_uniform && smpi_comm_is_uniform(comm)){
436         comm->non_uniform_map= non_uniform_map;
437     }else{
438         xbt_free(non_uniform_map);
439     }
440     comm->is_uniform=is_uniform;
441   }
442   smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
443
444   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
445      smpi_switch_data_segment(smpi_process_index());
446    }
447   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
448   int is_blocked=1;
449   int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
450     for (i=1; i<my_local_size; i++){
451       int this=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
452       if(this!=prev+1){
453         is_blocked=0;
454         break;
455       }
456       prev = this;
457   }
458
459   int global_blocked;
460   smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1,
461             MPI_INT, MPI_LAND, comm);
462
463   if(comm==MPI_COMM_WORLD){
464     if(smpi_comm_rank(comm)==0){
465         comm->is_blocked=global_blocked;
466     }
467   }else{
468     comm->is_blocked=global_blocked;
469   }
470   xbt_free(leader_list);
471   
472   if(replaying==1)
473     is_replay_active = 1; 
474 }
475