Logo AND Algorithmique Numérique Distribuée

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