1 /* Copyright (c) 2010-2020. 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"
11 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
12 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
17 Group::Group(const Group* origin)
19 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
20 size_ = origin->size();
21 // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
22 index_to_rank_map_ = origin->index_to_rank_map_;
23 rank_to_actor_map_ = origin->rank_to_actor_map_;
24 actor_to_rank_map_ = origin->actor_to_rank_map_;
28 void Group::set_mapping(s4u::Actor* actor, int rank)
30 if (0 <= rank && rank < size_) {
31 int index = actor->get_pid();
32 if (index != MPI_UNDEFINED) {
33 if ((unsigned)index >= index_to_rank_map_.size())
34 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
35 index_to_rank_map_[index] = rank;
38 rank_to_actor_map_[rank] = actor;
39 actor_to_rank_map_.insert({actor, rank});
43 int Group::rank(int index)
46 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
47 rank = index_to_rank_map_[index];
54 s4u::Actor* Group::actor(int rank)
56 if (0 <= rank && rank < size_)
57 return rank_to_actor_map_[rank];
62 int Group::rank(s4u::Actor* actor)
64 auto iterator = actor_to_rank_map_.find(actor);
65 //I'm not in the communicator ... but maybe my parent is ?
66 if (iterator == actor_to_rank_map_.end())
67 iterator = actor_to_rank_map_.find(s4u::Actor::by_pid(actor->get_ppid()).get());
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)
110 *newgroup = MPI_GROUP_EMPTY;
111 } else if (n == size_) {
113 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
116 *newgroup = new Group(n);
117 for (int i = 0; i < n; i++) {
118 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
119 (*newgroup)->set_mapping(actor, i);
125 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
128 int size2 = group2->size();
129 for (int i = 0; i < size2; i++) {
130 s4u::Actor* actor = group2->actor(i);
131 int proc1 = this->rank(actor);
132 if (proc1 == MPI_UNDEFINED) {
137 *newgroup = MPI_GROUP_EMPTY;
139 *newgroup = new Group(size1);
140 size2 = this->size();
141 for (int i = 0; i < size2; i++) {
142 s4u::Actor* actor1 = this->actor(i);
143 (*newgroup)->set_mapping(actor1, i);
145 for (int i = size2; i < size1; i++) {
146 s4u::Actor* actor = group2->actor(i - size2);
147 (*newgroup)->set_mapping(actor, i);
153 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
155 int size2 = group2->size();
156 for (int i = 0; i < size2; i++) {
157 s4u::Actor* actor = group2->actor(i);
158 int proc1 = this->rank(actor);
159 if (proc1 == MPI_UNDEFINED) {
164 *newgroup = MPI_GROUP_EMPTY;
166 *newgroup = new Group(size2);
168 for (int i = 0; i < group2->size(); i++) {
169 s4u::Actor* actor = group2->actor(i);
170 int proc1 = this->rank(actor);
171 if (proc1 != MPI_UNDEFINED) {
172 (*newgroup)->set_mapping(actor, j);
180 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
184 for (int i = 0; i < size2; i++) {
185 s4u::Actor* actor = this->actor(i);
186 int proc2 = group2->rank(actor);
187 if (proc2 != MPI_UNDEFINED) {
192 *newgroup = MPI_GROUP_EMPTY;
194 *newgroup = new Group(newsize);
195 for (int i = 0; i < size2; i++) {
196 s4u::Actor* actor = this->actor(i);
197 int proc2 = group2->rank(actor);
198 if (proc2 == MPI_UNDEFINED) {
199 (*newgroup)->set_mapping(actor, i);
206 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
208 int newsize = oldsize - n;
209 *newgroup = new Group(newsize);
210 auto* to_exclude = new int[size_];
211 for (int i = 0; i < oldsize; i++)
213 for (int i = 0; i < n; i++)
214 to_exclude[ranks[i]]=1;
216 for (int i = 0; i < oldsize; i++) {
217 if(to_exclude[i]==0){
218 s4u::Actor* actor = this->actor(i);
219 (*newgroup)->set_mapping(actor, j);
227 static bool is_rank_in_range(int rank, int first, int last)
235 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
237 for (int i = 0; i < n; i++) {
238 for (int rank = ranges[i][0]; /* First */
239 rank >= 0 && rank < size_; /* Last */
242 if(rank == ranges[i][1]){/*already last ?*/
245 rank += ranges[i][2]; /* Stride */
246 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
250 *newgroup = new Group(newsize);
252 for (int i = 0; i < n; i++) {
253 for (int rank = ranges[i][0]; /* First */
254 rank >= 0 && rank < size_; /* Last */
256 s4u::Actor* actor = this->actor(rank);
257 (*newgroup)->set_mapping(actor, j);
259 if(rank == ranges[i][1]){/*already last ?*/
262 rank += ranges[i][2]; /* Stride */
263 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
270 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
272 for (int i = 0; i < n; i++) {
273 for (int rank = ranges[i][0]; /* First */
274 rank >= 0 && rank < size_; /* Last */
277 if(rank == ranges[i][1]){/*already last ?*/
280 rank += ranges[i][2]; /* Stride */
281 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
286 *newgroup = MPI_GROUP_EMPTY;
288 *newgroup = new Group(newsize);
291 while (newrank < newsize) {
293 for (int i = 0; i < n; i++) {
294 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
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 s4u::Actor* actor = this->actor(oldrank);
309 (*newgroup)->set_mapping(actor, newrank);
318 MPI_Group Group::f2c(int id) {
320 return MPI_GROUP_EMPTY;
321 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
323 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
325 return MPI_GROUP_NULL;