Logo AND Algorithmique Numérique Distribuée

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