Logo AND Algorithmique Numérique Distribuée

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