Logo AND Algorithmique Numérique Distribuée

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