Logo AND Algorithmique Numérique Distribuée

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