1 /* Copyright (c) 2010-2017. 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), rank_to_index_map_(size_, MPI_UNDEFINED), index_to_rank_map_(size_, MPI_UNDEFINED)
33 Group::Group(MPI_Group origin)
35 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
36 size_ = origin->size();
38 rank_to_index_map_ = origin->rank_to_index_map_;
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->getPid();
49 rank_to_index_map_[rank] = index;
50 if (index != MPI_UNDEFINED) {
51 if ((unsigned)index >= index_to_rank_map_.size())
52 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
53 index_to_rank_map_[index] = rank;
56 rank_to_actor_map_[rank] = actor;
57 if (actor != nullptr) {
58 actor_to_rank_map_.insert({actor, rank});
63 int Group::rank(int index)
66 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
67 rank = index_to_rank_map_[index];
74 simgrid::s4u::ActorPtr Group::actor(int rank) {
75 if (0 <= rank && rank < size_)
76 return rank_to_actor_map_[rank];
81 int Group::rank(const simgrid::s4u::ActorPtr actor) {
82 auto iterator = actor_to_rank_map_.find(actor);
83 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
91 void Group::unref(Group* group)
94 if (group->refcount_ <= 0) {
104 int Group::compare(MPI_Group group2)
109 if (size_ != group2->size()) {
110 result = MPI_UNEQUAL;
112 for (int i = 0; i < size_; i++) {
113 ActorPtr actor = this->actor(i);
114 int rank = group2->rank(actor);
115 if (rank == MPI_UNDEFINED) {
116 result = MPI_UNEQUAL;
120 result = MPI_SIMILAR;
127 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
131 *newgroup = MPI_GROUP_EMPTY;
132 } else if (n == size_) {
134 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
137 *newgroup = new Group(n);
138 for (i = 0; i < n; i++) {
139 ActorPtr actor = this->actor(ranks[i]); // ranks[] was passed as a param!
140 (*newgroup)->set_mapping(actor, i);
146 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
149 int size2 = group2->size();
150 for (int i = 0; i < size2; i++) {
151 ActorPtr actor = group2->actor(i);
152 int proc1 = this->rank(actor);
153 if (proc1 == MPI_UNDEFINED) {
158 *newgroup = MPI_GROUP_EMPTY;
160 *newgroup = new Group(size1);
161 size2 = this->size();
162 for (int i = 0; i < size2; i++) {
163 ActorPtr actor1 = this->actor(i);
164 (*newgroup)->set_mapping(actor1, i);
166 for (int i = size2; i < size1; i++) {
167 ActorPtr actor = group2->actor(i - size2);
168 (*newgroup)->set_mapping(actor, i);
174 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
176 int size2 = group2->size();
177 for (int i = 0; i < size2; i++) {
178 ActorPtr actor = group2->actor(i);
179 int proc1 = this->rank(actor);
180 if (proc1 == MPI_UNDEFINED) {
185 *newgroup = MPI_GROUP_EMPTY;
187 *newgroup = new Group(size2);
189 for (int i = 0; i < group2->size(); i++) {
190 ActorPtr actor = group2->actor(i);
191 int proc1 = this->rank(actor);
192 if (proc1 != MPI_UNDEFINED) {
193 (*newgroup)->set_mapping(actor, j);
201 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
205 for (int i = 0; i < size2; i++) {
206 ActorPtr actor = this->actor(i);
207 int proc2 = group2->rank(actor);
208 if (proc2 != MPI_UNDEFINED) {
213 *newgroup = MPI_GROUP_EMPTY;
215 *newgroup = new Group(newsize);
216 for (int i = 0; i < size2; i++) {
217 ActorPtr actor = this->actor(i);
218 int proc2 = group2->rank(actor);
219 if (proc2 == MPI_UNDEFINED) {
220 (*newgroup)->set_mapping(actor, i);
227 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
229 int newsize = oldsize - n;
230 *newgroup = new Group(newsize);
231 int* to_exclude = new int[size_];
232 for (int i = 0; i < oldsize; i++)
234 for (int i = 0; i < n; i++)
235 to_exclude[ranks[i]]=1;
237 for (int i = 0; i < oldsize; i++) {
238 if(to_exclude[i]==0){
239 ActorPtr actor = this->actor(i);
240 (*newgroup)->set_mapping(actor, j);
249 static bool is_rank_in_range(int rank, int first, int last)
257 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
259 for (int i = 0; i < n; i++) {
260 for (int rank = ranges[i][0]; /* First */
261 rank >= 0 && rank < size_; /* Last */
264 if(rank == ranges[i][1]){/*already last ?*/
267 rank += ranges[i][2]; /* Stride */
268 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
272 *newgroup = new Group(newsize);
274 for (int i = 0; i < n; i++) {
275 for (int rank = ranges[i][0]; /* First */
276 rank >= 0 && rank < size_; /* Last */
278 ActorPtr actor = this->actor(rank);
279 (*newgroup)->set_mapping(actor, j);
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]))
292 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
294 for (int i = 0; i < n; i++) {
295 for (int rank = ranges[i][0]; /* First */
296 rank >= 0 && rank < size_; /* Last */
299 if(rank == ranges[i][1]){/*already last ?*/
302 rank += ranges[i][2]; /* Stride */
303 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
308 *newgroup = MPI_GROUP_EMPTY;
310 *newgroup = new Group(newsize);
313 while (newrank < newsize) {
315 for (int i = 0; i < n; i++) {
316 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
321 if(rank == ranges[i][1]){/*already last ?*/
324 rank += ranges[i][2]; /* Stride */
325 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
330 ActorPtr actor = this->actor(oldrank);
331 (*newgroup)->set_mapping(actor, newrank);
340 MPI_Group Group::f2c(int id) {
342 return MPI_GROUP_EMPTY;
343 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
345 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
347 return static_cast<MPI_Group>(MPI_GROUP_NULL);