Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
generalize previous change to other MPI predefined globals.
[simgrid.git] / src / smpi / mpi / smpi_group.cpp
1 /* Copyright (c) 2010-2021. 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
11 simgrid::smpi::Group smpi_MPI_GROUP_EMPTY;
12 extern XBT_PRIVATE MPI_Comm MPI_COMM_UNINITIALIZED;
13
14 namespace simgrid{
15 namespace smpi{
16
17 Group::Group(const Group* origin)
18 {
19   if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
20     size_              = origin->size();
21     // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
22     index_to_rank_map_ = origin->index_to_rank_map_;
23     rank_to_actor_map_ = origin->rank_to_actor_map_;
24     actor_to_rank_map_ = origin->actor_to_rank_map_;
25   }
26 }
27
28 void Group::set_mapping(s4u::Actor* actor, int rank)
29 {
30   if (0 <= rank && rank < size_) {
31     int index                = actor->get_pid();
32     if (index != MPI_UNDEFINED) {
33       if ((unsigned)index >= index_to_rank_map_.size())
34         index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
35       index_to_rank_map_[index] = rank;
36     }
37
38     rank_to_actor_map_[rank] = actor;
39     actor_to_rank_map_.insert({actor, rank});
40   }
41 }
42
43 int Group::rank(int index)
44 {
45   int rank;
46   if (0 <= index && (unsigned)index < index_to_rank_map_.size())
47     rank = index_to_rank_map_[index];
48   else
49     rank = MPI_UNDEFINED;
50
51   return rank;
52 }
53
54 s4u::Actor* Group::actor(int rank)
55 {
56   if (0 <= rank && rank < size_)
57     return rank_to_actor_map_[rank];
58   else
59     return nullptr;
60 }
61
62 int Group::rank(s4u::Actor* actor)
63 {
64   auto iterator = actor_to_rank_map_.find(actor);
65   //I'm not in the communicator ... but maybe my parent is ?
66   if (iterator == actor_to_rank_map_.end())
67     iterator = actor_to_rank_map_.find(s4u::Actor::by_pid(actor->get_ppid()).get());
68   return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
69 }
70
71 void Group::ref()
72 {
73   refcount_++;
74 }
75
76 void Group::unref(Group* group)
77 {
78   group->refcount_--;
79   if (group->refcount_ <= 0) {
80     if (simgrid::smpi::F2C::lookup() != nullptr)
81       F2C::free_f(group->c2f());
82     delete group;
83   }
84 }
85
86 int Group::compare(MPI_Group group2)
87 {
88   int result;
89
90   result = MPI_IDENT;
91   if (size_ != group2->size()) {
92     result = MPI_UNEQUAL;
93   } else {
94     for (int i = 0; i < size_; i++) {
95       s4u::Actor* actor = this->actor(i);
96       int rank = group2->rank(actor);
97       if (rank == MPI_UNDEFINED) {
98         result = MPI_UNEQUAL;
99         break;
100       }
101       if (rank != i) {
102         result = MPI_SIMILAR;
103       }
104     }
105   }
106   return result;
107 }
108
109 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
110 {
111   if (n == 0) {
112     *newgroup = MPI_GROUP_EMPTY;
113   } else {
114     *newgroup = new Group(n);
115     for (int i = 0; i < n; i++) {
116       s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
117       (*newgroup)->set_mapping(actor, i);
118     }
119     if((*newgroup)!=MPI_GROUP_EMPTY)
120       (*newgroup)->add_f();
121   }
122   return MPI_SUCCESS;
123 }
124
125 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
126 {
127   int size1 = size_;
128   int size2 = group2->size();
129   for (int i = 0; i < size2; i++) {
130     s4u::Actor* actor = group2->actor(i);
131     int proc1 = this->rank(actor);
132     if (proc1 == MPI_UNDEFINED) {
133       size1++;
134     }
135   }
136   if (size1 == 0) {
137     *newgroup = MPI_GROUP_EMPTY;
138   } else {
139     *newgroup = new  Group(size1);
140     size2 = this->size();
141     for (int i = 0; i < size2; i++) {
142       s4u::Actor* actor1 = this->actor(i);
143       (*newgroup)->set_mapping(actor1, i);
144     }
145     for (int i = size2; i < size1; i++) {
146       s4u::Actor* actor = group2->actor(i - size2);
147       (*newgroup)->set_mapping(actor, i);
148     }
149     if((*newgroup)!=MPI_GROUP_EMPTY)
150       (*newgroup)->add_f();
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::Actor* 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::Actor* 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     if((*newgroup)!=MPI_GROUP_EMPTY)
179       (*newgroup)->add_f();
180   }
181   return MPI_SUCCESS;
182 }
183
184 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
185 {
186   int newsize = size_;
187   int size2 = size_;
188   for (int i = 0; i < size2; i++) {
189     s4u::Actor* actor = this->actor(i);
190     int proc2 = group2->rank(actor);
191     if (proc2 != MPI_UNDEFINED) {
192       newsize--;
193     }
194   }
195   if (newsize == 0) {
196     *newgroup = MPI_GROUP_EMPTY;
197   } else {
198     *newgroup = new  Group(newsize);
199     for (int i = 0; i < size2; i++) {
200       s4u::Actor* actor = this->actor(i);
201       int proc2 = group2->rank(actor);
202       if (proc2 == MPI_UNDEFINED) {
203         (*newgroup)->set_mapping(actor, i);
204       }
205     }
206     if((*newgroup)!=MPI_GROUP_EMPTY)
207       (*newgroup)->add_f();
208   }
209   return MPI_SUCCESS;
210 }
211
212 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
213   int oldsize = size_;
214   int newsize = oldsize - n;
215   *newgroup = new  Group(newsize);
216   std::vector<bool> to_exclude(size_, false);
217   for (int i = 0; i < n; i++)
218     to_exclude[ranks[i]] = true;
219   int j = 0;
220   for (int i = 0; i < oldsize; i++) {
221     if (not to_exclude[i]) {
222       s4u::Actor* actor = this->actor(i);
223       (*newgroup)->set_mapping(actor, j);
224       j++;
225     }
226   }
227   if((*newgroup)!=MPI_GROUP_EMPTY)
228     (*newgroup)->add_f();
229   return MPI_SUCCESS;
230 }
231
232 static bool is_rank_in_range(int rank, int first, int last)
233 {
234   return (first <= rank && rank <= last) || (first >= rank && rank >= last);
235 }
236
237 int Group::range_incl(int n, int ranges[][3], MPI_Group* newgroup)
238 {
239   std::vector<int> to_incl;
240   for (int i = 0; i < n; i++)
241     for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
242          j += ranges[i][2])
243       to_incl.push_back(j);
244
245   int newsize = static_cast<int>(to_incl.size());
246   *newgroup   = new Group(newsize);
247
248   for (int j = 0; j < newsize; j++) {
249     int rank          = to_incl[j];
250     s4u::Actor* actor = this->actor(rank);
251     (*newgroup)->set_mapping(actor, j);
252   }
253   if((*newgroup)!=MPI_GROUP_EMPTY)
254     (*newgroup)->add_f();
255   return MPI_SUCCESS;
256 }
257
258 int Group::range_excl(int n, int ranges[][3], MPI_Group* newgroup)
259 {
260   std::vector<bool> to_excl(size_, false);
261   int newsize = size_;
262   for (int i = 0; i < n; i++) {
263     for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
264          j += ranges[i][2]) {
265       to_excl[j] = true;
266       newsize--;
267     }
268   }
269   if (newsize == 0) {
270     *newgroup = MPI_GROUP_EMPTY;
271   } else {
272     *newgroup = new Group(newsize);
273
274     int j = 0;
275     for (int rank = 0; rank < size_; rank++) {
276       if (not to_excl[rank]) {
277         s4u::Actor* actor = this->actor(rank);
278         (*newgroup)->set_mapping(actor, j);
279         j++;
280       }
281     }
282   }
283   if((*newgroup)!=MPI_GROUP_EMPTY)
284     (*newgroup)->add_f();
285   return MPI_SUCCESS;
286 }
287
288 MPI_Group Group::f2c(int id) {
289   if(id == -2) {
290     return MPI_GROUP_EMPTY;
291   } else if (F2C::lookup() != nullptr && id >= 0) {
292     return static_cast<MPI_Group>(F2C::lookup()->at(id));
293   } else {
294     return MPI_GROUP_NULL;
295   }
296 }
297
298 }
299 }