Logo AND Algorithmique Numérique Distribuée

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