Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_comm.cpp
1 /* Copyright (c) 2010-2015. 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 #include <limits.h>
9
10 #include <xbt/dict.h>
11 #include <xbt/ex.h>
12 #include <xbt/ex.hpp>
13
14 #include <simgrid/s4u/host.hpp>
15
16 #include "private.h"
17 #include "smpi_mpi_dt_private.h"
18 #include "src/simix/smx_private.h"
19 #include "colls/colls.h"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
22
23 xbt_dict_t smpi_comm_keyvals = nullptr;
24 int comm_keyval_id = 0;//avoid collisions
25
26 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
27  * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
28
29 static int smpi_compare_rankmap(const void *a, const void *b)
30 {
31   const int* x = static_cast<const int*>(a);
32   const int* y = static_cast<const int*>(b);
33
34   if (x[1] < y[1]) {
35     return -1;
36   }
37   if (x[1] == y[1]) {
38     if (x[0] < y[0]) {
39       return -1;
40     }
41     if (x[0] == y[0]) {
42       return 0;
43     }
44     return 1;
45   }
46   return 1;
47 }
48
49 namespace simgrid{
50 namespace SMPI{
51
52 Comm::Comm(MPI_Group group, MPI_Topology topo)
53 {
54   m_group = group;
55   m_refcount=1;
56   m_topoType = MPI_INVALID_TOPO;
57   m_topo = topo;
58   m_intra_comm = MPI_COMM_NULL;
59   m_leaders_comm = MPI_COMM_NULL;
60   m_is_uniform=1;
61   m_non_uniform_map = nullptr;
62   m_leaders_map = nullptr;
63   m_is_blocked=0;
64   m_attributes=nullptr;
65 }
66
67 void Comm::destroy()
68 {
69   if (this == MPI_COMM_UNINITIALIZED)
70     return smpi_process_comm_world()->destroy();
71   smpi_topo_destroy(m_topo); // there's no use count on topos
72   this->unuse();
73 }
74
75 int Comm::dup(MPI_Comm* newcomm){
76   if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
77      smpi_switch_data_segment(smpi_process_index());
78    }
79   MPI_Group cp = new simgrid::SMPI::Group(this->group());
80   (*newcomm) = new simgrid::SMPI::Comm(cp, this->topo());
81   int ret = MPI_SUCCESS;
82
83   if(m_attributes !=nullptr){
84     (*newcomm)->m_attributes   = xbt_dict_new_homogeneous(nullptr);
85     xbt_dict_cursor_t cursor = nullptr;
86     char* key;
87     int flag;
88     void* value_in;
89     void* value_out;
90     xbt_dict_foreach (m_attributes, cursor, key, value_in) {
91       smpi_comm_key_elem elem =
92           static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
93       if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
94         ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
95         if (ret != MPI_SUCCESS) {
96           (*newcomm)->destroy();
97           *newcomm = MPI_COMM_NULL;
98           xbt_dict_cursor_free(&cursor);
99           return ret;
100         }
101         if (flag)
102           xbt_dict_set_ext((*newcomm)->m_attributes, key, sizeof(int), value_out, nullptr);
103       }
104       }
105     }
106   return ret;
107 }
108
109 MPI_Group Comm::group()
110 {
111   if (this == MPI_COMM_UNINITIALIZED)
112     return smpi_process_comm_world()->group();
113   return m_group;
114 }
115
116 MPI_Topology Comm::topo() {
117   return m_topo;
118 }
119
120 int Comm::size()
121 {
122   if (this == MPI_COMM_UNINITIALIZED)
123     return smpi_process_comm_world()->size();
124   return m_group->size();
125 }
126
127 int Comm::rank()
128 {
129   if (this == MPI_COMM_UNINITIALIZED)
130     return smpi_process_comm_world()->rank();
131   return m_group->rank(smpi_process_index());
132 }
133
134 void Comm::get_name (char* name, int* len)
135 {
136   if (this == MPI_COMM_UNINITIALIZED)
137     return smpi_process_comm_world()->get_name(name, len);
138   if(this == MPI_COMM_WORLD) {
139     strncpy(name, "WORLD",5);
140     *len = 5;
141   } else {
142     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
143   }
144 }
145
146 void Comm::set_leaders_comm(MPI_Comm leaders){
147   if (this == MPI_COMM_UNINITIALIZED)
148     return smpi_process_comm_world()->set_leaders_comm(leaders);
149   m_leaders_comm=leaders;
150 }
151
152 void Comm::set_intra_comm(MPI_Comm leaders){
153   m_intra_comm=leaders;
154 }
155
156 int* Comm::get_non_uniform_map(){
157   if (this == MPI_COMM_UNINITIALIZED)
158     return smpi_process_comm_world()->get_non_uniform_map();
159   return m_non_uniform_map;
160 }
161
162 int* Comm::get_leaders_map(){
163   if (this == MPI_COMM_UNINITIALIZED)
164     return smpi_process_comm_world()->get_leaders_map();
165   return m_leaders_map;
166 }
167
168 MPI_Comm Comm::get_leaders_comm(){
169   if (this == MPI_COMM_UNINITIALIZED)
170     return smpi_process_comm_world()->get_leaders_comm();
171   return m_leaders_comm;
172 }
173
174 MPI_Comm Comm::get_intra_comm(){
175   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD) 
176     return smpi_process_get_comm_intra();
177   else return m_intra_comm;
178 }
179
180 int Comm::is_uniform(){
181   if (this == MPI_COMM_UNINITIALIZED)
182     return smpi_process_comm_world()->is_uniform();
183   return m_is_uniform;
184 }
185
186 int Comm::is_blocked(){
187   if (this == MPI_COMM_UNINITIALIZED)
188     return smpi_process_comm_world()->is_blocked();
189   return m_is_blocked;
190 }
191
192 MPI_Comm Comm::split(int color, int key)
193 {
194   if (this == MPI_COMM_UNINITIALIZED)
195     return smpi_process_comm_world()->split(color, key);
196   int system_tag = 123;
197   int* recvbuf;
198
199   MPI_Group group_root = nullptr;
200   MPI_Group group_out  = nullptr;
201   MPI_Group group      = this->group();
202   int rank             = this->rank();
203   int size             = this->size();
204   /* Gather all colors and keys on rank 0 */
205   int* sendbuf = xbt_new(int, 2);
206   sendbuf[0] = color;
207   sendbuf[1] = key;
208   if(rank == 0) {
209     recvbuf = xbt_new(int, 2 * size);
210   } else {
211     recvbuf = nullptr;
212   }
213   smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
214   xbt_free(sendbuf);
215   /* Do the actual job */
216   if(rank == 0) {
217     MPI_Group* group_snd = xbt_new(MPI_Group, size);
218     int* rankmap         = xbt_new(int, 2 * size);
219     for (int i = 0; i < size; i++) {
220       if (recvbuf[2 * i] != MPI_UNDEFINED) {
221         int count = 0;
222         for (int j = i + 1; j < size; j++) {
223           if(recvbuf[2 * i] == recvbuf[2 * j]) {
224             recvbuf[2 * j] = MPI_UNDEFINED;
225             rankmap[2 * count] = j;
226             rankmap[2 * count + 1] = recvbuf[2 * j + 1];
227             count++;
228           }
229         }
230         /* Add self in the group */
231         recvbuf[2 * i] = MPI_UNDEFINED;
232         rankmap[2 * count] = i;
233         rankmap[2 * count + 1] = recvbuf[2 * i + 1];
234         count++;
235         qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
236         group_out = new simgrid::SMPI::Group(count);
237         if (i == 0) {
238           group_root = group_out; /* Save root's group */
239         }
240         for (int j = 0; j < count; j++) {
241           int index = group->index(rankmap[2 * j]);
242           group_out->set_mapping(index, j);
243         }
244         MPI_Request* requests = xbt_new(MPI_Request, count);
245         int reqs              = 0;
246         for (int j = 0; j < count; j++) {
247           if(rankmap[2 * j] != 0) {
248             group_snd[reqs]=new simgrid::SMPI::Group(group_out);
249             requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
250             reqs++;
251           }
252         }
253         if(i != 0) {
254           group_out->destroy();
255         }
256         smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
257         xbt_free(requests);
258       }
259     }
260     xbt_free(recvbuf);
261     xbt_free(rankmap);
262     xbt_free(group_snd);
263     group_out = group_root; /* exit with root's group */
264   } else {
265     if(color != MPI_UNDEFINED) {
266       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
267     } /* otherwise, exit with group_out == nullptr */
268   }
269   return group_out!=nullptr ? new simgrid::SMPI::Comm(group_out, nullptr) : MPI_COMM_NULL;
270 }
271
272 void Comm::use(){
273   if (this == MPI_COMM_UNINITIALIZED)
274     return smpi_process_comm_world()->use();
275   m_group->use();
276   m_refcount++;
277 }
278
279 void Comm::cleanup_attributes(){
280   if(m_attributes !=nullptr){
281     xbt_dict_cursor_t cursor = nullptr;
282     char* key;
283     void* value;
284     int flag;
285     xbt_dict_foreach (m_attributes, cursor, key, value) {
286       smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
287       if (elem != nullptr && elem->delete_fn != nullptr)
288         elem->delete_fn(this, atoi(key), value, &flag);
289     }
290     xbt_dict_free(&m_attributes);
291   }
292 }
293
294 void Comm::cleanup_smp(){
295   if (m_intra_comm != MPI_COMM_NULL)
296     m_intra_comm->unuse();
297   if (m_leaders_comm != MPI_COMM_NULL)
298     m_leaders_comm->unuse();
299   if (m_non_uniform_map != nullptr)
300     xbt_free(m_non_uniform_map);
301   if (m_leaders_map != nullptr)
302     xbt_free(m_leaders_map);
303 }
304
305 void Comm::unuse(){
306   if (this == MPI_COMM_UNINITIALIZED)
307     return smpi_process_comm_world()->unuse();
308   m_refcount--;
309   m_group->unuse();
310
311   if(m_refcount==0){
312     this->cleanup_smp();
313     this->cleanup_attributes();
314     delete this;
315   }
316 }
317
318 static int compare_ints (const void *a, const void *b)
319 {
320   const int *da = static_cast<const int *>(a);
321   const int *db = static_cast<const int *>(b);
322
323   return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
324 }
325
326 void Comm::init_smp(){
327   int leader = -1;
328
329   if (this == MPI_COMM_UNINITIALIZED)
330     return smpi_process_comm_world()->init_smp();
331
332   int comm_size = this->size();
333   
334   // If we are in replay - perform an ugly hack  
335   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
336   bool replaying = false; //cache data to set it back again after
337   if(smpi_process_get_replaying()){
338    replaying=true;
339    smpi_process_set_replaying(false);
340   }
341
342   if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
343      smpi_switch_data_segment(smpi_process_index());
344    }
345   //identify neighbours in comm
346   //get the indexes of all processes sharing the same simix host
347   xbt_swag_t process_list = SIMIX_host_self()->processes();
348   int intra_comm_size = 0;
349   int i =0;
350   int min_index=INT_MAX;//the minimum index will be the leader
351   smx_actor_t process = nullptr;
352   xbt_swag_foreach(process, process_list) {
353     int index = process->pid -1;
354
355     if(this->group()->rank(index)!=MPI_UNDEFINED){
356         intra_comm_size++;
357       //the process is in the comm
358       if(index < min_index)
359         min_index=index;
360       i++;
361     }
362   }
363   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
364   MPI_Group group_intra = new simgrid::SMPI::Group(intra_comm_size);
365   i=0;
366   process = nullptr;
367   xbt_swag_foreach(process, process_list) {
368     int index = process->pid -1;
369     if(this->group()->rank(index)!=MPI_UNDEFINED){
370       group_intra->set_mapping(index, i);
371       i++;
372     }
373   }
374
375   MPI_Comm comm_intra = new simgrid::SMPI::Comm(group_intra, nullptr);
376   leader=min_index;
377
378   int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
379   int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
380   for(i=0; i<comm_size; i++){
381       leader_list[i]=-1;
382   }
383
384   smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
385
386   if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
387      smpi_switch_data_segment(smpi_process_index());
388    }
389
390   if(m_leaders_map==nullptr){
391     m_leaders_map= leaders_map;
392   }else{
393     xbt_free(leaders_map);
394   }
395   int j=0;
396   int leader_group_size = 0;
397   for(i=0; i<comm_size; i++){
398       int already_done=0;
399       for(j=0;j<leader_group_size; j++){
400         if(m_leaders_map[i]==leader_list[j]){
401             already_done=1;
402         }
403       }
404       if(already_done==0){
405         leader_list[leader_group_size]=m_leaders_map[i];
406         leader_group_size++;
407       }
408   }
409   qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
410
411   MPI_Group leaders_group = new simgrid::SMPI::Group(leader_group_size);
412
413   MPI_Comm leader_comm = MPI_COMM_NULL;
414   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
415     //create leader_communicator
416     for (i=0; i< leader_group_size;i++)
417       leaders_group->set_mapping(leader_list[i], i);
418     leader_comm = new simgrid::SMPI::Comm(leaders_group, nullptr);
419     this->set_leaders_comm(leader_comm);
420     this->set_intra_comm(comm_intra);
421
422    //create intracommunicator
423   }else{
424     for (i=0; i< leader_group_size;i++)
425       leaders_group->set_mapping(leader_list[i], i);
426
427     if(this->get_leaders_comm()==MPI_COMM_NULL){
428       leader_comm = new simgrid::SMPI::Comm(leaders_group, nullptr);
429       this->set_leaders_comm(leader_comm);
430     }else{
431       leader_comm=this->get_leaders_comm();
432       leaders_group->unuse();
433     }
434     smpi_process_set_comm_intra(comm_intra);
435   }
436
437   int is_uniform = 1;
438
439   // Are the nodes uniform ? = same number of process/node
440   int my_local_size=comm_intra->size();
441   if(comm_intra->rank()==0) {
442     int* non_uniform_map = xbt_new0(int,leader_group_size);
443     smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
444         non_uniform_map, 1, MPI_INT, leader_comm);
445     for(i=0; i < leader_group_size; i++) {
446       if(non_uniform_map[0] != non_uniform_map[i]) {
447         is_uniform = 0;
448         break;
449       }
450     }
451     if(is_uniform==0 && this->is_uniform()!=0){
452         m_non_uniform_map= non_uniform_map;
453     }else{
454         xbt_free(non_uniform_map);
455     }
456     m_is_uniform=is_uniform;
457   }
458   smpi_coll_tuned_bcast_mpich(&(m_is_uniform),1, MPI_INT, 0, comm_intra );
459
460   if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
461      smpi_switch_data_segment(smpi_process_index());
462    }
463   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
464   int is_blocked=1;
465   int prev=this->group()->rank(comm_intra->group()->index(0));
466     for (i=1; i<my_local_size; i++){
467       int that=this->group()->rank(comm_intra->group()->index(i));
468       if(that!=prev+1){
469         is_blocked=0;
470         break;
471       }
472       prev = that;
473   }
474
475   int global_blocked;
476   smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
477
478   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
479     if(this->rank()==0){
480         m_is_blocked=global_blocked;
481     }
482   }else{
483     m_is_blocked=global_blocked;
484   }
485   xbt_free(leader_list);
486   
487   if(replaying)
488     smpi_process_set_replaying(true); 
489 }
490
491 int Comm::attr_delete(int keyval){
492   smpi_comm_key_elem elem =
493      static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
494   if(elem==nullptr)
495     return MPI_ERR_ARG;
496   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
497     void* value = nullptr;
498     int flag;
499     if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
500       int ret = elem->delete_fn(this, keyval, value, &flag);
501       if(ret!=MPI_SUCCESS) 
502         return ret;
503     }
504   }
505   if(m_attributes==nullptr)
506     return MPI_ERR_ARG;
507
508   xbt_dict_remove_ext(m_attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
509   return MPI_SUCCESS;
510 }
511
512 int Comm::attr_get(int keyval, void* attr_value, int* flag){
513   smpi_comm_key_elem elem =
514     static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
515   if(elem==nullptr)
516     return MPI_ERR_ARG;
517   if(m_attributes==nullptr){
518     *flag=0;
519     return MPI_SUCCESS;
520   }
521   try {
522     *static_cast<void**>(attr_value) =
523         xbt_dict_get_ext(m_attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
524     *flag=1;
525   }
526   catch (xbt_ex& ex) {
527     *flag=0;
528   }
529   return MPI_SUCCESS;
530 }
531
532 int Comm::attr_put(int keyval, void* attr_value){
533   if(smpi_comm_keyvals==nullptr)
534     smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
535   smpi_comm_key_elem elem =
536     static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(&keyval), sizeof(int)));
537   if(elem==nullptr)
538     return MPI_ERR_ARG;
539   int flag;
540   void* value = nullptr;
541   this->attr_get(keyval, &value, &flag);
542   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
543     int ret = elem->delete_fn(this, keyval, value, &flag);
544     if(ret!=MPI_SUCCESS) 
545       return ret;
546   }
547   if(m_attributes==nullptr)
548     m_attributes = xbt_dict_new_homogeneous(nullptr);
549
550   xbt_dict_set_ext(m_attributes,  reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
551   return MPI_SUCCESS;
552 }
553
554 }
555 }
556
557 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
558                             void* extra_state){
559   if(smpi_comm_keyvals==nullptr)
560     smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
561
562   smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
563
564   value->copy_fn=copy_fn;
565   value->delete_fn=delete_fn;
566
567   *keyval = comm_keyval_id;
568   xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
569   comm_keyval_id++;
570   return MPI_SUCCESS;
571 }
572
573 int smpi_comm_keyval_free(int* keyval){
574   smpi_comm_key_elem elem =
575      static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(keyval), sizeof(int)));
576   if(elem==nullptr)
577     return MPI_ERR_ARG;
578   xbt_dict_remove_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(keyval), sizeof(int));
579   xbt_free(elem);
580   return MPI_SUCCESS;
581 }