Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
moved a line for comprehension
[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   MPI_Group 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()
129 {
130   if (this == MPI_COMM_UNINITIALIZED)
131     return smpi_process()->comm_world()->size();
132   return group_->size();
133 }
134
135 int Comm::rank()
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()
143 {
144   return id_;
145 }
146
147 void Comm::get_name (char* name, int* len)
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(){
183   if (this == MPI_COMM_UNINITIALIZED)
184     return smpi_process()->comm_world()->get_non_uniform_map();
185   return non_uniform_map_;
186 }
187
188 int* Comm::get_leaders_map(){
189   if (this == MPI_COMM_UNINITIALIZED)
190     return smpi_process()->comm_world()->get_leaders_map();
191   return leaders_map_;
192 }
193
194 MPI_Comm Comm::get_leaders_comm(){
195   if (this == MPI_COMM_UNINITIALIZED)
196     return smpi_process()->comm_world()->get_leaders_comm();
197   return leaders_comm_;
198 }
199
200 MPI_Comm Comm::get_intra_comm(){
201   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
202     return smpi_process()->comm_intra();
203   else return intra_comm_;
204 }
205
206 bool Comm::is_uniform()
207 {
208   if (this == MPI_COMM_UNINITIALIZED)
209     return smpi_process()->comm_world()->is_uniform();
210   return is_uniform_ != 0;
211 }
212
213 bool Comm::is_blocked()
214 {
215   if (this == MPI_COMM_UNINITIALIZED)
216     return smpi_process()->comm_world()->is_blocked();
217   return is_blocked_ != 0;
218 }
219
220 bool Comm::is_smp_comm()
221 {
222   if (this == MPI_COMM_UNINITIALIZED)
223     return smpi_process()->comm_world()->is_smp_comm();
224   return is_smp_comm_;
225 }
226
227 MPI_Comm Comm::split(int color, int key)
228 {
229   if (this == MPI_COMM_UNINITIALIZED)
230     return smpi_process()->comm_world()->split(color, key);
231   int system_tag = -123;
232   int* recvbuf;
233
234   MPI_Group group_root = nullptr;
235   MPI_Group group_out  = nullptr;
236   MPI_Group group      = this->group();
237   int myrank           = this->rank();
238   int size             = this->size();
239   /* Gather all colors and keys on rank 0 */
240   int* sendbuf = xbt_new(int, 2);
241   sendbuf[0] = color;
242   sendbuf[1] = key;
243   if (myrank == 0) {
244     recvbuf = xbt_new(int, 2 * size);
245   } else {
246     recvbuf = nullptr;
247   }
248   gather__default(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
249   xbt_free(sendbuf);
250   /* Do the actual job */
251   if (myrank == 0) {
252     MPI_Group* group_snd = xbt_new(MPI_Group, 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.push_back({recvbuf[2 * j + 1], j});
262           }
263         }
264         /* Add self in the group */
265         recvbuf[2 * i] = MPI_UNDEFINED;
266         rankmap.push_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         MPI_Request* requests = xbt_new(MPI_Request, 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, MPI_STATUS_IGNORE);
289         xbt_free(requests);
290       }
291     }
292     xbt_free(recvbuf);
293     xbt_free(group_snd);
294     group_out = group_root; /* exit with root's group */
295   } else {
296     if(color != MPI_UNDEFINED) {
297       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
298     } /* otherwise, exit with group_out == nullptr */
299   }
300   return group_out!=nullptr ? new  Comm(group_out, topo_) : MPI_COMM_NULL;
301 }
302
303 void Comm::ref(){
304   if (this == MPI_COMM_UNINITIALIZED){
305     smpi_process()->comm_world()->ref();
306     return;
307   }
308   group_->ref();
309   refcount_++;
310 }
311
312 void Comm::cleanup_smp(){
313   if (intra_comm_ != MPI_COMM_NULL)
314     Comm::unref(intra_comm_);
315   if (leaders_comm_ != MPI_COMM_NULL)
316     Comm::unref(leaders_comm_);
317   xbt_free(non_uniform_map_);
318   delete[] leaders_map_;
319 }
320
321 void Comm::unref(Comm* comm){
322   if (comm == MPI_COMM_UNINITIALIZED){
323     Comm::unref(smpi_process()->comm_world());
324     return;
325   }
326   comm->refcount_--;
327   Group::unref(comm->group_);
328
329   if(comm->refcount_==0){
330     comm->cleanup_smp();
331     comm->cleanup_attr<Comm>();
332     if (comm->info_ != MPI_INFO_NULL)
333       simgrid::smpi::Info::unref(comm->info_);
334     if (comm->errhandler_ != MPI_ERRHANDLER_NULL)
335       simgrid::smpi::Errhandler::unref(comm->errhandler_);
336     delete comm;
337   }
338 }
339
340 MPI_Comm Comm::find_intra_comm(int * leader){
341   //get the indices of all processes sharing the same simix host
342   auto& actor_list        = sg_host_self()->pimpl_->actor_list_;
343   int intra_comm_size     = 0;
344   int min_index           = INT_MAX; // the minimum index will be the leader
345   for (auto& actor : actor_list) {
346     int index = actor.get_pid();
347     if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) { // Is this process in the current group?
348       intra_comm_size++;
349       if (index < min_index)
350         min_index = index;
351     }
352   }
353   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
354   MPI_Group group_intra = new  Group(intra_comm_size);
355   int i = 0;
356   for (auto& actor : actor_list) {
357     if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) {
358       group_intra->set_mapping(actor.ciface(), i);
359       i++;
360     }
361   }
362   *leader=min_index;
363   return new  Comm(group_intra, nullptr, 1);
364 }
365
366 void Comm::init_smp(){
367   int leader = -1;
368   int i = 0;
369   if (this == MPI_COMM_UNINITIALIZED)
370     smpi_process()->comm_world()->init_smp();
371
372   int comm_size = this->size();
373
374   // If we are in replay - perform an ugly hack
375   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
376   bool replaying = false; //cache data to set it back again after
377   if(smpi_process()->replaying()){
378     replaying = true;
379     smpi_process()->set_replaying(false);
380   }
381
382   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
383     // we need to switch as the called function may silently touch global variables
384     smpi_switch_data_segment(s4u::Actor::self());
385   }
386   // identify neighbors in comm
387   MPI_Comm comm_intra = find_intra_comm(&leader);
388
389
390   int* leaders_map = new int[comm_size];
391   int* leader_list = new int[comm_size];
392   std::fill_n(leaders_map, comm_size, 0);
393   std::fill_n(leader_list, comm_size, -1);
394
395   allgather__ring(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
396
397   if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
398     // we need to switch as the called function may silently touch global variables
399     smpi_switch_data_segment(s4u::Actor::self());
400   }
401
402   if(leaders_map_==nullptr){
403     leaders_map_= leaders_map;
404   }else{
405     delete[] leaders_map;
406   }
407   int leader_group_size = 0;
408   for(i=0; i<comm_size; i++){
409     int already_done = 0;
410     for (int j = 0; j < leader_group_size; j++) {
411       if (leaders_map_[i] == leader_list[j]) {
412         already_done = 1;
413       }
414     }
415     if (already_done == 0) {
416       leader_list[leader_group_size] = leaders_map_[i];
417       leader_group_size++;
418     }
419   }
420   xbt_assert(leader_group_size > 0);
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]).get(), 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]).get(), 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     allgather__ring(&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()) {
463       non_uniform_map_ = non_uniform_map;
464     }else{
465       xbt_free(non_uniform_map);
466     }
467     is_uniform_=is_uniform;
468   }
469   bcast__scatter_LR_allgather(&(is_uniform_),1, MPI_INT, 0, comm_intra );
470
471   if (smpi_cfg_privatization() == 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   allreduce__default(&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 {
542   if (info_ == MPI_INFO_NULL)
543     info_ = new Info();
544   info_->ref();
545   return info_;
546 }
547
548 void Comm::set_info(MPI_Info info)
549 {
550   if (info_ != MPI_INFO_NULL)
551     simgrid::smpi::Info::unref(info);
552   info_ = info;
553   if (info_ != MPI_INFO_NULL)
554     info->ref();
555 }
556
557 MPI_Errhandler Comm::errhandler()
558 {
559   if (errhandler_ != MPI_ERRHANDLER_NULL)
560     errhandler_->ref();
561   return errhandler_;
562 }
563
564 void Comm::set_errhandler(MPI_Errhandler errhandler)
565 {
566   if (errhandler_ != MPI_ERRHANDLER_NULL)
567     simgrid::smpi::Errhandler::unref(errhandler_);
568   errhandler_ = errhandler;
569   if (errhandler_ != MPI_ERRHANDLER_NULL)
570     errhandler_->ref();
571 }
572
573 MPI_Comm Comm::split_type(int type, int /*key*/, const Info*)
574 {
575   //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
576   if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
577     return MPI_COMM_NULL;
578   }
579   int leader=0;
580   MPI_Comm res= this->find_intra_comm(&leader);
581   if(type != MPI_UNDEFINED)
582     return res;
583   else{
584     xbt_assert(res->refcount_ == 1); // ensure the next call to Comm::destroy really frees the comm
585     Comm::destroy(res);
586     return MPI_COMM_NULL;
587   }
588 }
589
590 } // namespace smpi
591 } // namespace simgrid