1 /* Copyright (c) 2010-2018. 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 using simgrid::s4u::ActorPtr;
25 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
28 Group::Group(int n) : size_(n), rank_to_actor_map_(size_, nullptr), index_to_rank_map_(size_, MPI_UNDEFINED)
33 Group::Group(Group* origin)
35 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
36 size_ = origin->size();
38 // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
39 index_to_rank_map_ = origin->index_to_rank_map_;
40 rank_to_actor_map_ = origin->rank_to_actor_map_;
41 actor_to_rank_map_ = origin->actor_to_rank_map_;
45 void Group::set_mapping(simgrid::s4u::ActorPtr actor, int rank)
47 if (0 <= rank && rank < size_) {
48 int index = actor->get_pid();
49 if (index != MPI_UNDEFINED) {
50 if ((unsigned)index >= index_to_rank_map_.size())
51 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
52 index_to_rank_map_[index] = rank;
55 rank_to_actor_map_[rank] = actor;
56 if (actor != nullptr) {
57 actor_to_rank_map_.insert({actor, rank});
62 int Group::rank(int index)
65 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
66 rank = index_to_rank_map_[index];
73 simgrid::s4u::ActorPtr Group::actor(int rank) {
74 if (0 <= rank && rank < size_)
75 return rank_to_actor_map_[rank];
80 int Group::rank(const simgrid::s4u::ActorPtr actor) {
81 auto iterator = actor_to_rank_map_.find(actor);
82 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
90 void Group::unref(Group* group)
93 if (group->refcount_ <= 0) {
103 int Group::compare(MPI_Group group2)
108 if (size_ != group2->size()) {
109 result = MPI_UNEQUAL;
111 for (int i = 0; i < size_; i++) {
112 ActorPtr actor = this->actor(i);
113 int rank = group2->rank(actor);
114 if (rank == MPI_UNDEFINED) {
115 result = MPI_UNEQUAL;
119 result = MPI_SIMILAR;
126 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
130 *newgroup = MPI_GROUP_EMPTY;
131 } else if (n == size_) {
133 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
136 *newgroup = new Group(n);
137 for (i = 0; i < n; i++) {
138 ActorPtr actor = this->actor(ranks[i]); // ranks[] was passed as a param!
139 (*newgroup)->set_mapping(actor, i);
145 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
148 int size2 = group2->size();
149 for (int i = 0; i < size2; i++) {
150 ActorPtr actor = group2->actor(i);
151 int proc1 = this->rank(actor);
152 if (proc1 == MPI_UNDEFINED) {
157 *newgroup = MPI_GROUP_EMPTY;
159 *newgroup = new Group(size1);
160 size2 = this->size();
161 for (int i = 0; i < size2; i++) {
162 ActorPtr actor1 = this->actor(i);
163 (*newgroup)->set_mapping(actor1, i);
165 for (int i = size2; i < size1; i++) {
166 ActorPtr actor = group2->actor(i - size2);
167 (*newgroup)->set_mapping(actor, i);
173 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
175 int size2 = group2->size();
176 for (int i = 0; i < size2; i++) {
177 ActorPtr actor = group2->actor(i);
178 int proc1 = this->rank(actor);
179 if (proc1 == MPI_UNDEFINED) {
184 *newgroup = MPI_GROUP_EMPTY;
186 *newgroup = new Group(size2);
188 for (int i = 0; i < group2->size(); i++) {
189 ActorPtr actor = group2->actor(i);
190 int proc1 = this->rank(actor);
191 if (proc1 != MPI_UNDEFINED) {
192 (*newgroup)->set_mapping(actor, j);
200 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
204 for (int i = 0; i < size2; i++) {
205 ActorPtr actor = this->actor(i);
206 int proc2 = group2->rank(actor);
207 if (proc2 != MPI_UNDEFINED) {
212 *newgroup = MPI_GROUP_EMPTY;
214 *newgroup = new Group(newsize);
215 for (int i = 0; i < size2; i++) {
216 ActorPtr actor = this->actor(i);
217 int proc2 = group2->rank(actor);
218 if (proc2 == MPI_UNDEFINED) {
219 (*newgroup)->set_mapping(actor, i);
226 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
228 int newsize = oldsize - n;
229 *newgroup = new Group(newsize);
230 int* to_exclude = new int[size_];
231 for (int i = 0; i < oldsize; i++)
233 for (int i = 0; i < n; i++)
234 to_exclude[ranks[i]]=1;
236 for (int i = 0; i < oldsize; i++) {
237 if(to_exclude[i]==0){
238 ActorPtr actor = this->actor(i);
239 (*newgroup)->set_mapping(actor, j);
248 static bool is_rank_in_range(int rank, int first, int last)
256 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
258 for (int i = 0; i < n; i++) {
259 for (int rank = ranges[i][0]; /* First */
260 rank >= 0 && rank < size_; /* Last */
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]))
271 *newgroup = new Group(newsize);
273 for (int i = 0; i < n; i++) {
274 for (int rank = ranges[i][0]; /* First */
275 rank >= 0 && rank < size_; /* Last */
277 ActorPtr actor = this->actor(rank);
278 (*newgroup)->set_mapping(actor, j);
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]))
291 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
293 for (int i = 0; i < n; i++) {
294 for (int rank = ranges[i][0]; /* First */
295 rank >= 0 && rank < size_; /* Last */
298 if(rank == ranges[i][1]){/*already last ?*/
301 rank += ranges[i][2]; /* Stride */
302 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
307 *newgroup = MPI_GROUP_EMPTY;
309 *newgroup = new Group(newsize);
312 while (newrank < newsize) {
314 for (int i = 0; i < n; i++) {
315 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
320 if(rank == ranges[i][1]){/*already last ?*/
323 rank += ranges[i][2]; /* Stride */
324 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
329 ActorPtr actor = this->actor(oldrank);
330 (*newgroup)->set_mapping(actor, newrank);
339 MPI_Group Group::f2c(int id) {
341 return MPI_GROUP_EMPTY;
342 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
344 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
346 return static_cast<MPI_Group>(MPI_GROUP_NULL);