Logo AND Algorithmique Numérique Distribuée

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