Logo AND Algorithmique Numérique Distribuée

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