Logo AND Algorithmique Numérique Distribuée

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