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::Actor* 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 actor_to_rank_map_.insert({actor, rank});
46 int Group::rank(int index)
49 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
50 rank = index_to_rank_map_[index];
57 s4u::Actor* Group::actor(int rank)
59 if (0 <= rank && rank < size_)
60 return rank_to_actor_map_[rank];
65 int Group::rank(s4u::Actor* actor)
67 auto iterator = actor_to_rank_map_.find(actor);
68 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
76 void Group::unref(Group* group)
79 if (group->refcount_ <= 0) {
84 int Group::compare(MPI_Group group2)
89 if (size_ != group2->size()) {
92 for (int i = 0; i < size_; i++) {
93 s4u::Actor* actor = this->actor(i);
94 int rank = group2->rank(actor);
95 if (rank == MPI_UNDEFINED) {
100 result = MPI_SIMILAR;
107 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
111 *newgroup = MPI_GROUP_EMPTY;
112 } else if (n == size_) {
114 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
117 *newgroup = new Group(n);
118 for (i = 0; i < n; i++) {
119 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
120 (*newgroup)->set_mapping(actor, i);
126 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
129 int size2 = group2->size();
130 for (int i = 0; i < size2; i++) {
131 s4u::Actor* actor = group2->actor(i);
132 int proc1 = this->rank(actor);
133 if (proc1 == MPI_UNDEFINED) {
138 *newgroup = MPI_GROUP_EMPTY;
140 *newgroup = new Group(size1);
141 size2 = this->size();
142 for (int i = 0; i < size2; i++) {
143 s4u::Actor* actor1 = this->actor(i);
144 (*newgroup)->set_mapping(actor1, i);
146 for (int i = size2; i < size1; i++) {
147 s4u::Actor* actor = group2->actor(i - size2);
148 (*newgroup)->set_mapping(actor, i);
154 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
156 int size2 = group2->size();
157 for (int i = 0; i < size2; i++) {
158 s4u::Actor* actor = group2->actor(i);
159 int proc1 = this->rank(actor);
160 if (proc1 == MPI_UNDEFINED) {
165 *newgroup = MPI_GROUP_EMPTY;
167 *newgroup = new Group(size2);
169 for (int i = 0; i < group2->size(); i++) {
170 s4u::Actor* actor = group2->actor(i);
171 int proc1 = this->rank(actor);
172 if (proc1 != MPI_UNDEFINED) {
173 (*newgroup)->set_mapping(actor, j);
181 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
185 for (int i = 0; i < size2; i++) {
186 s4u::Actor* actor = this->actor(i);
187 int proc2 = group2->rank(actor);
188 if (proc2 != MPI_UNDEFINED) {
193 *newgroup = MPI_GROUP_EMPTY;
195 *newgroup = new Group(newsize);
196 for (int i = 0; i < size2; i++) {
197 s4u::Actor* actor = this->actor(i);
198 int proc2 = group2->rank(actor);
199 if (proc2 == MPI_UNDEFINED) {
200 (*newgroup)->set_mapping(actor, i);
207 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
209 int newsize = oldsize - n;
210 *newgroup = new Group(newsize);
211 int* to_exclude = new int[size_];
212 for (int i = 0; i < oldsize; i++)
214 for (int i = 0; i < n; i++)
215 to_exclude[ranks[i]]=1;
217 for (int i = 0; i < oldsize; i++) {
218 if(to_exclude[i]==0){
219 s4u::Actor* actor = this->actor(i);
220 (*newgroup)->set_mapping(actor, j);
229 static bool is_rank_in_range(int rank, int first, int last)
237 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
239 for (int i = 0; i < n; i++) {
240 for (int rank = ranges[i][0]; /* First */
241 rank >= 0 && rank < size_; /* Last */
244 if(rank == ranges[i][1]){/*already last ?*/
247 rank += ranges[i][2]; /* Stride */
248 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
252 *newgroup = new Group(newsize);
254 for (int i = 0; i < n; i++) {
255 for (int rank = ranges[i][0]; /* First */
256 rank >= 0 && rank < size_; /* Last */
258 s4u::Actor* actor = this->actor(rank);
259 (*newgroup)->set_mapping(actor, j);
261 if(rank == ranges[i][1]){/*already last ?*/
264 rank += ranges[i][2]; /* Stride */
265 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
272 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
274 for (int i = 0; i < n; i++) {
275 for (int rank = ranges[i][0]; /* First */
276 rank >= 0 && rank < size_; /* Last */
279 if(rank == ranges[i][1]){/*already last ?*/
282 rank += ranges[i][2]; /* Stride */
283 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
288 *newgroup = MPI_GROUP_EMPTY;
290 *newgroup = new Group(newsize);
293 while (newrank < newsize) {
295 for (int i = 0; i < n; i++) {
296 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
301 if(rank == ranges[i][1]){/*already last ?*/
304 rank += ranges[i][2]; /* Stride */
305 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
310 s4u::Actor* actor = this->actor(oldrank);
311 (*newgroup)->set_mapping(actor, newrank);
320 MPI_Group Group::f2c(int id) {
322 return MPI_GROUP_EMPTY;
323 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
325 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
327 return static_cast<MPI_Group>(MPI_GROUP_NULL);