Logo AND Algorithmique Numérique Distribuée

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