1 /* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved. */
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. */
6 #include "simgrid/s4u/Actor.hpp"
7 #include "smpi_group.hpp"
8 #include "smpi_comm.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
14 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
15 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
20 Group::Group(Group* origin)
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_;
31 void Group::set_mapping(s4u::ActorPtr actor, int rank)
33 if (0 <= rank && rank < size_) {
34 int index = actor->get_pid();
35 if (index != MPI_UNDEFINED) {
36 if ((unsigned)index >= index_to_rank_map_.size())
37 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
38 index_to_rank_map_[index] = rank;
41 rank_to_actor_map_[rank] = actor;
42 if (actor != nullptr) {
43 actor_to_rank_map_.insert({actor, rank});
48 int Group::rank(int index)
51 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
52 rank = index_to_rank_map_[index];
59 s4u::ActorPtr Group::actor(int rank)
61 if (0 <= rank && rank < size_)
62 return rank_to_actor_map_[rank];
67 int Group::rank(const s4u::ActorPtr actor)
69 auto iterator = actor_to_rank_map_.find(actor);
70 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
78 void Group::unref(Group* group)
81 if (group->refcount_ <= 0) {
86 int Group::compare(MPI_Group group2)
91 if (size_ != group2->size()) {
94 for (int i = 0; i < size_; i++) {
95 s4u::ActorPtr actor = this->actor(i);
96 int rank = group2->rank(actor);
97 if (rank == MPI_UNDEFINED) {
102 result = MPI_SIMILAR;
109 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
113 *newgroup = MPI_GROUP_EMPTY;
114 } else if (n == size_) {
116 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
119 *newgroup = new Group(n);
120 for (i = 0; i < n; i++) {
121 s4u::ActorPtr actor = this->actor(ranks[i]); // ranks[] was passed as a param!
122 (*newgroup)->set_mapping(actor, i);
128 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
131 int size2 = group2->size();
132 for (int i = 0; i < size2; i++) {
133 s4u::ActorPtr actor = group2->actor(i);
134 int proc1 = this->rank(actor);
135 if (proc1 == MPI_UNDEFINED) {
140 *newgroup = MPI_GROUP_EMPTY;
142 *newgroup = new Group(size1);
143 size2 = this->size();
144 for (int i = 0; i < size2; i++) {
145 s4u::ActorPtr actor1 = this->actor(i);
146 (*newgroup)->set_mapping(actor1, i);
148 for (int i = size2; i < size1; i++) {
149 s4u::ActorPtr actor = group2->actor(i - size2);
150 (*newgroup)->set_mapping(actor, i);
156 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
158 int size2 = group2->size();
159 for (int i = 0; i < size2; i++) {
160 s4u::ActorPtr actor = group2->actor(i);
161 int proc1 = this->rank(actor);
162 if (proc1 == MPI_UNDEFINED) {
167 *newgroup = MPI_GROUP_EMPTY;
169 *newgroup = new Group(size2);
171 for (int i = 0; i < group2->size(); i++) {
172 s4u::ActorPtr actor = group2->actor(i);
173 int proc1 = this->rank(actor);
174 if (proc1 != MPI_UNDEFINED) {
175 (*newgroup)->set_mapping(actor, j);
183 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
187 for (int i = 0; i < size2; i++) {
188 s4u::ActorPtr actor = this->actor(i);
189 int proc2 = group2->rank(actor);
190 if (proc2 != MPI_UNDEFINED) {
195 *newgroup = MPI_GROUP_EMPTY;
197 *newgroup = new Group(newsize);
198 for (int i = 0; i < size2; i++) {
199 s4u::ActorPtr actor = this->actor(i);
200 int proc2 = group2->rank(actor);
201 if (proc2 == MPI_UNDEFINED) {
202 (*newgroup)->set_mapping(actor, i);
209 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
211 int newsize = oldsize - n;
212 *newgroup = new Group(newsize);
213 int* to_exclude = new int[size_];
214 for (int i = 0; i < oldsize; i++)
216 for (int i = 0; i < n; i++)
217 to_exclude[ranks[i]]=1;
219 for (int i = 0; i < oldsize; i++) {
220 if(to_exclude[i]==0){
221 s4u::ActorPtr actor = this->actor(i);
222 (*newgroup)->set_mapping(actor, j);
231 static bool is_rank_in_range(int rank, int first, int last)
239 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
241 for (int i = 0; i < n; i++) {
242 for (int rank = ranges[i][0]; /* First */
243 rank >= 0 && rank < size_; /* Last */
246 if(rank == ranges[i][1]){/*already last ?*/
249 rank += ranges[i][2]; /* Stride */
250 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
254 *newgroup = new Group(newsize);
256 for (int i = 0; i < n; i++) {
257 for (int rank = ranges[i][0]; /* First */
258 rank >= 0 && rank < size_; /* Last */
260 s4u::ActorPtr actor = this->actor(rank);
261 (*newgroup)->set_mapping(actor, j);
263 if(rank == ranges[i][1]){/*already last ?*/
266 rank += ranges[i][2]; /* Stride */
267 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
274 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
276 for (int i = 0; i < n; i++) {
277 for (int rank = ranges[i][0]; /* First */
278 rank >= 0 && rank < size_; /* Last */
281 if(rank == ranges[i][1]){/*already last ?*/
284 rank += ranges[i][2]; /* Stride */
285 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
290 *newgroup = MPI_GROUP_EMPTY;
292 *newgroup = new Group(newsize);
295 while (newrank < newsize) {
297 for (int i = 0; i < n; i++) {
298 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
303 if(rank == ranges[i][1]){/*already last ?*/
306 rank += ranges[i][2]; /* Stride */
307 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
312 s4u::ActorPtr actor = this->actor(oldrank);
313 (*newgroup)->set_mapping(actor, newrank);
322 MPI_Group Group::f2c(int id) {
324 return MPI_GROUP_EMPTY;
325 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
327 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
329 return static_cast<MPI_Group>(MPI_GROUP_NULL);