Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use plain pointers in internal structures, and hopefully fix more memory leaks.
[simgrid.git] / src / smpi / mpi / smpi_group.cpp
1 /* Copyright (c) 2010-2019. 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 Group::Group(Group* origin)
21 {
22   if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
23     size_              = origin->size();
24     // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
25     index_to_rank_map_ = origin->index_to_rank_map_;
26     rank_to_actor_map_ = origin->rank_to_actor_map_;
27     actor_to_rank_map_ = origin->actor_to_rank_map_;
28   }
29 }
30
31 void Group::set_mapping(s4u::ActorPtr actor_ptr, int rank)
32 {
33   s4u::Actor* actor = actor_ptr.get();
34   if (0 <= rank && rank < size_) {
35     int index                = actor->get_pid();
36     if (index != MPI_UNDEFINED) {
37       if ((unsigned)index >= index_to_rank_map_.size())
38         index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
39       index_to_rank_map_[index] = rank;
40     }
41
42     rank_to_actor_map_[rank] = actor;
43     actor_to_rank_map_.insert({actor, rank});
44   }
45 }
46
47 int Group::rank(int index)
48 {
49   int rank;
50   if (0 <= index && (unsigned)index < index_to_rank_map_.size())
51     rank = index_to_rank_map_[index];
52   else
53     rank = MPI_UNDEFINED;
54
55   return rank;
56 }
57
58 s4u::ActorPtr Group::actor(int rank)
59 {
60   if (0 <= rank && rank < size_)
61     return rank_to_actor_map_[rank];
62   else
63     return nullptr;
64 }
65
66 int Group::rank(s4u::ActorPtr actor)
67 {
68   auto iterator = actor_to_rank_map_.find(actor.get());
69   return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
70 }
71
72 void Group::ref()
73 {
74   refcount_++;
75 }
76
77 void Group::unref(Group* group)
78 {
79   group->refcount_--;
80   if (group->refcount_ <= 0) {
81     delete group;
82   }
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       s4u::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, const 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       s4u::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     s4u::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       s4u::ActorPtr actor1 = this->actor(i);
145       (*newgroup)->set_mapping(actor1, i);
146     }
147     for (int i = size2; i < size1; i++) {
148       s4u::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     s4u::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       s4u::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     s4u::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       s4u::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, const 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       s4u::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       s4u::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         s4u::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 }