Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill smpi::Group::rank(s4u::Actor*).
[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)
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   if (errhandlers_ != nullptr)//MPI_COMM_WORLD, only grab our own
117     (*newcomm)->set_errhandler(errhandlers_[this->rank()]);
118   else
119     (*newcomm)->set_errhandler(errhandler_);
120   return ret;
121 }
122
123 int Comm::dup_with_info(MPI_Info info, MPI_Comm* newcomm){
124   int ret = dup(newcomm);
125   if(ret != MPI_SUCCESS)
126     return ret;
127   if((*newcomm)->info_!=MPI_INFO_NULL){
128     simgrid::smpi::Info::unref((*newcomm)->info_);
129     (*newcomm)->info_=MPI_INFO_NULL;
130   }
131   if(info != MPI_INFO_NULL){
132     info->ref();
133     (*newcomm)->info_=info;
134   }
135   return ret;
136 }
137
138 MPI_Group Comm::group()
139 {
140   if (this == MPI_COMM_UNINITIALIZED)
141     return smpi_process()->comm_world()->group();
142   return group_;
143 }
144
145 int Comm::size() const
146 {
147   if (this == MPI_COMM_UNINITIALIZED)
148     return smpi_process()->comm_world()->size();
149   return group_->size();
150 }
151
152 int Comm::rank() const
153 {
154   if (this == MPI_COMM_UNINITIALIZED)
155     return smpi_process()->comm_world()->rank();
156   return group_->rank(s4u::this_actor::get_pid());
157 }
158
159 int Comm::id() const
160 {
161   return id_;
162 }
163
164 void Comm::get_name(char* name, int* len) const
165 {
166   if (this == MPI_COMM_UNINITIALIZED){
167     smpi_process()->comm_world()->get_name(name, len);
168     return;
169   }
170   if(this == MPI_COMM_WORLD && name_.empty()) {
171     strncpy(name, "MPI_COMM_WORLD", 15);
172     *len = 14;
173   } else {
174     *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
175   }
176 }
177
178 std::string Comm::name() const
179 {
180   int size;
181   char name[MPI_MAX_NAME_STRING+1];
182   this->get_name(name, &size);
183   if (name[0]=='\0')
184     return std::string("MPI_Comm");
185   else
186     return std::string(name);
187 }
188
189
190 void Comm::set_name (const char* name)
191 {
192   if (this == MPI_COMM_UNINITIALIZED){
193     smpi_process()->comm_world()->set_name(name);
194     return;
195   }
196   name_.replace (0, MPI_MAX_NAME_STRING+1, name);
197 }
198
199
200 void Comm::set_leaders_comm(MPI_Comm leaders){
201   if (this == MPI_COMM_UNINITIALIZED){
202     smpi_process()->comm_world()->set_leaders_comm(leaders);
203     return;
204   }
205   leaders_comm_=leaders;
206 }
207
208 int* Comm::get_non_uniform_map() const
209 {
210   if (this == MPI_COMM_UNINITIALIZED)
211     return smpi_process()->comm_world()->get_non_uniform_map();
212   return non_uniform_map_;
213 }
214
215 int* Comm::get_leaders_map() const
216 {
217   if (this == MPI_COMM_UNINITIALIZED)
218     return smpi_process()->comm_world()->get_leaders_map();
219   return leaders_map_;
220 }
221
222 MPI_Comm Comm::get_leaders_comm() const
223 {
224   if (this == MPI_COMM_UNINITIALIZED)
225     return smpi_process()->comm_world()->get_leaders_comm();
226   return leaders_comm_;
227 }
228
229 MPI_Comm Comm::get_intra_comm() const
230 {
231   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
232     return smpi_process()->comm_intra();
233   else return intra_comm_;
234 }
235
236 bool Comm::is_uniform() const
237 {
238   if (this == MPI_COMM_UNINITIALIZED)
239     return smpi_process()->comm_world()->is_uniform();
240   return is_uniform_ != 0;
241 }
242
243 bool Comm::is_blocked() const
244 {
245   if (this == MPI_COMM_UNINITIALIZED)
246     return smpi_process()->comm_world()->is_blocked();
247   return is_blocked_ != 0;
248 }
249
250 bool Comm::is_smp_comm() const
251 {
252   if (this == MPI_COMM_UNINITIALIZED)
253     return smpi_process()->comm_world()->is_smp_comm();
254   return is_smp_comm_;
255 }
256
257 MPI_Comm Comm::split(int color, int key)
258 {
259   if (this == MPI_COMM_UNINITIALIZED)
260     return smpi_process()->comm_world()->split(color, key);
261   int system_tag = -123;
262
263   MPI_Group group_root = nullptr;
264   MPI_Group group_out  = nullptr;
265   const Group* group   = this->group();
266   int myrank           = this->rank();
267   int size             = this->size();
268   /* Gather all colors and keys on rank 0 */
269   const std::array<int, 2> sendbuf = {{color, key}};
270   std::vector<int> recvbuf;
271   if (myrank == 0)
272     recvbuf.resize(2 * size);
273   gather__default(sendbuf.data(), 2, MPI_INT, recvbuf.data(), 2, MPI_INT, 0, this);
274   /* Do the actual job */
275   if (myrank == 0) {
276     std::vector<MPI_Group> group_snd(size);
277     std::vector<std::pair<int, int>> rankmap;
278     rankmap.reserve(size);
279     for (int i = 0; i < size; i++) {
280       if (recvbuf[2 * i] != MPI_UNDEFINED) {
281         rankmap.clear();
282         for (int j = i + 1; j < size; j++) {
283           if(recvbuf[2 * i] == recvbuf[2 * j]) {
284             recvbuf[2 * j] = MPI_UNDEFINED;
285             rankmap.emplace_back(recvbuf[2 * j + 1], j);
286           }
287         }
288         /* Add self in the group */
289         recvbuf[2 * i] = MPI_UNDEFINED;
290         rankmap.emplace_back(recvbuf[2 * i + 1], i);
291         std::sort(begin(rankmap), end(rankmap));
292         group_out = new Group(rankmap.size());
293         if (i == 0) {
294           group_root = group_out; /* Save root's group */
295         }
296         for (unsigned j = 0; j < rankmap.size(); j++) {
297           aid_t actor = group->actor_pid(rankmap[j].second);
298           group_out->set_mapping(actor, j);
299         }
300         std::vector<MPI_Request> requests(rankmap.size());
301         int reqs              = 0;
302         for (auto const& rank : rankmap) {
303           if (rank.second != 0) {
304             group_snd[reqs]=new  Group(group_out);
305             requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
306             reqs++;
307           }
308         }
309         if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
310           Group::unref(group_out);
311
312         Request::waitall(reqs, requests.data(), MPI_STATUS_IGNORE);
313       }
314     }
315     group_out = group_root; /* exit with root's group */
316   } else {
317     if(color != MPI_UNDEFINED) {
318       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
319     } /* otherwise, exit with group_out == nullptr */
320   }
321   return group_out!=nullptr ? new  Comm(group_out, topo_) : MPI_COMM_NULL;
322 }
323
324 void Comm::ref(){
325   if (this == MPI_COMM_UNINITIALIZED){
326     smpi_process()->comm_world()->ref();
327     return;
328   }
329   group_->ref();
330   refcount_++;
331 }
332
333 void Comm::cleanup_smp(){
334   if (intra_comm_ != MPI_COMM_NULL)
335     Comm::unref(intra_comm_);
336   if (leaders_comm_ != MPI_COMM_NULL)
337     Comm::unref(leaders_comm_);
338   xbt_free(non_uniform_map_);
339   delete[] leaders_map_;
340 }
341
342 void Comm::unref(Comm* comm){
343   if (comm == MPI_COMM_UNINITIALIZED){
344     Comm::unref(smpi_process()->comm_world());
345     return;
346   }
347   comm->refcount_--;
348
349   if(comm->refcount_==0){
350     if(simgrid::smpi::F2C::lookup() != nullptr)
351       F2C::free_f(comm->f2c_id());
352     comm->cleanup_smp();
353     comm->cleanup_attr<Comm>();
354     if (comm->info_ != MPI_INFO_NULL)
355       simgrid::smpi::Info::unref(comm->info_);
356     if(comm->errhandlers_!=nullptr){
357       for (int i=0; i<comm->size(); i++)
358         if (comm->errhandlers_[i]!=MPI_ERRHANDLER_NULL)
359           simgrid::smpi::Errhandler::unref(comm->errhandlers_[i]);
360       delete[] comm->errhandlers_;
361     } else if (comm->errhandler_ != MPI_ERRHANDLER_NULL)
362       simgrid::smpi::Errhandler::unref(comm->errhandler_);
363   }
364   Group::unref(comm->group_);
365   if(comm->refcount_==0)
366     delete comm;
367 }
368
369 MPI_Comm Comm::find_intra_comm(int * leader){
370   //get the indices of all processes sharing the same simix host
371   int intra_comm_size     = 0;
372   int min_index           = INT_MAX; // the minimum index will be the leader
373   sg_host_self()->get_impl()->foreach_actor([this, &intra_comm_size, &min_index](auto& actor) {
374     if (this->group()->rank(actor.get_pid()) != MPI_UNDEFINED) { // Is this process in the current group?
375       intra_comm_size++;
376       int index = actor.get_pid();
377       if (index < min_index)
378         min_index = index;
379     }
380   });
381   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
382   auto* group_intra = new Group(intra_comm_size);
383   int i = 0;
384   sg_host_self()->get_impl()->foreach_actor([this, group_intra, &i](auto& actor) {
385     if (this->group()->rank(actor.get_pid()) != MPI_UNDEFINED) {
386       group_intra->set_mapping(actor.get_pid(), i);
387       i++;
388     }
389   });
390   *leader=min_index;
391   return new Comm(group_intra, nullptr, true);
392 }
393
394 void Comm::init_smp(){
395   int leader = -1;
396   int i = 0;
397   if (this == MPI_COMM_UNINITIALIZED)
398     smpi_process()->comm_world()->init_smp();
399
400   int comm_size = this->size();
401
402   // If we are in replay - perform an ugly hack
403   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
404   bool replaying = false; //cache data to set it back again after
405   if(smpi_process()->replaying()){
406     replaying = true;
407     smpi_process()->set_replaying(false);
408   }
409
410   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
411     // we need to switch as the called function may silently touch global variables
412     smpi_switch_data_segment(s4u::Actor::self());
413   }
414   // identify neighbors in comm
415   MPI_Comm comm_intra = find_intra_comm(&leader);
416
417   auto* leaders_map = new int[comm_size];
418   auto* leader_list = new int[comm_size];
419   std::fill_n(leaders_map, comm_size, 0);
420   std::fill_n(leader_list, comm_size, -1);
421
422   allgather__ring(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
423
424   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
425     // we need to switch as the called function may silently touch global variables
426     smpi_switch_data_segment(s4u::Actor::self());
427   }
428
429   if(leaders_map_==nullptr){
430     leaders_map_= leaders_map;
431   }else{
432     delete[] leaders_map;
433   }
434   int leader_group_size = 0;
435   for(i=0; i<comm_size; i++){
436     int already_done = 0;
437     for (int j = 0; j < leader_group_size; j++) {
438       if (leaders_map_[i] == leader_list[j]) {
439         already_done = 1;
440       }
441     }
442     if (already_done == 0) {
443       leader_list[leader_group_size] = leaders_map_[i];
444       leader_group_size++;
445     }
446   }
447   xbt_assert(leader_group_size > 0);
448   std::sort(leader_list, leader_list + leader_group_size);
449
450   auto* leaders_group = new Group(leader_group_size);
451
452   MPI_Comm leader_comm = MPI_COMM_NULL;
453   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
454     //create leader_communicator
455     for (i=0; i< leader_group_size;i++)
456       leaders_group->set_mapping(leader_list[i], i);
457     leader_comm = new Comm(leaders_group, nullptr, true);
458     this->set_leaders_comm(leader_comm);
459     this->set_intra_comm(comm_intra);
460
461     // create intracommunicator
462   }else{
463     for (i=0; i< leader_group_size;i++)
464       leaders_group->set_mapping(leader_list[i], i);
465
466     if(this->get_leaders_comm()==MPI_COMM_NULL){
467       leader_comm = new Comm(leaders_group, nullptr, true);
468       this->set_leaders_comm(leader_comm);
469     }else{
470       leader_comm=this->get_leaders_comm();
471       Group::unref(leaders_group);
472     }
473     smpi_process()->set_comm_intra(comm_intra);
474   }
475
476   // Are the nodes uniform ? = same number of process/node
477   int my_local_size=comm_intra->size();
478   if(comm_intra->rank()==0) {
479     int is_uniform       = 1;
480     int* non_uniform_map = xbt_new0(int,leader_group_size);
481     allgather__ring(&my_local_size, 1, MPI_INT,
482         non_uniform_map, 1, MPI_INT, leader_comm);
483     for(i=0; i < leader_group_size; i++) {
484       if(non_uniform_map[0] != non_uniform_map[i]) {
485         is_uniform = 0;
486         break;
487       }
488     }
489     if (is_uniform == 0 && this->is_uniform()) {
490       non_uniform_map_ = non_uniform_map;
491     }else{
492       xbt_free(non_uniform_map);
493     }
494     is_uniform_=is_uniform;
495   }
496   bcast__scatter_LR_allgather(&is_uniform_, 1, MPI_INT, 0, comm_intra);
497
498   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
499     // we need to switch as the called function may silently touch global variables
500     smpi_switch_data_segment(s4u::Actor::self());
501   }
502   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
503   int is_blocked=1;
504   int prev      = this->group()->rank(comm_intra->group()->actor_pid(0));
505   for (i = 1; i < my_local_size; i++) {
506     int that = this->group()->rank(comm_intra->group()->actor_pid(i));
507     if (that != prev + 1) {
508       is_blocked = 0;
509       break;
510     }
511     prev = that;
512   }
513
514   int global_blocked;
515   allreduce__default(&is_blocked, &global_blocked, 1, MPI_INT, MPI_LAND, this);
516
517   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
518     if(this->rank()==0){
519       is_blocked_ = global_blocked;
520     }
521   }else{
522     is_blocked_=global_blocked;
523   }
524   delete[] leader_list;
525
526   if(replaying)
527     smpi_process()->set_replaying(true);
528 }
529
530 MPI_Comm Comm::f2c(int id) {
531   if(id == -2) {
532     return MPI_COMM_SELF;
533   } else if(id==0){
534     return MPI_COMM_WORLD;
535   } else if (F2C::lookup() != nullptr && id >= 0) {
536     const auto& lookup = F2C::lookup();
537     auto comm          = lookup->find(id);
538     return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
539   } else {
540     return MPI_COMM_NULL;
541   }
542 }
543
544 void Comm::free_f(int id) {
545   F2C::lookup()->erase(id);
546 }
547
548 void Comm::add_rma_win(MPI_Win win){
549   rma_wins_.push_back(win);
550 }
551
552 void Comm::remove_rma_win(MPI_Win win){
553   rma_wins_.remove(win);
554 }
555
556 void Comm::finish_rma_calls() const
557 {
558   for (auto const& it : rma_wins_) {
559     if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
560       int finished = it->finish_comms();
561       XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
562     }
563   }
564 }
565
566 MPI_Info Comm::info()
567 {
568   if (info_ == MPI_INFO_NULL)
569     info_ = new Info();
570   info_->ref();
571   return info_;
572 }
573
574 void Comm::set_info(MPI_Info info)
575 {
576   if (info_ != MPI_INFO_NULL)
577     simgrid::smpi::Info::unref(info);
578   info_ = info;
579   if (info_ != MPI_INFO_NULL)
580     info->ref();
581 }
582
583 MPI_Errhandler Comm::errhandler()
584 {
585   if (this != MPI_COMM_WORLD){
586     if (errhandler_ != MPI_ERRHANDLER_NULL)
587       errhandler_->ref();
588     return errhandler_;
589   } else {
590     if(errhandlers_==nullptr)
591       return MPI_ERRORS_ARE_FATAL;
592     else {
593       if(errhandlers_[this->rank()] != MPI_ERRHANDLER_NULL)
594         errhandlers_[this->rank()]->ref();
595       return errhandlers_[this->rank()];
596     }
597   }
598 }
599
600 void Comm::set_errhandler(MPI_Errhandler errhandler)
601 {
602   if(this != MPI_COMM_WORLD){
603     if (errhandler_ != MPI_ERRHANDLER_NULL)
604       simgrid::smpi::Errhandler::unref(errhandler_);
605     errhandler_ = errhandler;
606   }else{
607     if(errhandlers_==nullptr)
608       errhandlers_= new MPI_Errhandler[this->size()]{MPI_ERRHANDLER_NULL};
609     if(errhandlers_[this->rank()] != MPI_ERRHANDLER_NULL)
610       simgrid::smpi::Errhandler::unref(errhandlers_[this->rank()]);
611     errhandlers_[this->rank()]=errhandler;
612   }
613   if (errhandler != MPI_ERRHANDLER_NULL)
614     errhandler->ref();
615 }
616
617 MPI_Comm Comm::split_type(int type, int /*key*/, const Info*)
618 {
619   //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
620   if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
621     return MPI_COMM_NULL;
622   }
623   int leader=0;
624   MPI_Comm res= this->find_intra_comm(&leader);
625   if(type != MPI_UNDEFINED)
626     return res;
627   else{
628     xbt_assert(res->refcount_ == 1); // ensure the next call to Comm::destroy really frees the comm
629     Comm::destroy(res);
630     return MPI_COMM_NULL;
631   }
632 }
633
634 } // namespace smpi
635 } // namespace simgrid