Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c4cd35377903b3e837df047de427844226c6b94
[simgrid.git] / src / smpi / smpi_group.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 "src/smpi/smpi_comm.hpp"
7 #include "src/smpi/smpi_group.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
10
11 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
12 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
13
14 namespace simgrid{
15 namespace smpi{
16
17 Group::Group()
18 {
19   size_=0;                            /* size */
20   rank_to_index_map_=nullptr;                         /* rank_to_index_map_ */
21   index_to_rank_map_=nullptr;                         /* index_to_rank_map_ */
22   refcount_=1;                            /* refcount_: start > 0 so that this group never gets freed */
23 }
24
25 Group::Group(int n) : size_(n)
26 {
27   rank_to_index_map_ = xbt_new(int, size_);
28   index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
29   refcount_ = 1;
30   for (int i = 0; i < size_; i++) {
31     rank_to_index_map_[i] = MPI_UNDEFINED;
32   }
33 }
34
35 Group::Group(MPI_Group origin)
36 {
37   if(origin != MPI_GROUP_NULL
38             && origin != MPI_GROUP_EMPTY)
39     {
40       size_ = origin->size();
41       rank_to_index_map_ = xbt_new(int, size_);
42       index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
43       refcount_ = 1;
44       for (int i = 0; i < size_; i++) {
45         rank_to_index_map_[i] = origin->rank_to_index_map_[i];
46       }
47
48       char* key;
49       char* ptr_rank;
50       xbt_dict_cursor_t cursor = nullptr;
51       xbt_dict_foreach(origin->index_to_rank_map_, cursor, key, ptr_rank) {
52         int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
53         *cp=*reinterpret_cast<int*>(ptr_rank);
54         xbt_dict_set(index_to_rank_map_, key, cp, nullptr);
55       }
56     }
57 }
58
59 Group::~Group()
60 {
61   xbt_free(rank_to_index_map_);
62   xbt_dict_free(&index_to_rank_map_);
63 }
64
65 void Group::set_mapping(int index, int rank)
66 {
67   int * val_rank;
68
69   if (rank < size_) {
70     rank_to_index_map_[rank] = index;
71     if (index!=MPI_UNDEFINED ) {
72       val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
73       *val_rank = rank;
74
75       char * key = bprintf("%d", index);
76       xbt_dict_set(index_to_rank_map_, key, val_rank, nullptr);
77       xbt_free(key);
78     }
79   }
80 }
81
82 int Group::index(int rank)
83 {
84   int index = MPI_UNDEFINED;
85
86   if (0 <= rank && rank < size_) {
87     index = rank_to_index_map_[rank];
88   }
89   return index;
90 }
91
92 int Group::rank(int index)
93 {
94   int * ptr_rank = nullptr;
95   if (this==MPI_GROUP_EMPTY)
96     return MPI_UNDEFINED;
97   char * key = bprintf("%d", index);
98   ptr_rank = static_cast<int*>(xbt_dict_get_or_null(index_to_rank_map_, key));
99   xbt_free(key);
100
101   if (ptr_rank==nullptr)
102     return MPI_UNDEFINED;
103   return *ptr_rank;
104 }
105
106 void Group::ref()
107 {
108   refcount_++;
109 }
110
111 void Group::unref(Group* group)
112 {
113   group->refcount_--;
114   if (group->refcount_ <= 0) {
115     delete group;
116   }
117 }
118
119 int Group::size()
120 {
121   return size_;
122 }
123
124 int Group::compare(MPI_Group group2)
125 {
126   int result;
127   int i;
128   int index;
129
130   result = MPI_IDENT;
131   if (size_ != group2->size()) {
132     result = MPI_UNEQUAL;
133   } else {
134     int sz = group2->size();
135     for (i = 0; i < sz; i++) {
136       index = this->index(i);
137       int rank = group2->rank(index);
138       if (rank == MPI_UNDEFINED) {
139         result = MPI_UNEQUAL;
140         break;
141       }
142       if (rank != i) {
143         result = MPI_SIMILAR;
144       }
145     }
146   }
147   return result;
148 }
149
150 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
151 {
152   int i=0;
153   int index=0;
154   if (n == 0) {
155     *newgroup = MPI_GROUP_EMPTY;
156   } else if (n == size_) {
157     *newgroup = this;
158     if(this!= MPI_COMM_WORLD->group()
159               && this != MPI_COMM_SELF->group()
160               && this != MPI_GROUP_EMPTY)
161     this->ref();
162   } else {
163     *newgroup = new Group(n);
164     for (i = 0; i < n; i++) {
165       index = this->index(ranks[i]);
166       (*newgroup)->set_mapping(index, i);
167     }
168   }
169   return MPI_SUCCESS;
170 }
171
172 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
173 {
174   int size1 = size_;
175   int size2 = group2->size();
176   for (int i = 0; i < size2; i++) {
177     int proc2 = group2->index(i);
178     int proc1 = this->rank(proc2);
179     if (proc1 == MPI_UNDEFINED) {
180       size1++;
181     }
182   }
183   if (size1 == 0) {
184     *newgroup = MPI_GROUP_EMPTY;
185   } else {
186     *newgroup = new  Group(size1);
187     size2 = this->size();
188     for (int i = 0; i < size2; i++) {
189       int proc1 = this->index(i);
190       (*newgroup)->set_mapping(proc1, i);
191     }
192     for (int i = size2; i < size1; i++) {
193       int proc2 = group2->index(i - size2);
194       (*newgroup)->set_mapping(proc2, i);
195     }
196   }
197   return MPI_SUCCESS;
198 }
199
200 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
201 {
202   int size2 = group2->size();
203   for (int i = 0; i < size2; i++) {
204     int proc2 = group2->index(i);
205     int proc1 = this->rank(proc2);
206     if (proc1 == MPI_UNDEFINED) {
207       size2--;
208     }
209   }
210   if (size2 == 0) {
211     *newgroup = MPI_GROUP_EMPTY;
212   } else {
213     *newgroup = new  Group(size2);
214     int j=0;
215     for (int i = 0; i < group2->size(); i++) {
216       int proc2 = group2->index(i);
217       int proc1 = this->rank(proc2);
218       if (proc1 != MPI_UNDEFINED) {
219         (*newgroup)->set_mapping(proc2, j);
220         j++;
221       }
222     }
223   }
224   return MPI_SUCCESS;
225 }
226
227 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
228 {
229   int newsize = size_;
230   int size2 = size_;
231   for (int i = 0; i < size2; i++) {
232     int proc1 = this->index(i);
233     int proc2 = group2->rank(proc1);
234     if (proc2 != MPI_UNDEFINED) {
235       newsize--;
236     }
237   }
238   if (newsize == 0) {
239     *newgroup = MPI_GROUP_EMPTY;
240   } else {
241     *newgroup = new  Group(newsize);
242     for (int i = 0; i < size2; i++) {
243       int proc1 = this->index(i);
244       int proc2 = group2->rank(proc1);
245       if (proc2 == MPI_UNDEFINED) {
246         (*newgroup)->set_mapping(proc1, i);
247       }
248     }
249   }
250   return MPI_SUCCESS;
251 }
252
253 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
254   int oldsize = size_;
255   int newsize = oldsize - n;
256   *newgroup = new  Group(newsize);
257   int* to_exclude=xbt_new0(int, size_);
258   for (int i     = 0; i < oldsize; i++)
259     to_exclude[i]=0;
260   for (int i            = 0; i < n; i++)
261     to_exclude[ranks[i]]=1;
262   int j = 0;
263   for (int i = 0; i < oldsize; i++) {
264     if(to_exclude[i]==0){
265       int index = this->index(i);
266       (*newgroup)->set_mapping(index, j);
267       j++;
268     }
269   }
270   xbt_free(to_exclude);
271   return MPI_SUCCESS;
272
273 }
274
275 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
276   int newsize = 0;
277   for (int i = 0; i < n; i++) {
278     for (int rank = ranges[i][0];                    /* First */
279          rank >= 0 && rank < size_; /* Last */
280          ) {
281       newsize++;
282       if(rank == ranges[i][1]){/*already last ?*/
283         break;
284       }
285       rank += ranges[i][2]; /* Stride */
286       if (ranges[i][0] < ranges[i][1]) {
287         if (rank > ranges[i][1])
288           break;
289       } else {
290         if (rank < ranges[i][1])
291           break;
292       }
293     }
294   }
295   *newgroup = new  Group(newsize);
296   int j     = 0;
297   for (int i = 0; i < n; i++) {
298     for (int rank = ranges[i][0];                    /* First */
299          rank >= 0 && rank < size_; /* Last */
300          ) {
301       int index = this->index(rank);
302       (*newgroup)->set_mapping(index, j);
303       j++;
304       if(rank == ranges[i][1]){/*already last ?*/
305         break;
306       }
307       rank += ranges[i][2]; /* Stride */
308       if (ranges[i][0] < ranges[i][1]) {
309         if (rank > ranges[i][1])
310           break;
311       } else {
312         if (rank < ranges[i][1])
313           break;
314       }
315     }
316   }
317   return MPI_SUCCESS;
318 }
319
320 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
321   int newsize = size_;
322   for (int i = 0; i < n; i++) {
323     for (int rank = ranges[i][0];                    /* First */
324          rank >= 0 && rank < size_; /* Last */
325          ) {
326       newsize--;
327       if(rank == ranges[i][1]){/*already last ?*/
328         break;
329       }
330       rank += ranges[i][2]; /* Stride */
331       if (ranges[i][0] < ranges[i][1]) {
332         if (rank > ranges[i][1])
333           break;
334       } else {
335         if (rank < ranges[i][1])
336           break;
337       }
338     }
339   }
340   if (newsize == 0) {
341     *newgroup = MPI_GROUP_EMPTY;
342   } else {
343     *newgroup = new  Group(newsize);
344     int newrank = 0;
345     int oldrank = 0;
346     while (newrank < newsize) {
347       int add = 1;
348       for (int i = 0; i < n; i++) {
349         for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
350           if(rank==oldrank){
351             add = 0;
352             break;
353           }
354           if(rank == ranges[i][1]){/*already last ?*/
355             break;
356           }
357           rank += ranges[i][2]; /* Stride */
358           if (ranges[i][0]<ranges[i][1]){
359             if (rank > ranges[i][1])
360               break;
361           }else{
362             if (rank < ranges[i][1])
363               break;
364           }
365         }
366       }
367       if(add==1){
368         int index = this->index(oldrank);
369         (*newgroup)->set_mapping(index, newrank);
370         newrank++;
371       }
372       oldrank++;
373     }
374   }
375   return MPI_SUCCESS;
376 }
377
378 MPI_Group Group::f2c(int id) {
379   if(id == -2) {
380     return MPI_GROUP_EMPTY;
381   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
382     char key[KEY_SIZE];
383     return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
384   } else {
385     return static_cast<MPI_Group>(MPI_GROUP_NULL);
386   }
387 }
388
389 }
390 }