Logo AND Algorithmique Numérique Distribuée

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