1 /* Copyright (c) 2010-2021. 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;
13 extern XBT_PRIVATE MPI_Comm MPI_COMM_UNINITIALIZED;
18 Group::Group(const Group* origin)
20 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
21 size_ = origin->size();
22 // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
23 index_to_rank_map_ = origin->index_to_rank_map_;
24 rank_to_actor_map_ = origin->rank_to_actor_map_;
25 actor_to_rank_map_ = origin->actor_to_rank_map_;
29 void Group::set_mapping(s4u::Actor* actor, int rank)
31 if (0 <= rank && rank < size_) {
32 int index = actor->get_pid();
33 if (index != MPI_UNDEFINED) {
34 if ((unsigned)index >= index_to_rank_map_.size())
35 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
36 index_to_rank_map_[index] = rank;
39 rank_to_actor_map_[rank] = actor;
40 actor_to_rank_map_.insert({actor, rank});
44 int Group::rank(int index)
47 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
48 rank = index_to_rank_map_[index];
55 s4u::Actor* Group::actor(int rank)
57 if (0 <= rank && rank < size_)
58 return rank_to_actor_map_[rank];
63 int Group::rank(s4u::Actor* actor)
65 auto iterator = actor_to_rank_map_.find(actor);
66 //I'm not in the communicator ... but maybe my parent is ?
67 if (iterator == actor_to_rank_map_.end())
68 iterator = actor_to_rank_map_.find(s4u::Actor::by_pid(actor->get_ppid()).get());
69 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
77 void Group::unref(Group* group)
80 if (group->refcount_ <= 0) {
81 if (simgrid::smpi::F2C::lookup() != nullptr)
82 F2C::free_f(group->c2f());
87 int Group::compare(MPI_Group group2)
92 if (size_ != group2->size()) {
95 for (int i = 0; i < size_; i++) {
96 s4u::Actor* actor = this->actor(i);
97 int rank = group2->rank(actor);
98 if (rank == MPI_UNDEFINED) {
103 result = MPI_SIMILAR;
110 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
113 *newgroup = MPI_GROUP_EMPTY;
114 } else if (n == size_) {
116 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
119 *newgroup = new Group(n);
120 for (int i = 0; i < n; i++) {
121 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
122 (*newgroup)->set_mapping(actor, i);
124 (*newgroup)->add_f();
129 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
132 int size2 = group2->size();
133 for (int i = 0; i < size2; i++) {
134 s4u::Actor* actor = group2->actor(i);
135 int proc1 = this->rank(actor);
136 if (proc1 == MPI_UNDEFINED) {
141 *newgroup = MPI_GROUP_EMPTY;
143 *newgroup = new Group(size1);
144 size2 = this->size();
145 for (int i = 0; i < size2; i++) {
146 s4u::Actor* actor1 = this->actor(i);
147 (*newgroup)->set_mapping(actor1, i);
149 for (int i = size2; i < size1; i++) {
150 s4u::Actor* actor = group2->actor(i - size2);
151 (*newgroup)->set_mapping(actor, i);
153 (*newgroup)->add_f();
158 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
160 int size2 = group2->size();
161 for (int i = 0; i < size2; i++) {
162 s4u::Actor* actor = group2->actor(i);
163 int proc1 = this->rank(actor);
164 if (proc1 == MPI_UNDEFINED) {
169 *newgroup = MPI_GROUP_EMPTY;
171 *newgroup = new Group(size2);
173 for (int i = 0; i < group2->size(); i++) {
174 s4u::Actor* actor = group2->actor(i);
175 int proc1 = this->rank(actor);
176 if (proc1 != MPI_UNDEFINED) {
177 (*newgroup)->set_mapping(actor, j);
180 (*newgroup)->add_f();
186 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
190 for (int i = 0; i < size2; i++) {
191 s4u::Actor* actor = this->actor(i);
192 int proc2 = group2->rank(actor);
193 if (proc2 != MPI_UNDEFINED) {
198 *newgroup = MPI_GROUP_EMPTY;
200 *newgroup = new Group(newsize);
201 for (int i = 0; i < size2; i++) {
202 s4u::Actor* actor = this->actor(i);
203 int proc2 = group2->rank(actor);
204 if (proc2 == MPI_UNDEFINED) {
205 (*newgroup)->set_mapping(actor, i);
208 (*newgroup)->add_f();
213 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
215 int newsize = oldsize - n;
216 *newgroup = new Group(newsize);
217 std::vector<bool> to_exclude(size_, false);
218 for (int i = 0; i < n; i++)
219 to_exclude[ranks[i]] = true;
221 for (int i = 0; i < oldsize; i++) {
222 if (not to_exclude[i]) {
223 s4u::Actor* actor = this->actor(i);
224 (*newgroup)->set_mapping(actor, j);
228 (*newgroup)->add_f();
232 static bool is_rank_in_range(int rank, int first, int last)
240 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
242 for (int i = 0; i < n; i++) {
243 for (int rank = ranges[i][0]; /* First */
244 rank >= 0 && rank < size_; /* Last */
247 if(rank == ranges[i][1]){/*already last ?*/
250 rank += ranges[i][2]; /* Stride */
251 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
255 *newgroup = new Group(newsize);
257 for (int i = 0; i < n; i++) {
258 for (int rank = ranges[i][0]; /* First */
259 rank >= 0 && rank < size_; /* Last */
261 s4u::Actor* actor = this->actor(rank);
262 (*newgroup)->set_mapping(actor, j);
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)->add_f();
276 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
278 for (int i = 0; i < n; i++) {
279 for (int rank = ranges[i][0]; /* First */
280 rank >= 0 && rank < size_; /* Last */
283 if(rank == ranges[i][1]){/*already last ?*/
286 rank += ranges[i][2]; /* Stride */
287 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
292 *newgroup = MPI_GROUP_EMPTY;
294 *newgroup = new Group(newsize);
297 while (newrank < newsize) {
299 for (int i = 0; i < n; i++) {
300 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
305 if(rank == ranges[i][1]){/*already last ?*/
308 rank += ranges[i][2]; /* Stride */
309 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
314 s4u::Actor* actor = this->actor(oldrank);
315 (*newgroup)->set_mapping(actor, newrank);
321 (*newgroup)->add_f();
325 MPI_Group Group::f2c(int id) {
327 return MPI_GROUP_EMPTY;
328 } else if (F2C::lookup() != nullptr && id >= 0) {
329 return static_cast<MPI_Group>(F2C::lookup()->at(id));
331 return MPI_GROUP_NULL;