Logo AND Algorithmique Numérique Distribuée

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