Logo AND Algorithmique Numérique Distribuée

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