Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
hide this from users
[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 xbt_dict_t smpi_comm_keyvals = nullptr;
22 int comm_keyval_id = 0;//avoid collisions
23
24
25  Comm mpi_MPI_COMM_UNINITIALIZED;
26 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
27
28 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
29  * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
30
31 static int smpi_compare_rankmap(const void *a, const void *b)
32 {
33   const int* x = static_cast<const int*>(a);
34   const int* y = static_cast<const int*>(b);
35
36   if (x[1] < y[1]) {
37     return -1;
38   }
39   if (x[1] == y[1]) {
40     if (x[0] < y[0]) {
41       return -1;
42     }
43     if (x[0] == y[0]) {
44       return 0;
45     }
46     return 1;
47   }
48   return 1;
49 }
50
51 namespace simgrid{
52 namespace smpi{
53
54 Comm::Comm(){}
55
56 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
57 {
58   refcount_=1;
59   topoType_ = MPI_INVALID_TOPO;
60   intra_comm_ = MPI_COMM_NULL;
61   leaders_comm_ = MPI_COMM_NULL;
62   is_uniform_=1;
63   non_uniform_map_ = nullptr;
64   leaders_map_ = nullptr;
65   is_blocked_=0;
66   attributes_=nullptr;
67 }
68
69 void Comm::destroy(Comm* comm)
70 {
71   if (comm == MPI_COMM_UNINITIALIZED){
72     Comm::destroy(smpi_process_comm_world());
73     return;
74   }
75   delete comm->topo_; // there's no use count on topos
76   Comm::unref(comm);
77 }
78
79 int Comm::dup(MPI_Comm* newcomm){
80   if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
81      smpi_switch_data_segment(smpi_process_index());
82    }
83   MPI_Group cp = new  Group(this->group());
84   (*newcomm) = new  Comm(cp, this->topo());
85   int ret = MPI_SUCCESS;
86
87   if(attributes_ !=nullptr){
88     (*newcomm)->attributes_   = xbt_dict_new_homogeneous(nullptr);
89     xbt_dict_cursor_t cursor = nullptr;
90     char* key;
91     int flag;
92     void* value_in;
93     void* value_out;
94     xbt_dict_foreach (attributes_, cursor, key, value_in) {
95       smpi_comm_key_elem elem =
96           static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
97       if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
98         ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
99         if (ret != MPI_SUCCESS) {
100           Comm::destroy(*newcomm);
101           *newcomm = MPI_COMM_NULL;
102           xbt_dict_cursor_free(&cursor);
103           return ret;
104         }
105         if (flag)
106           xbt_dict_set_ext((*newcomm)->attributes_, key, sizeof(int), value_out, nullptr);
107       }
108       }
109     }
110   return ret;
111 }
112
113 MPI_Group Comm::group()
114 {
115   if (this == MPI_COMM_UNINITIALIZED)
116     return smpi_process_comm_world()->group();
117   return group_;
118 }
119
120 MPI_Topology Comm::topo() {
121   return topo_;
122 }
123
124 int Comm::size()
125 {
126   if (this == MPI_COMM_UNINITIALIZED)
127     return smpi_process_comm_world()->size();
128   return group_->size();
129 }
130
131 int Comm::rank()
132 {
133   if (this == MPI_COMM_UNINITIALIZED)
134     return smpi_process_comm_world()->rank();
135   return group_->rank(smpi_process_index());
136 }
137
138 void Comm::get_name (char* name, int* len)
139 {
140   if (this == MPI_COMM_UNINITIALIZED){
141     smpi_process_comm_world()->get_name(name, len);
142     return;
143   }
144   if(this == MPI_COMM_WORLD) {
145     strncpy(name, "WORLD",5);
146     *len = 5;
147   } else {
148     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
149   }
150 }
151
152 void Comm::set_leaders_comm(MPI_Comm leaders){
153   if (this == MPI_COMM_UNINITIALIZED){
154     smpi_process_comm_world()->set_leaders_comm(leaders);
155     return;
156   }
157   leaders_comm_=leaders;
158 }
159
160 void Comm::set_intra_comm(MPI_Comm leaders){
161   intra_comm_=leaders;
162 }
163
164 int* Comm::get_non_uniform_map(){
165   if (this == MPI_COMM_UNINITIALIZED)
166     return smpi_process_comm_world()->get_non_uniform_map();
167   return non_uniform_map_;
168 }
169
170 int* Comm::get_leaders_map(){
171   if (this == MPI_COMM_UNINITIALIZED)
172     return smpi_process_comm_world()->get_leaders_map();
173   return leaders_map_;
174 }
175
176 MPI_Comm Comm::get_leaders_comm(){
177   if (this == MPI_COMM_UNINITIALIZED)
178     return smpi_process_comm_world()->get_leaders_comm();
179   return leaders_comm_;
180 }
181
182 MPI_Comm Comm::get_intra_comm(){
183   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD) 
184     return smpi_process_get_comm_intra();
185   else return intra_comm_;
186 }
187
188 int Comm::is_uniform(){
189   if (this == MPI_COMM_UNINITIALIZED)
190     return smpi_process_comm_world()->is_uniform();
191   return is_uniform_;
192 }
193
194 int Comm::is_blocked(){
195   if (this == MPI_COMM_UNINITIALIZED)
196     return smpi_process_comm_world()->is_blocked();
197   return is_blocked_;
198 }
199
200 MPI_Comm Comm::split(int color, int key)
201 {
202   if (this == MPI_COMM_UNINITIALIZED)
203     return smpi_process_comm_world()->split(color, key);
204   int system_tag = 123;
205   int* recvbuf;
206
207   MPI_Group group_root = nullptr;
208   MPI_Group group_out  = nullptr;
209   MPI_Group group      = this->group();
210   int rank             = this->rank();
211   int size             = this->size();
212   /* Gather all colors and keys on rank 0 */
213   int* sendbuf = xbt_new(int, 2);
214   sendbuf[0] = color;
215   sendbuf[1] = key;
216   if(rank == 0) {
217     recvbuf = xbt_new(int, 2 * size);
218   } else {
219     recvbuf = nullptr;
220   }
221   Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
222   xbt_free(sendbuf);
223   /* Do the actual job */
224   if(rank == 0) {
225     MPI_Group* group_snd = xbt_new(MPI_Group, size);
226     int* rankmap         = xbt_new(int, 2 * size);
227     for (int i = 0; i < size; i++) {
228       if (recvbuf[2 * i] != MPI_UNDEFINED) {
229         int count = 0;
230         for (int j = i + 1; j < size; j++) {
231           if(recvbuf[2 * i] == recvbuf[2 * j]) {
232             recvbuf[2 * j] = MPI_UNDEFINED;
233             rankmap[2 * count] = j;
234             rankmap[2 * count + 1] = recvbuf[2 * j + 1];
235             count++;
236           }
237         }
238         /* Add self in the group */
239         recvbuf[2 * i] = MPI_UNDEFINED;
240         rankmap[2 * count] = i;
241         rankmap[2 * count + 1] = recvbuf[2 * i + 1];
242         count++;
243         qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
244         group_out = new  Group(count);
245         if (i == 0) {
246           group_root = group_out; /* Save root's group */
247         }
248         for (int j = 0; j < count; j++) {
249           int index = group->index(rankmap[2 * j]);
250           group_out->set_mapping(index, j);
251         }
252         MPI_Request* requests = xbt_new(MPI_Request, count);
253         int reqs              = 0;
254         for (int j = 0; j < count; j++) {
255           if(rankmap[2 * j] != 0) {
256             group_snd[reqs]=new  Group(group_out);
257             requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
258             reqs++;
259           }
260         }
261         if(i != 0) {
262           if(group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
263             Group::unref(group_out);
264         }
265         Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
266         xbt_free(requests);
267       }
268     }
269     xbt_free(recvbuf);
270     xbt_free(rankmap);
271     xbt_free(group_snd);
272     group_out = group_root; /* exit with root's group */
273   } else {
274     if(color != MPI_UNDEFINED) {
275       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
276     } /* otherwise, exit with group_out == nullptr */
277   }
278   return group_out!=nullptr ? new  Comm(group_out, nullptr) : MPI_COMM_NULL;
279 }
280
281 void Comm::ref(){
282   if (this == MPI_COMM_UNINITIALIZED){
283     smpi_process_comm_world()->ref();
284     return;
285   }
286   group_->ref();
287   refcount_++;
288 }
289
290 void Comm::cleanup_attributes(){
291   if(attributes_ !=nullptr){
292     xbt_dict_cursor_t cursor = nullptr;
293     char* key;
294     void* value;
295     int flag;
296     xbt_dict_foreach (attributes_, cursor, key, value) {
297       smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
298       if (elem != nullptr && elem->delete_fn != nullptr)
299         elem->delete_fn(this, atoi(key), value, &flag);
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_comm_key_elem elem =
504      static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
505   if(elem==nullptr)
506     return MPI_ERR_ARG;
507   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
508     void* value = nullptr;
509     int flag;
510     if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
511       int ret = elem->delete_fn(this, keyval, value, &flag);
512       if(ret!=MPI_SUCCESS) 
513         return ret;
514     }
515   }
516   if(attributes_==nullptr)
517     return MPI_ERR_ARG;
518
519   xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
520   return MPI_SUCCESS;
521 }
522
523 int Comm::attr_get(int keyval, void* attr_value, int* flag){
524   smpi_comm_key_elem elem =
525     static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
526   if(elem==nullptr)
527     return MPI_ERR_ARG;
528   if(attributes_==nullptr){
529     *flag=0;
530     return MPI_SUCCESS;
531   }
532   try {
533     *static_cast<void**>(attr_value) =
534         xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
535     *flag=1;
536   }
537   catch (xbt_ex& ex) {
538     *flag=0;
539   }
540   return MPI_SUCCESS;
541 }
542
543 int Comm::attr_put(int keyval, void* attr_value){
544   if(smpi_comm_keyvals==nullptr)
545     smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
546   smpi_comm_key_elem elem =
547     static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(&keyval), sizeof(int)));
548   if(elem==nullptr)
549     return MPI_ERR_ARG;
550   int flag;
551   void* value = nullptr;
552   this->attr_get(keyval, &value, &flag);
553   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
554     int ret = elem->delete_fn(this, keyval, value, &flag);
555     if(ret!=MPI_SUCCESS) 
556       return ret;
557   }
558   if(attributes_==nullptr)
559     attributes_ = xbt_dict_new_homogeneous(nullptr);
560
561   xbt_dict_set_ext(attributes_,  reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
562   return MPI_SUCCESS;
563 }
564
565 MPI_Comm Comm::f2c(int id) {
566   if(id == -2) {
567     return MPI_COMM_SELF;
568   } else if(id==0){
569     return MPI_COMM_WORLD;
570   } else if(Comm::f2c_lookup_ != nullptr && id >= 0) {
571       char key[KEY_SIZE];
572       MPI_Comm tmp =  static_cast<MPI_Comm>(xbt_dict_get_or_null(Comm::f2c_lookup_,get_key_id(key, id)));
573       return tmp != nullptr ? tmp : MPI_COMM_NULL ;
574   } else {
575     return MPI_COMM_NULL;
576   }
577 }
578
579 void Comm::free_f(int id) {
580   char key[KEY_SIZE];
581   xbt_dict_remove(Comm::f2c_lookup_, id==0? get_key(key, id) : get_key_id(key, id));
582 }
583
584 int Comm::add_f() {
585   if(Comm::f2c_lookup_==nullptr){
586     Comm::f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
587   }
588   char key[KEY_SIZE];
589   xbt_dict_set(Comm::f2c_lookup_, this==MPI_COMM_WORLD? get_key(key, Comm::f2c_id_) : get_key_id(key,Comm::f2c_id_), this, nullptr);
590   Comm::f2c_id_++;
591   return Comm::f2c_id_-1;
592 }
593
594
595 }
596 }
597
598 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
599                             void* extra_state){
600   if(smpi_comm_keyvals==nullptr)
601     smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
602
603   smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
604
605   value->copy_fn=copy_fn;
606   value->delete_fn=delete_fn;
607
608   *keyval = comm_keyval_id;
609   xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
610   comm_keyval_id++;
611   return MPI_SUCCESS;
612 }
613
614 int smpi_comm_keyval_free(int* keyval){
615   smpi_comm_key_elem elem =
616      static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(keyval), sizeof(int)));
617   if(elem==nullptr)
618     return MPI_ERR_ARG;
619   xbt_dict_remove_ext(smpi_comm_keyvals,  reinterpret_cast<const char*>(keyval), sizeof(int));
620   xbt_free(elem);
621   return MPI_SUCCESS;
622 }