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 smpi_MPI_GROUP_EMPTY;
12 extern XBT_PRIVATE MPI_Comm MPI_COMM_UNINITIALIZED;
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) {
80 if (simgrid::smpi::F2C::lookup() != nullptr)
81 F2C::free_f(group->c2f());
86 int Group::compare(MPI_Group group2)
91 if (size_ != group2->size()) {
94 for (int i = 0; i < size_; i++) {
95 s4u::Actor* actor = this->actor(i);
96 int rank = group2->rank(actor);
97 if (rank == MPI_UNDEFINED) {
102 result = MPI_SIMILAR;
109 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
112 *newgroup = MPI_GROUP_EMPTY;
114 *newgroup = new Group(n);
115 for (int i = 0; i < n; i++) {
116 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
117 (*newgroup)->set_mapping(actor, i);
119 if((*newgroup)!=MPI_GROUP_EMPTY)
120 (*newgroup)->add_f();
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);
149 if((*newgroup)!=MPI_GROUP_EMPTY)
150 (*newgroup)->add_f();
155 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
157 int size2 = group2->size();
158 for (int i = 0; i < size2; i++) {
159 s4u::Actor* actor = group2->actor(i);
160 int proc1 = this->rank(actor);
161 if (proc1 == MPI_UNDEFINED) {
166 *newgroup = MPI_GROUP_EMPTY;
168 *newgroup = new Group(size2);
170 for (int i = 0; i < group2->size(); i++) {
171 s4u::Actor* actor = group2->actor(i);
172 int proc1 = this->rank(actor);
173 if (proc1 != MPI_UNDEFINED) {
174 (*newgroup)->set_mapping(actor, j);
178 if((*newgroup)!=MPI_GROUP_EMPTY)
179 (*newgroup)->add_f();
184 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
188 for (int i = 0; i < size2; i++) {
189 s4u::Actor* actor = this->actor(i);
190 int proc2 = group2->rank(actor);
191 if (proc2 != MPI_UNDEFINED) {
196 *newgroup = MPI_GROUP_EMPTY;
198 *newgroup = new Group(newsize);
199 for (int i = 0; i < size2; i++) {
200 s4u::Actor* actor = this->actor(i);
201 int proc2 = group2->rank(actor);
202 if (proc2 == MPI_UNDEFINED) {
203 (*newgroup)->set_mapping(actor, i);
206 if((*newgroup)!=MPI_GROUP_EMPTY)
207 (*newgroup)->add_f();
212 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
214 int newsize = oldsize - n;
215 *newgroup = new Group(newsize);
216 std::vector<bool> to_exclude(size_, false);
217 for (int i = 0; i < n; i++)
218 to_exclude[ranks[i]] = true;
220 for (int i = 0; i < oldsize; i++) {
221 if (not to_exclude[i]) {
222 s4u::Actor* actor = this->actor(i);
223 (*newgroup)->set_mapping(actor, j);
227 if((*newgroup)!=MPI_GROUP_EMPTY)
228 (*newgroup)->add_f();
232 static bool is_rank_in_range(int rank, int first, int last)
234 return (first <= rank && rank <= last) || (first >= rank && rank >= last);
237 int Group::range_incl(int n, int ranges[][3], MPI_Group* newgroup)
239 std::vector<int> to_incl;
240 for (int i = 0; i < n; i++)
241 for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
243 to_incl.push_back(j);
245 int newsize = static_cast<int>(to_incl.size());
246 *newgroup = new Group(newsize);
248 for (int j = 0; j < newsize; j++) {
249 int rank = to_incl[j];
250 s4u::Actor* actor = this->actor(rank);
251 (*newgroup)->set_mapping(actor, j);
253 if((*newgroup)!=MPI_GROUP_EMPTY)
254 (*newgroup)->add_f();
258 int Group::range_excl(int n, int ranges[][3], MPI_Group* newgroup)
260 std::vector<bool> to_excl(size_, false);
262 for (int i = 0; i < n; i++) {
263 for (int j = ranges[i][0]; j >= 0 && j < size_ && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
270 *newgroup = MPI_GROUP_EMPTY;
272 *newgroup = new Group(newsize);
275 for (int rank = 0; rank < size_; rank++) {
276 if (not to_excl[rank]) {
277 s4u::Actor* actor = this->actor(rank);
278 (*newgroup)->set_mapping(actor, j);
283 if((*newgroup)!=MPI_GROUP_EMPTY)
284 (*newgroup)->add_f();
288 MPI_Group Group::f2c(int id) {
290 return MPI_GROUP_EMPTY;
291 } else if (F2C::lookup() != nullptr && id >= 0) {
292 return static_cast<MPI_Group>(F2C::lookup()->at(id));
294 return MPI_GROUP_NULL;