Logo AND Algorithmique Numérique Distribuée

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