Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
activity::CommImpl: stick to our naming standards for the fields
[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 "src/smpi/include/smpi_actor.hpp"
12 #include "src/surf/HostImpl.hpp"
13
14 #include <climits>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
17
18 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
19 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
20
21 using simgrid::s4u::ActorPtr;
22
23 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
24  * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
25
26 namespace simgrid{
27 namespace smpi{
28
29 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
30 int Comm::keyval_id_=0;
31
32 Comm::Comm(MPI_Group group, MPI_Topology topo, int smp) : group_(group), topo_(topo),is_smp_comm_(smp)
33 {
34   refcount_        = 1;
35   topoType_        = MPI_INVALID_TOPO;
36   intra_comm_      = MPI_COMM_NULL;
37   leaders_comm_    = MPI_COMM_NULL;
38   is_uniform_      = 1;
39   non_uniform_map_ = nullptr;
40   leaders_map_     = nullptr;
41   is_blocked_      = 0;
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(simgrid::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   return ret;
94 }
95
96 MPI_Group Comm::group()
97 {
98   if (this == MPI_COMM_UNINITIALIZED)
99     return smpi_process()->comm_world()->group();
100   return group_;
101 }
102
103 MPI_Topology Comm::topo() {
104   return topo_;
105 }
106
107 int Comm::size()
108 {
109   if (this == MPI_COMM_UNINITIALIZED)
110     return smpi_process()->comm_world()->size();
111   return group_->size();
112 }
113
114 int Comm::rank()
115 {
116   if (this == MPI_COMM_UNINITIALIZED)
117     return smpi_process()->comm_world()->rank();
118   return group_->rank(simgrid::s4u::Actor::self());
119 }
120
121 void Comm::get_name (char* name, int* len)
122 {
123   if (this == MPI_COMM_UNINITIALIZED){
124     smpi_process()->comm_world()->get_name(name, len);
125     return;
126   }
127   if(this == MPI_COMM_WORLD) {
128     strncpy(name, "WORLD", 6);
129     *len = 5;
130   } else {
131     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
132   }
133 }
134
135 void Comm::set_leaders_comm(MPI_Comm leaders){
136   if (this == MPI_COMM_UNINITIALIZED){
137     smpi_process()->comm_world()->set_leaders_comm(leaders);
138     return;
139   }
140   leaders_comm_=leaders;
141 }
142
143 void Comm::set_intra_comm(MPI_Comm leaders){
144   intra_comm_=leaders;
145 }
146
147 int* Comm::get_non_uniform_map(){
148   if (this == MPI_COMM_UNINITIALIZED)
149     return smpi_process()->comm_world()->get_non_uniform_map();
150   return non_uniform_map_;
151 }
152
153 int* Comm::get_leaders_map(){
154   if (this == MPI_COMM_UNINITIALIZED)
155     return smpi_process()->comm_world()->get_leaders_map();
156   return leaders_map_;
157 }
158
159 MPI_Comm Comm::get_leaders_comm(){
160   if (this == MPI_COMM_UNINITIALIZED)
161     return smpi_process()->comm_world()->get_leaders_comm();
162   return leaders_comm_;
163 }
164
165 MPI_Comm Comm::get_intra_comm(){
166   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
167     return smpi_process()->comm_intra();
168   else return intra_comm_;
169 }
170
171 int Comm::is_uniform(){
172   if (this == MPI_COMM_UNINITIALIZED)
173     return smpi_process()->comm_world()->is_uniform();
174   return is_uniform_;
175 }
176
177 int Comm::is_blocked(){
178   if (this == MPI_COMM_UNINITIALIZED)
179     return smpi_process()->comm_world()->is_blocked();
180   return is_blocked_;
181 }
182
183 int Comm::is_smp_comm(){
184   if (this == MPI_COMM_UNINITIALIZED)
185     return smpi_process()->comm_world()->is_smp_comm();
186   return is_smp_comm_;
187 }
188
189 MPI_Comm Comm::split(int color, int key)
190 {
191   if (this == MPI_COMM_UNINITIALIZED)
192     return smpi_process()->comm_world()->split(color, key);
193   int system_tag = 123;
194   int* recvbuf;
195
196   MPI_Group group_root = nullptr;
197   MPI_Group group_out  = nullptr;
198   MPI_Group group      = this->group();
199   int rank             = this->rank();
200   int size             = this->size();
201   /* Gather all colors and keys on rank 0 */
202   int* sendbuf = xbt_new(int, 2);
203   sendbuf[0] = color;
204   sendbuf[1] = key;
205   if(rank == 0) {
206     recvbuf = xbt_new(int, 2 * size);
207   } else {
208     recvbuf = nullptr;
209   }
210   Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
211   xbt_free(sendbuf);
212   /* Do the actual job */
213   if(rank == 0) {
214     MPI_Group* group_snd = xbt_new(MPI_Group, size);
215     std::vector<std::pair<int, int>> rankmap;
216     rankmap.reserve(size);
217     for (int i = 0; i < size; i++) {
218       if (recvbuf[2 * i] != MPI_UNDEFINED) {
219         rankmap.clear();
220         for (int j = i + 1; j < size; j++) {
221           if(recvbuf[2 * i] == recvbuf[2 * j]) {
222             recvbuf[2 * j] = MPI_UNDEFINED;
223             rankmap.push_back({recvbuf[2 * j + 1], j});
224           }
225         }
226         /* Add self in the group */
227         recvbuf[2 * i] = MPI_UNDEFINED;
228         rankmap.push_back({recvbuf[2 * i + 1], i});
229         std::sort(begin(rankmap), end(rankmap));
230         group_out = new Group(rankmap.size());
231         if (i == 0) {
232           group_root = group_out; /* Save root's group */
233         }
234         for (unsigned j = 0; j < rankmap.size(); j++) {
235           ActorPtr actor = group->actor(rankmap[j].second);
236           group_out->set_mapping(actor, j);
237         }
238         MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
239         int reqs              = 0;
240         for (auto const& rank : rankmap) {
241           if (rank.second != 0) {
242             group_snd[reqs]=new  Group(group_out);
243             requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
244             reqs++;
245           }
246         }
247         if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
248           Group::unref(group_out);
249
250         Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
251         xbt_free(requests);
252       }
253     }
254     xbt_free(recvbuf);
255     xbt_free(group_snd);
256     group_out = group_root; /* exit with root's group */
257   } else {
258     if(color != MPI_UNDEFINED) {
259       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
260     } /* otherwise, exit with group_out == nullptr */
261   }
262   return group_out!=nullptr ? new  Comm(group_out, nullptr) : MPI_COMM_NULL;
263 }
264
265 void Comm::ref(){
266   if (this == MPI_COMM_UNINITIALIZED){
267     smpi_process()->comm_world()->ref();
268     return;
269   }
270   group_->ref();
271   refcount_++;
272 }
273
274 void Comm::cleanup_smp(){
275   if (intra_comm_ != MPI_COMM_NULL)
276     Comm::unref(intra_comm_);
277   if (leaders_comm_ != MPI_COMM_NULL)
278     Comm::unref(leaders_comm_);
279   xbt_free(non_uniform_map_);
280   delete[] leaders_map_;
281 }
282
283 void Comm::unref(Comm* comm){
284   if (comm == MPI_COMM_UNINITIALIZED){
285     Comm::unref(smpi_process()->comm_world());
286     return;
287   }
288   comm->refcount_--;
289   Group::unref(comm->group_);
290
291   if(comm->refcount_==0){
292     comm->cleanup_smp();
293     comm->cleanup_attr<Comm>();
294     delete comm;
295   }
296 }
297
298 void Comm::init_smp(){
299   int leader = -1;
300
301   if (this == MPI_COMM_UNINITIALIZED)
302     smpi_process()->comm_world()->init_smp();
303
304   int comm_size = this->size();
305
306   // If we are in replay - perform an ugly hack
307   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
308   bool replaying = false; //cache data to set it back again after
309   if(smpi_process()->replaying()){
310     replaying = true;
311     smpi_process()->set_replaying(false);
312   }
313
314   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
315     // we need to switch as the called function may silently touch global variables
316     smpi_switch_data_segment(simgrid::s4u::Actor::self());
317   }
318   //identify neighbours in comm
319   //get the indices of all processes sharing the same simix host
320   auto& process_list      = sg_host_self()->pimpl_->process_list_;
321   int intra_comm_size     = 0;
322   int min_index           = INT_MAX; // the minimum index will be the leader
323   for (auto& actor : process_list) {
324     int index = actor.pid_;
325     if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
326       intra_comm_size++;
327       if (index < min_index)
328         min_index = index;
329     }
330   }
331   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
332   MPI_Group group_intra = new  Group(intra_comm_size);
333   int i = 0;
334   for (auto& actor : process_list) {
335     if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
336       group_intra->set_mapping(actor.iface(), i);
337       i++;
338     }
339   }
340
341   MPI_Comm comm_intra = new  Comm(group_intra, nullptr, 1);
342   leader=min_index;
343
344   int* leaders_map = new int[comm_size];
345   int* leader_list = new int[comm_size];
346   std::fill_n(leaders_map, comm_size, 0);
347   std::fill_n(leader_list, comm_size, -1);
348
349   Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
350
351   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
352     // we need to switch as the called function may silently touch global variables
353     smpi_switch_data_segment(simgrid::s4u::Actor::self());
354   }
355
356   if(leaders_map_==nullptr){
357     leaders_map_= leaders_map;
358   }else{
359     delete[] leaders_map;
360   }
361   int leader_group_size = 0;
362   for(i=0; i<comm_size; i++){
363     int already_done = 0;
364     for (int j = 0; j < leader_group_size; j++) {
365       if (leaders_map_[i] == leader_list[j]) {
366         already_done = 1;
367       }
368     }
369     if (already_done == 0) {
370       leader_list[leader_group_size] = leaders_map_[i];
371       leader_group_size++;
372     }
373   }
374   std::sort(leader_list, leader_list + leader_group_size);
375
376   MPI_Group leaders_group = new  Group(leader_group_size);
377
378   MPI_Comm leader_comm = MPI_COMM_NULL;
379   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
380     //create leader_communicator
381     for (i=0; i< leader_group_size;i++)
382       leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
383     leader_comm = new  Comm(leaders_group, nullptr,1);
384     this->set_leaders_comm(leader_comm);
385     this->set_intra_comm(comm_intra);
386
387     // create intracommunicator
388   }else{
389     for (i=0; i< leader_group_size;i++)
390       leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
391
392     if(this->get_leaders_comm()==MPI_COMM_NULL){
393       leader_comm = new  Comm(leaders_group, nullptr,1);
394       this->set_leaders_comm(leader_comm);
395     }else{
396       leader_comm=this->get_leaders_comm();
397       Group::unref(leaders_group);
398     }
399     smpi_process()->set_comm_intra(comm_intra);
400   }
401
402   // Are the nodes uniform ? = same number of process/node
403   int my_local_size=comm_intra->size();
404   if(comm_intra->rank()==0) {
405     int is_uniform       = 1;
406     int* non_uniform_map = xbt_new0(int,leader_group_size);
407     Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
408         non_uniform_map, 1, MPI_INT, leader_comm);
409     for(i=0; i < leader_group_size; i++) {
410       if(non_uniform_map[0] != non_uniform_map[i]) {
411         is_uniform = 0;
412         break;
413       }
414     }
415     if(is_uniform==0 && this->is_uniform()!=0){
416       non_uniform_map_ = non_uniform_map;
417     }else{
418       xbt_free(non_uniform_map);
419     }
420     is_uniform_=is_uniform;
421   }
422   Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
423
424   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
425     // we need to switch as the called function may silently touch global variables
426     smpi_switch_data_segment(simgrid::s4u::Actor::self());
427   }
428   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
429   int is_blocked=1;
430   int prev=this->group()->rank(comm_intra->group()->actor(0));
431   for (i = 1; i < my_local_size; i++) {
432     int that = this->group()->rank(comm_intra->group()->actor(i));
433     if (that != prev + 1) {
434       is_blocked = 0;
435       break;
436     }
437     prev = that;
438   }
439
440   int global_blocked;
441   Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
442
443   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
444     if(this->rank()==0){
445       is_blocked_ = global_blocked;
446     }
447   }else{
448     is_blocked_=global_blocked;
449   }
450   delete[] leader_list;
451
452   if(replaying)
453     smpi_process()->set_replaying(true);
454 }
455
456 MPI_Comm Comm::f2c(int id) {
457   if(id == -2) {
458     return MPI_COMM_SELF;
459   } else if(id==0){
460     return MPI_COMM_WORLD;
461   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
462     char key[KEY_SIZE];
463     const auto& lookup = F2C::f2c_lookup();
464     auto comm          = lookup->find(get_key_id(key, id));
465     return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
466   } else {
467     return MPI_COMM_NULL;
468   }
469 }
470
471 void Comm::free_f(int id) {
472   char key[KEY_SIZE];
473   F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
474 }
475
476 int Comm::add_f() {
477   if(F2C::f2c_lookup()==nullptr){
478     F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
479   }
480   char key[KEY_SIZE];
481   (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
482   f2c_id_increment();
483   return F2C::f2c_id()-1;
484 }
485
486 void Comm::add_rma_win(MPI_Win win){
487   rma_wins_.push_back(win);
488 }
489
490 void Comm::remove_rma_win(MPI_Win win){
491   rma_wins_.remove(win);
492 }
493
494 void Comm::finish_rma_calls(){
495   for (auto const& it : rma_wins_) {
496     if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
497       int finished = it->finish_comms();
498       XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
499     }
500   }
501 }
502
503 MPI_Comm Comm::split_type(int type, int key, MPI_Info info)
504 {
505   if(type != MPI_COMM_TYPE_SHARED){
506     return MPI_COMM_NULL;
507   }
508   this->init_smp();
509   this->ref();
510   this->get_intra_comm()->ref();
511   return this->get_intra_comm();
512 }
513
514 }
515 }
516
517