Logo AND Algorithmique Numérique Distribuée

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