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 if((*newgroup)!=MPI_GROUP_EMPTY)
125 (*newgroup)->add_f();
130 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
133 int size2 = group2->size();
134 for (int i = 0; i < size2; i++) {
135 s4u::Actor* actor = group2->actor(i);
136 int proc1 = this->rank(actor);
137 if (proc1 == MPI_UNDEFINED) {
142 *newgroup = MPI_GROUP_EMPTY;
144 *newgroup = new Group(size1);
145 size2 = this->size();
146 for (int i = 0; i < size2; i++) {
147 s4u::Actor* actor1 = this->actor(i);
148 (*newgroup)->set_mapping(actor1, i);
150 for (int i = size2; i < size1; i++) {
151 s4u::Actor* actor = group2->actor(i - size2);
152 (*newgroup)->set_mapping(actor, i);
154 if((*newgroup)!=MPI_GROUP_EMPTY)
155 (*newgroup)->add_f();
160 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
162 int size2 = group2->size();
163 for (int i = 0; i < size2; i++) {
164 s4u::Actor* actor = group2->actor(i);
165 int proc1 = this->rank(actor);
166 if (proc1 == MPI_UNDEFINED) {
171 *newgroup = MPI_GROUP_EMPTY;
173 *newgroup = new Group(size2);
175 for (int i = 0; i < group2->size(); i++) {
176 s4u::Actor* actor = group2->actor(i);
177 int proc1 = this->rank(actor);
178 if (proc1 != MPI_UNDEFINED) {
179 (*newgroup)->set_mapping(actor, j);
183 if((*newgroup)!=MPI_GROUP_EMPTY)
184 (*newgroup)->add_f();
189 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
193 for (int i = 0; i < size2; i++) {
194 s4u::Actor* actor = this->actor(i);
195 int proc2 = group2->rank(actor);
196 if (proc2 != MPI_UNDEFINED) {
201 *newgroup = MPI_GROUP_EMPTY;
203 *newgroup = new Group(newsize);
204 for (int i = 0; i < size2; i++) {
205 s4u::Actor* actor = this->actor(i);
206 int proc2 = group2->rank(actor);
207 if (proc2 == MPI_UNDEFINED) {
208 (*newgroup)->set_mapping(actor, i);
211 if((*newgroup)!=MPI_GROUP_EMPTY)
212 (*newgroup)->add_f();
217 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
219 int newsize = oldsize - n;
220 *newgroup = new Group(newsize);
221 std::vector<bool> to_exclude(size_, false);
222 for (int i = 0; i < n; i++)
223 to_exclude[ranks[i]] = true;
225 for (int i = 0; i < oldsize; i++) {
226 if (not to_exclude[i]) {
227 s4u::Actor* actor = this->actor(i);
228 (*newgroup)->set_mapping(actor, j);
232 if((*newgroup)!=MPI_GROUP_EMPTY)
233 (*newgroup)->add_f();
237 static bool is_rank_in_range(int rank, int first, int last)
239 return (first <= rank && rank <= last) || (first >= rank && rank >= last);
242 int Group::range_incl(int n, int ranges[][3], MPI_Group* newgroup)
244 std::vector<int> to_incl;
245 for (int i = 0; i < n; i++)
246 for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
248 to_incl.push_back(j);
250 int newsize = static_cast<int>(to_incl.size());
251 *newgroup = new Group(newsize);
253 for (int j = 0; j < newsize; j++) {
254 int rank = to_incl[j];
255 s4u::Actor* actor = this->actor(rank);
256 (*newgroup)->set_mapping(actor, j);
258 if((*newgroup)!=MPI_GROUP_EMPTY)
259 (*newgroup)->add_f();
263 int Group::range_excl(int n, int ranges[][3], MPI_Group* newgroup)
265 std::vector<bool> to_excl(size_, false);
267 for (int i = 0; i < n; i++) {
268 for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
275 *newgroup = MPI_GROUP_EMPTY;
277 *newgroup = new Group(newsize);
279 for (int j = 0, rank = 0; rank < size_; rank++) {
280 if (not to_excl[rank]) {
281 s4u::Actor* actor = this->actor(rank);
282 (*newgroup)->set_mapping(actor, j);
287 if((*newgroup)!=MPI_GROUP_EMPTY)
288 (*newgroup)->add_f();
292 MPI_Group Group::f2c(int id) {
294 return MPI_GROUP_EMPTY;
295 } else if (F2C::lookup() != nullptr && id >= 0) {
296 return static_cast<MPI_Group>(F2C::lookup()->at(id));
298 return MPI_GROUP_NULL;