Logo AND Algorithmique Numérique Distribuée

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