Logo AND Algorithmique Numérique Distribuée

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