Logo AND Algorithmique Numérique Distribuée

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