Logo AND Algorithmique Numérique Distribuée

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