1 /* Copyright (c) 2010-2017. 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 "smpi_group.hpp"
7 #include "smpi_comm.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
12 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
13 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
21 rank_to_index_map_ = nullptr; /* rank_to_index_map_ */
22 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
25 Group::Group(int n) : size_(n)
27 rank_to_index_map_ = new int[size_];
29 for (int i = 0; i < size_; i++)
30 rank_to_index_map_[i] = MPI_UNDEFINED;
33 Group::Group(MPI_Group origin)
35 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
36 size_ = origin->size();
37 rank_to_index_map_ = new int[size_];
39 for (int i = 0; i < size_; i++) {
40 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
43 for (auto const& elm : origin->index_to_rank_map_) {
44 index_to_rank_map_.insert({elm.first, elm.second});
51 delete[] rank_to_index_map_;
54 void Group::set_mapping(int index, int rank)
57 rank_to_index_map_[rank] = index;
58 if (index != MPI_UNDEFINED)
59 index_to_rank_map_.insert({index, rank});
63 int Group::index(int rank)
65 int index = MPI_UNDEFINED;
67 if (0 <= rank && rank < size_) {
68 index = rank_to_index_map_[rank];
73 int Group::rank(int index)
75 if (this == MPI_GROUP_EMPTY)
77 auto rank = index_to_rank_map_.find(index);
78 return rank == index_to_rank_map_.end() ? MPI_UNDEFINED : rank->second;
86 void Group::unref(Group* group)
89 if (group->refcount_ <= 0) {
99 int Group::compare(MPI_Group group2)
104 if (size_ != group2->size()) {
105 result = MPI_UNEQUAL;
107 int sz = group2->size();
108 for (int i = 0; i < sz; i++) {
109 int index = this->index(i);
110 int rank = group2->rank(index);
111 if (rank == MPI_UNDEFINED) {
112 result = MPI_UNEQUAL;
116 result = MPI_SIMILAR;
123 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
128 *newgroup = MPI_GROUP_EMPTY;
129 } else if (n == size_) {
131 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
134 *newgroup = new Group(n);
135 for (i = 0; i < n; i++) {
136 index = this->index(ranks[i]);
137 (*newgroup)->set_mapping(index, i);
143 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
146 int size2 = group2->size();
147 for (int i = 0; i < size2; i++) {
148 int proc2 = group2->index(i);
149 int proc1 = this->rank(proc2);
150 if (proc1 == MPI_UNDEFINED) {
155 *newgroup = MPI_GROUP_EMPTY;
157 *newgroup = new Group(size1);
158 size2 = this->size();
159 for (int i = 0; i < size2; i++) {
160 int proc1 = this->index(i);
161 (*newgroup)->set_mapping(proc1, i);
163 for (int i = size2; i < size1; i++) {
164 int proc2 = group2->index(i - size2);
165 (*newgroup)->set_mapping(proc2, i);
171 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
173 int size2 = group2->size();
174 for (int i = 0; i < size2; i++) {
175 int proc2 = group2->index(i);
176 int proc1 = this->rank(proc2);
177 if (proc1 == MPI_UNDEFINED) {
182 *newgroup = MPI_GROUP_EMPTY;
184 *newgroup = new Group(size2);
186 for (int i = 0; i < group2->size(); i++) {
187 int proc2 = group2->index(i);
188 int proc1 = this->rank(proc2);
189 if (proc1 != MPI_UNDEFINED) {
190 (*newgroup)->set_mapping(proc2, j);
198 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
202 for (int i = 0; i < size2; i++) {
203 int proc1 = this->index(i);
204 int proc2 = group2->rank(proc1);
205 if (proc2 != MPI_UNDEFINED) {
210 *newgroup = MPI_GROUP_EMPTY;
212 *newgroup = new Group(newsize);
213 for (int i = 0; i < size2; i++) {
214 int proc1 = this->index(i);
215 int proc2 = group2->rank(proc1);
216 if (proc2 == MPI_UNDEFINED) {
217 (*newgroup)->set_mapping(proc1, i);
224 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
226 int newsize = oldsize - n;
227 *newgroup = new Group(newsize);
228 int* to_exclude = new int[size_];
229 for (int i = 0; i < oldsize; i++)
231 for (int i = 0; i < n; i++)
232 to_exclude[ranks[i]]=1;
234 for (int i = 0; i < oldsize; i++) {
235 if(to_exclude[i]==0){
236 int index = this->index(i);
237 (*newgroup)->set_mapping(index, j);
246 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
248 for (int i = 0; i < n; i++) {
249 for (int rank = ranges[i][0]; /* First */
250 rank >= 0 && rank < size_; /* Last */
253 if(rank == ranges[i][1]){/*already last ?*/
256 rank += ranges[i][2]; /* Stride */
257 if (ranges[i][0] < ranges[i][1]) {
258 if (rank > ranges[i][1])
261 if (rank < ranges[i][1])
266 *newgroup = new Group(newsize);
268 for (int i = 0; i < n; i++) {
269 for (int rank = ranges[i][0]; /* First */
270 rank >= 0 && rank < size_; /* Last */
272 int index = this->index(rank);
273 (*newgroup)->set_mapping(index, j);
275 if(rank == ranges[i][1]){/*already last ?*/
278 rank += ranges[i][2]; /* Stride */
279 if (ranges[i][0] < ranges[i][1]) {
280 if (rank > ranges[i][1])
283 if (rank < ranges[i][1])
291 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
293 for (int i = 0; i < n; i++) {
294 for (int rank = ranges[i][0]; /* First */
295 rank >= 0 && rank < size_; /* Last */
298 if(rank == ranges[i][1]){/*already last ?*/
301 rank += ranges[i][2]; /* Stride */
302 if (ranges[i][0] < ranges[i][1]) {
303 if (rank > ranges[i][1])
306 if (rank < ranges[i][1])
312 *newgroup = MPI_GROUP_EMPTY;
314 *newgroup = new Group(newsize);
317 while (newrank < newsize) {
319 for (int i = 0; i < n; i++) {
320 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
325 if(rank == ranges[i][1]){/*already last ?*/
328 rank += ranges[i][2]; /* Stride */
329 if (ranges[i][0]<ranges[i][1]){
330 if (rank > ranges[i][1])
333 if (rank < ranges[i][1])
339 int index = this->index(oldrank);
340 (*newgroup)->set_mapping(index, newrank);
349 MPI_Group Group::f2c(int id) {
351 return MPI_GROUP_EMPTY;
352 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
354 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
356 return static_cast<MPI_Group>(MPI_GROUP_NULL);