Logo AND Algorithmique Numérique Distribuée

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