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(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 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
73 void Group::unref(Group* group)
76 if (group->refcount_ <= 0) {
81 int Group::compare(MPI_Group group2)
86 if (size_ != group2->size()) {
89 for (int i = 0; i < size_; i++) {
90 s4u::Actor* actor = this->actor(i);
91 int rank = group2->rank(actor);
92 if (rank == MPI_UNDEFINED) {
104 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
107 *newgroup = MPI_GROUP_EMPTY;
108 } else if (n == size_) {
110 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
113 *newgroup = new Group(n);
114 for (int i = 0; i < n; i++) {
115 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
116 (*newgroup)->set_mapping(actor, i);
122 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
125 int size2 = group2->size();
126 for (int i = 0; i < size2; i++) {
127 s4u::Actor* actor = group2->actor(i);
128 int proc1 = this->rank(actor);
129 if (proc1 == MPI_UNDEFINED) {
134 *newgroup = MPI_GROUP_EMPTY;
136 *newgroup = new Group(size1);
137 size2 = this->size();
138 for (int i = 0; i < size2; i++) {
139 s4u::Actor* actor1 = this->actor(i);
140 (*newgroup)->set_mapping(actor1, i);
142 for (int i = size2; i < size1; i++) {
143 s4u::Actor* actor = group2->actor(i - size2);
144 (*newgroup)->set_mapping(actor, i);
150 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
152 int size2 = group2->size();
153 for (int i = 0; i < size2; i++) {
154 s4u::Actor* actor = group2->actor(i);
155 int proc1 = this->rank(actor);
156 if (proc1 == MPI_UNDEFINED) {
161 *newgroup = MPI_GROUP_EMPTY;
163 *newgroup = new Group(size2);
165 for (int i = 0; i < group2->size(); i++) {
166 s4u::Actor* actor = group2->actor(i);
167 int proc1 = this->rank(actor);
168 if (proc1 != MPI_UNDEFINED) {
169 (*newgroup)->set_mapping(actor, j);
177 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
181 for (int i = 0; i < size2; i++) {
182 s4u::Actor* actor = this->actor(i);
183 int proc2 = group2->rank(actor);
184 if (proc2 != MPI_UNDEFINED) {
189 *newgroup = MPI_GROUP_EMPTY;
191 *newgroup = new Group(newsize);
192 for (int i = 0; i < size2; i++) {
193 s4u::Actor* actor = this->actor(i);
194 int proc2 = group2->rank(actor);
195 if (proc2 == MPI_UNDEFINED) {
196 (*newgroup)->set_mapping(actor, i);
203 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
205 int newsize = oldsize - n;
206 *newgroup = new Group(newsize);
207 int* to_exclude = new int[size_];
208 for (int i = 0; i < oldsize; i++)
210 for (int i = 0; i < n; i++)
211 to_exclude[ranks[i]]=1;
213 for (int i = 0; i < oldsize; i++) {
214 if(to_exclude[i]==0){
215 s4u::Actor* actor = this->actor(i);
216 (*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::f2c_lookup() != nullptr && id >= 0) {
320 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
322 return static_cast<MPI_Group>(MPI_GROUP_NULL);