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 std::vector<bool> to_exclude(size_, false);
211 for (int i = 0; i < n; i++)
212 to_exclude[ranks[i]] = true;
214 for (int i = 0; i < oldsize; i++) {
215 if (not to_exclude[i]) {
216 s4u::Actor* actor = this->actor(i);
217 (*newgroup)->set_mapping(actor, j);
224 static bool is_rank_in_range(int rank, int first, int last)
232 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
234 for (int i = 0; i < n; i++) {
235 for (int rank = ranges[i][0]; /* First */
236 rank >= 0 && rank < size_; /* Last */
239 if(rank == ranges[i][1]){/*already last ?*/
242 rank += ranges[i][2]; /* Stride */
243 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
247 *newgroup = new Group(newsize);
249 for (int i = 0; i < n; i++) {
250 for (int rank = ranges[i][0]; /* First */
251 rank >= 0 && rank < size_; /* Last */
253 s4u::Actor* actor = this->actor(rank);
254 (*newgroup)->set_mapping(actor, j);
256 if(rank == ranges[i][1]){/*already last ?*/
259 rank += ranges[i][2]; /* Stride */
260 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
267 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
269 for (int i = 0; i < n; i++) {
270 for (int rank = ranges[i][0]; /* First */
271 rank >= 0 && rank < size_; /* Last */
274 if(rank == ranges[i][1]){/*already last ?*/
277 rank += ranges[i][2]; /* Stride */
278 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
283 *newgroup = MPI_GROUP_EMPTY;
285 *newgroup = new Group(newsize);
288 while (newrank < newsize) {
290 for (int i = 0; i < n; i++) {
291 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
296 if(rank == ranges[i][1]){/*already last ?*/
299 rank += ranges[i][2]; /* Stride */
300 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
305 s4u::Actor* actor = this->actor(oldrank);
306 (*newgroup)->set_mapping(actor, newrank);
315 MPI_Group Group::f2c(int id) {
317 return MPI_GROUP_EMPTY;
318 } else if (F2C::lookup() != nullptr && id >= 0) {
319 return static_cast<MPI_Group>(F2C::lookup()->at(id));
321 return MPI_GROUP_NULL;