Logo AND Algorithmique Numérique Distribuée

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