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_ptr, int rank)
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;
42 rank_to_actor_map_[rank] = actor;
43 actor_to_rank_map_.insert({actor, rank});
47 int Group::rank(int index)
50 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
51 rank = index_to_rank_map_[index];
58 s4u::ActorPtr Group::actor(int rank)
60 if (0 <= rank && rank < size_)
61 return rank_to_actor_map_[rank];
66 int Group::rank(s4u::ActorPtr actor)
68 auto iterator = actor_to_rank_map_.find(actor.get());
69 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
77 void Group::unref(Group* group)
80 if (group->refcount_ <= 0) {
85 int Group::compare(MPI_Group group2)
90 if (size_ != group2->size()) {
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) {
101 result = MPI_SIMILAR;
108 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
112 *newgroup = MPI_GROUP_EMPTY;
113 } else if (n == size_) {
115 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
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);
127 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
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) {
139 *newgroup = MPI_GROUP_EMPTY;
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);
147 for (int i = size2; i < size1; i++) {
148 s4u::ActorPtr actor = group2->actor(i - size2);
149 (*newgroup)->set_mapping(actor, i);
155 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
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) {
166 *newgroup = MPI_GROUP_EMPTY;
168 *newgroup = new Group(size2);
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);
182 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
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) {
194 *newgroup = MPI_GROUP_EMPTY;
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);
208 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
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++)
215 for (int i = 0; i < n; i++)
216 to_exclude[ranks[i]]=1;
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);
230 static bool is_rank_in_range(int rank, int first, int last)
238 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
240 for (int i = 0; i < n; i++) {
241 for (int rank = ranges[i][0]; /* First */
242 rank >= 0 && rank < size_; /* Last */
245 if(rank == ranges[i][1]){/*already last ?*/
248 rank += ranges[i][2]; /* Stride */
249 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
253 *newgroup = new Group(newsize);
255 for (int i = 0; i < n; i++) {
256 for (int rank = ranges[i][0]; /* First */
257 rank >= 0 && rank < size_; /* Last */
259 s4u::ActorPtr actor = this->actor(rank);
260 (*newgroup)->set_mapping(actor, j);
262 if(rank == ranges[i][1]){/*already last ?*/
265 rank += ranges[i][2]; /* Stride */
266 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
273 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
275 for (int i = 0; i < n; i++) {
276 for (int rank = ranges[i][0]; /* First */
277 rank >= 0 && rank < size_; /* Last */
280 if(rank == ranges[i][1]){/*already last ?*/
283 rank += ranges[i][2]; /* Stride */
284 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
289 *newgroup = MPI_GROUP_EMPTY;
291 *newgroup = new Group(newsize);
294 while (newrank < newsize) {
296 for (int i = 0; i < n; i++) {
297 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
302 if(rank == ranges[i][1]){/*already last ?*/
305 rank += ranges[i][2]; /* Stride */
306 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
311 s4u::ActorPtr actor = this->actor(oldrank);
312 (*newgroup)->set_mapping(actor, newrank);
321 MPI_Group Group::f2c(int id) {
323 return MPI_GROUP_EMPTY;
324 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
326 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
328 return static_cast<MPI_Group>(MPI_GROUP_NULL);