Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix refcounting of smpi::Errhandler.
[simgrid.git] / src / smpi / mpi / smpi_comm.cpp
1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "smpi_comm.hpp"
7 #include "smpi_coll.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_request.hpp"
10 #include "smpi_win.hpp"
11 #include "smpi_info.hpp"
12 #include "src/smpi/include/smpi_actor.hpp"
13 #include "src/surf/HostImpl.hpp"
14
15 #include <climits>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
18
19 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
20 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
21
22 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
23  * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
24
25 namespace simgrid{
26 namespace smpi{
27
28 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
29 int Comm::keyval_id_=0;
30
31 Comm::Comm(MPI_Group group, MPI_Topology topo, int smp, int in_id) : group_(group), topo_(topo),is_smp_comm_(smp), id_(in_id)
32 {
33   refcount_        = 1;
34   topoType_        = MPI_INVALID_TOPO;
35   intra_comm_      = MPI_COMM_NULL;
36   leaders_comm_    = MPI_COMM_NULL;
37   is_uniform_      = 1;
38   non_uniform_map_ = nullptr;
39   leaders_map_     = nullptr;
40   is_blocked_      = 0;
41   info_            = MPI_INFO_NULL;
42   errhandler_      = MPI_ERRORS_ARE_FATAL;
43   errhandler_->ref();
44   //First creation of comm is done before SIMIX_run, so only do comms for others
45   if(in_id==MPI_UNDEFINED && smp==0 && this->rank()!=MPI_UNDEFINED ){
46     int id;
47     if(this->rank()==0){
48       static int global_id_ = 0;
49       id=global_id_;
50       global_id_++;
51     }
52     colls::bcast(&id, 1, MPI_INT, 0, this);
53     XBT_DEBUG("Communicator %p has id %d", this, id);
54     id_=id;//only set here, as we don't want to change it in the middle of the bcast
55     colls::barrier(this);
56   }
57 }
58
59 void Comm::destroy(Comm* comm)
60 {
61   if (comm == MPI_COMM_UNINITIALIZED){
62     Comm::destroy(smpi_process()->comm_world());
63     return;
64   }
65   Comm::unref(comm);
66 }
67
68 int Comm::dup(MPI_Comm* newcomm){
69   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
70     // we need to switch as the called function may silently touch global variables
71     smpi_switch_data_segment(s4u::Actor::self());
72   }
73   MPI_Group cp = new  Group(this->group());
74   (*newcomm)   = new  Comm(cp, this->topo());
75   int ret      = MPI_SUCCESS;
76
77   if (not attributes()->empty()) {
78     int flag=0;
79     void* value_out=nullptr;
80     for (auto const& it : *attributes()) {
81       smpi_key_elem elem = keyvals_.at(it.first);
82       if (elem != nullptr){
83         if( elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN && 
84             elem->copy_fn.comm_copy_fn != MPI_COMM_DUP_FN)
85           ret = elem->copy_fn.comm_copy_fn(this, it.first, elem->extra_state, it.second, &value_out, &flag);
86         else if ( elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN &&
87                   *(int*)*elem->copy_fn.comm_copy_fn_fort != 1){
88           value_out=(int*)xbt_malloc(sizeof(int));
89           elem->copy_fn.comm_copy_fn_fort(this, it.first, elem->extra_state, it.second, value_out, &flag,&ret);
90         }
91         if (ret != MPI_SUCCESS) {
92           Comm::destroy(*newcomm);
93           *newcomm = MPI_COMM_NULL;
94           return ret;
95         }
96         if (elem->copy_fn.comm_copy_fn == MPI_COMM_DUP_FN || 
97            ((elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN) && *(int*)*elem->copy_fn.comm_copy_fn_fort == 1)){
98           elem->refcount++;
99           (*newcomm)->attributes()->insert({it.first, it.second});
100         }else if (flag){
101           elem->refcount++;
102           (*newcomm)->attributes()->insert({it.first, value_out});
103         }
104       }
105     }
106   }
107   //duplicate info if present
108   if(info_!=MPI_INFO_NULL)
109     (*newcomm)->info_ = new simgrid::smpi::Info(info_);
110   //duplicate errhandler
111   (*newcomm)->set_errhandler(errhandler_);
112   return ret;
113 }
114
115 int Comm::dup_with_info(MPI_Info info, MPI_Comm* newcomm){
116   int ret = dup(newcomm);
117   if(ret != MPI_SUCCESS)
118     return ret;
119   if((*newcomm)->info_!=MPI_INFO_NULL){
120     simgrid::smpi::Info::unref((*newcomm)->info_);
121     (*newcomm)->info_=MPI_INFO_NULL;
122   }
123   if(info != MPI_INFO_NULL){
124     info->ref();
125     (*newcomm)->info_=info;
126   }
127   return ret;
128 }
129
130 MPI_Group Comm::group()
131 {
132   if (this == MPI_COMM_UNINITIALIZED)
133     return smpi_process()->comm_world()->group();
134   return group_;
135 }
136
137 int Comm::size()
138 {
139   if (this == MPI_COMM_UNINITIALIZED)
140     return smpi_process()->comm_world()->size();
141   return group_->size();
142 }
143
144 int Comm::rank()
145 {
146   if (this == MPI_COMM_UNINITIALIZED)
147     return smpi_process()->comm_world()->rank();
148   return group_->rank(s4u::Actor::self());
149 }
150
151 int Comm::id()
152 {
153   return id_;
154 }
155
156 void Comm::get_name (char* name, int* len)
157 {
158   if (this == MPI_COMM_UNINITIALIZED){
159     smpi_process()->comm_world()->get_name(name, len);
160     return;
161   }
162   if(this == MPI_COMM_WORLD && name_.empty()) {
163     strncpy(name, "MPI_COMM_WORLD", 15);
164     *len = 14;
165   } else if(this == MPI_COMM_SELF && name_.empty()) {
166     strncpy(name, "MPI_COMM_SELF", 14);
167     *len = 13;
168   } else {
169     *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
170   }
171 }
172
173 void Comm::set_name (const char* name)
174 {
175   if (this == MPI_COMM_UNINITIALIZED){
176     smpi_process()->comm_world()->set_name(name);
177     return;
178   }
179   name_.replace (0, MPI_MAX_NAME_STRING+1, name);
180 }
181
182
183 void Comm::set_leaders_comm(MPI_Comm leaders){
184   if (this == MPI_COMM_UNINITIALIZED){
185     smpi_process()->comm_world()->set_leaders_comm(leaders);
186     return;
187   }
188   leaders_comm_=leaders;
189 }
190
191 int* Comm::get_non_uniform_map(){
192   if (this == MPI_COMM_UNINITIALIZED)
193     return smpi_process()->comm_world()->get_non_uniform_map();
194   return non_uniform_map_;
195 }
196
197 int* Comm::get_leaders_map(){
198   if (this == MPI_COMM_UNINITIALIZED)
199     return smpi_process()->comm_world()->get_leaders_map();
200   return leaders_map_;
201 }
202
203 MPI_Comm Comm::get_leaders_comm(){
204   if (this == MPI_COMM_UNINITIALIZED)
205     return smpi_process()->comm_world()->get_leaders_comm();
206   return leaders_comm_;
207 }
208
209 MPI_Comm Comm::get_intra_comm(){
210   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
211     return smpi_process()->comm_intra();
212   else return intra_comm_;
213 }
214
215 int Comm::is_uniform(){
216   if (this == MPI_COMM_UNINITIALIZED)
217     return smpi_process()->comm_world()->is_uniform();
218   return is_uniform_;
219 }
220
221 int Comm::is_blocked(){
222   if (this == MPI_COMM_UNINITIALIZED)
223     return smpi_process()->comm_world()->is_blocked();
224   return is_blocked_;
225 }
226
227 int Comm::is_smp_comm(){
228   if (this == MPI_COMM_UNINITIALIZED)
229     return smpi_process()->comm_world()->is_smp_comm();
230   return is_smp_comm_;
231 }
232
233 MPI_Comm Comm::split(int color, int key)
234 {
235   if (this == MPI_COMM_UNINITIALIZED)
236     return smpi_process()->comm_world()->split(color, key);
237   int system_tag = -123;
238   int* recvbuf;
239
240   MPI_Group group_root = nullptr;
241   MPI_Group group_out  = nullptr;
242   MPI_Group group      = this->group();
243   int myrank           = this->rank();
244   int size             = this->size();
245   /* Gather all colors and keys on rank 0 */
246   int* sendbuf = xbt_new(int, 2);
247   sendbuf[0] = color;
248   sendbuf[1] = key;
249   if (myrank == 0) {
250     recvbuf = xbt_new(int, 2 * size);
251   } else {
252     recvbuf = nullptr;
253   }
254   gather__default(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
255   xbt_free(sendbuf);
256   /* Do the actual job */
257   if (myrank == 0) {
258     MPI_Group* group_snd = xbt_new(MPI_Group, size);
259     std::vector<std::pair<int, int>> rankmap;
260     rankmap.reserve(size);
261     for (int i = 0; i < size; i++) {
262       if (recvbuf[2 * i] != MPI_UNDEFINED) {
263         rankmap.clear();
264         for (int j = i + 1; j < size; j++) {
265           if(recvbuf[2 * i] == recvbuf[2 * j]) {
266             recvbuf[2 * j] = MPI_UNDEFINED;
267             rankmap.push_back({recvbuf[2 * j + 1], j});
268           }
269         }
270         /* Add self in the group */
271         recvbuf[2 * i] = MPI_UNDEFINED;
272         rankmap.push_back({recvbuf[2 * i + 1], i});
273         std::sort(begin(rankmap), end(rankmap));
274         group_out = new Group(rankmap.size());
275         if (i == 0) {
276           group_root = group_out; /* Save root's group */
277         }
278         for (unsigned j = 0; j < rankmap.size(); j++) {
279           s4u::Actor* actor = group->actor(rankmap[j].second);
280           group_out->set_mapping(actor, j);
281         }
282         MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
283         int reqs              = 0;
284         for (auto const& rank : rankmap) {
285           if (rank.second != 0) {
286             group_snd[reqs]=new  Group(group_out);
287             requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
288             reqs++;
289           }
290         }
291         if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
292           Group::unref(group_out);
293
294         Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
295         xbt_free(requests);
296       }
297     }
298     xbt_free(recvbuf);
299     xbt_free(group_snd);
300     group_out = group_root; /* exit with root's group */
301   } else {
302     if(color != MPI_UNDEFINED) {
303       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
304     } /* otherwise, exit with group_out == nullptr */
305   }
306   return group_out!=nullptr ? new  Comm(group_out, nullptr) : MPI_COMM_NULL;
307 }
308
309 void Comm::ref(){
310   if (this == MPI_COMM_UNINITIALIZED){
311     smpi_process()->comm_world()->ref();
312     return;
313   }
314   group_->ref();
315   refcount_++;
316 }
317
318 void Comm::cleanup_smp(){
319   if (intra_comm_ != MPI_COMM_NULL)
320     Comm::unref(intra_comm_);
321   if (leaders_comm_ != MPI_COMM_NULL)
322     Comm::unref(leaders_comm_);
323   xbt_free(non_uniform_map_);
324   delete[] leaders_map_;
325 }
326
327 void Comm::unref(Comm* comm){
328   if (comm == MPI_COMM_UNINITIALIZED){
329     Comm::unref(smpi_process()->comm_world());
330     return;
331   }
332   comm->refcount_--;
333   Group::unref(comm->group_);
334
335   if(comm->refcount_==0){
336     comm->cleanup_smp();
337     comm->cleanup_attr<Comm>();
338     if (comm->info_ != MPI_INFO_NULL)
339       simgrid::smpi::Info::unref(comm->info_);
340     if (comm->errhandler_ != MPI_ERRHANDLER_NULL)
341       simgrid::smpi::Errhandler::unref(comm->errhandler_);
342     delete comm->topo_; // there's no use count on topos
343     delete comm;
344   }
345 }
346
347 MPI_Comm Comm::find_intra_comm(int * leader){
348   //get the indices of all processes sharing the same simix host
349   auto& actor_list        = sg_host_self()->pimpl_->actor_list_;
350   int intra_comm_size     = 0;
351   int min_index           = INT_MAX; // the minimum index will be the leader
352   for (auto& actor : actor_list) {
353     int index = actor.get_pid();
354     if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) { // Is this process in the current group?
355       intra_comm_size++;
356       if (index < min_index)
357         min_index = index;
358     }
359   }
360   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
361   MPI_Group group_intra = new  Group(intra_comm_size);
362   int i = 0;
363   for (auto& actor : actor_list) {
364     if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) {
365       group_intra->set_mapping(actor.ciface(), i);
366       i++;
367     }
368   }
369   *leader=min_index;
370   return new  Comm(group_intra, nullptr, 1);
371 }
372
373 void Comm::init_smp(){
374   int leader = -1;
375   int i = 0;
376   if (this == MPI_COMM_UNINITIALIZED)
377     smpi_process()->comm_world()->init_smp();
378
379   int comm_size = this->size();
380
381   // If we are in replay - perform an ugly hack
382   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
383   bool replaying = false; //cache data to set it back again after
384   if(smpi_process()->replaying()){
385     replaying = true;
386     smpi_process()->set_replaying(false);
387   }
388
389   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
390     // we need to switch as the called function may silently touch global variables
391     smpi_switch_data_segment(s4u::Actor::self());
392   }
393   // identify neighbors in comm
394   MPI_Comm comm_intra = find_intra_comm(&leader);
395
396
397   int* leaders_map = new int[comm_size];
398   int* leader_list = new int[comm_size];
399   std::fill_n(leaders_map, comm_size, 0);
400   std::fill_n(leader_list, comm_size, -1);
401
402   allgather__ring(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
403
404   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
405     // we need to switch as the called function may silently touch global variables
406     smpi_switch_data_segment(s4u::Actor::self());
407   }
408
409   if(leaders_map_==nullptr){
410     leaders_map_= leaders_map;
411   }else{
412     delete[] leaders_map;
413   }
414   int leader_group_size = 0;
415   for(i=0; i<comm_size; i++){
416     int already_done = 0;
417     for (int j = 0; j < leader_group_size; j++) {
418       if (leaders_map_[i] == leader_list[j]) {
419         already_done = 1;
420       }
421     }
422     if (already_done == 0) {
423       leader_list[leader_group_size] = leaders_map_[i];
424       leader_group_size++;
425     }
426   }
427   std::sort(leader_list, leader_list + leader_group_size);
428
429   MPI_Group leaders_group = new  Group(leader_group_size);
430
431   MPI_Comm leader_comm = MPI_COMM_NULL;
432   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
433     //create leader_communicator
434     for (i=0; i< leader_group_size;i++)
435       leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]).get(), i);
436     leader_comm = new  Comm(leaders_group, nullptr,1);
437     this->set_leaders_comm(leader_comm);
438     this->set_intra_comm(comm_intra);
439
440     // create intracommunicator
441   }else{
442     for (i=0; i< leader_group_size;i++)
443       leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]).get(), i);
444
445     if(this->get_leaders_comm()==MPI_COMM_NULL){
446       leader_comm = new  Comm(leaders_group, nullptr,1);
447       this->set_leaders_comm(leader_comm);
448     }else{
449       leader_comm=this->get_leaders_comm();
450       Group::unref(leaders_group);
451     }
452     smpi_process()->set_comm_intra(comm_intra);
453   }
454
455   // Are the nodes uniform ? = same number of process/node
456   int my_local_size=comm_intra->size();
457   if(comm_intra->rank()==0) {
458     int is_uniform       = 1;
459     int* non_uniform_map = xbt_new0(int,leader_group_size);
460     allgather__ring(&my_local_size, 1, MPI_INT,
461         non_uniform_map, 1, MPI_INT, leader_comm);
462     for(i=0; i < leader_group_size; i++) {
463       if(non_uniform_map[0] != non_uniform_map[i]) {
464         is_uniform = 0;
465         break;
466       }
467     }
468     if(is_uniform==0 && this->is_uniform()!=0){
469       non_uniform_map_ = non_uniform_map;
470     }else{
471       xbt_free(non_uniform_map);
472     }
473     is_uniform_=is_uniform;
474   }
475   bcast__scatter_LR_allgather(&(is_uniform_),1, MPI_INT, 0, comm_intra );
476
477   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
478     // we need to switch as the called function may silently touch global variables
479     smpi_switch_data_segment(s4u::Actor::self());
480   }
481   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
482   int is_blocked=1;
483   int prev=this->group()->rank(comm_intra->group()->actor(0));
484   for (i = 1; i < my_local_size; i++) {
485     int that = this->group()->rank(comm_intra->group()->actor(i));
486     if (that != prev + 1) {
487       is_blocked = 0;
488       break;
489     }
490     prev = that;
491   }
492
493   int global_blocked;
494   allreduce__default(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
495
496   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
497     if(this->rank()==0){
498       is_blocked_ = global_blocked;
499     }
500   }else{
501     is_blocked_=global_blocked;
502   }
503   delete[] leader_list;
504
505   if(replaying)
506     smpi_process()->set_replaying(true);
507 }
508
509 MPI_Comm Comm::f2c(int id) {
510   if(id == -2) {
511     return MPI_COMM_SELF;
512   } else if(id==0){
513     return MPI_COMM_WORLD;
514   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
515     char key[KEY_SIZE];
516     const auto& lookup = F2C::f2c_lookup();
517     auto comm          = lookup->find(get_key(key, id));
518     return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
519   } else {
520     return MPI_COMM_NULL;
521   }
522 }
523
524 void Comm::free_f(int id) {
525   char key[KEY_SIZE];
526   F2C::f2c_lookup()->erase(get_key(key, id));
527 }
528
529 void Comm::add_rma_win(MPI_Win win){
530   rma_wins_.push_back(win);
531 }
532
533 void Comm::remove_rma_win(MPI_Win win){
534   rma_wins_.remove(win);
535 }
536
537 void Comm::finish_rma_calls(){
538   for (auto const& it : rma_wins_) {
539     if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
540       int finished = it->finish_comms();
541       XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
542     }
543   }
544 }
545
546 MPI_Info Comm::info()
547 {
548   if (info_ == MPI_INFO_NULL)
549     info_ = new Info();
550   info_->ref();
551   return info_;
552 }
553
554 void Comm::set_info(MPI_Info info)
555 {
556   if (info_ != MPI_INFO_NULL)
557     simgrid::smpi::Info::unref(info);
558   info_ = info;
559   if (info_ != MPI_INFO_NULL)
560     info->ref();
561 }
562
563 MPI_Errhandler Comm::errhandler()
564 {
565   if (errhandler_ != MPI_ERRHANDLER_NULL)
566     errhandler_->ref();
567   return errhandler_;
568 }
569
570 void Comm::set_errhandler(MPI_Errhandler errhandler)
571 {
572   if (errhandler_ != MPI_ERRHANDLER_NULL)
573     simgrid::smpi::Errhandler::unref(errhandler_);
574   errhandler_ = errhandler;
575   if (errhandler_ != MPI_ERRHANDLER_NULL)
576     errhandler_->ref();
577 }
578
579 MPI_Comm Comm::split_type(int type, int /*key*/, MPI_Info)
580 {
581   //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
582   if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
583     return MPI_COMM_NULL;
584   }
585   int leader=0;
586   MPI_Comm res= this->find_intra_comm(&leader);
587   if(type != MPI_UNDEFINED)
588     return res;
589   else{
590     xbt_assert(res->refcount_ == 1); // ensure the next call to Comm::destroy really frees the comm
591     Comm::destroy(res);
592     return MPI_COMM_NULL;
593   }
594 }
595
596 } // namespace smpi
597 } // namespace simgrid