Logo AND Algorithmique Numérique Distribuée

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