Logo AND Algorithmique Numérique Distribuée

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