Logo AND Algorithmique Numérique Distribuée

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