Logo AND Algorithmique Numérique Distribuée

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