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_comm.hpp"
7 #include "smpi_group.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
11 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
12 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
20 rank_to_index_map_ = nullptr; /* rank_to_index_map_ */
21 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
24 Group::Group(int n) : size_(n)
26 rank_to_index_map_ = new int[size_];
28 for (int i = 0; i < size_; i++)
29 rank_to_index_map_[i] = MPI_UNDEFINED;
32 Group::Group(MPI_Group origin)
34 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
35 size_ = origin->size();
36 rank_to_index_map_ = new int[size_];
38 for (int i = 0; i < size_; i++) {
39 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
42 for (auto const& elm : origin->index_to_rank_map_) {
43 index_to_rank_map_.insert({elm.first, elm.second});
50 delete[] rank_to_index_map_;
53 void Group::set_mapping(int index, int rank)
56 rank_to_index_map_[rank] = index;
57 if (index != MPI_UNDEFINED)
58 index_to_rank_map_.insert({index, rank});
62 int Group::index(int rank)
64 int index = MPI_UNDEFINED;
66 if (0 <= rank && rank < size_) {
67 index = rank_to_index_map_[rank];
72 int Group::rank(int index)
74 if (this == MPI_GROUP_EMPTY)
76 auto rank = index_to_rank_map_.find(index);
77 return rank == index_to_rank_map_.end() ? MPI_UNDEFINED : rank->second;
85 void Group::unref(Group* group)
88 if (group->refcount_ <= 0) {
98 int Group::compare(MPI_Group group2)
103 if (size_ != group2->size()) {
104 result = MPI_UNEQUAL;
106 int sz = group2->size();
107 for (int i = 0; i < sz; i++) {
108 int index = this->index(i);
109 int rank = group2->rank(index);
110 if (rank == MPI_UNDEFINED) {
111 result = MPI_UNEQUAL;
115 result = MPI_SIMILAR;
122 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
127 *newgroup = MPI_GROUP_EMPTY;
128 } else if (n == size_) {
130 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
133 *newgroup = new Group(n);
134 for (i = 0; i < n; i++) {
135 index = this->index(ranks[i]);
136 (*newgroup)->set_mapping(index, i);
142 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
145 int size2 = group2->size();
146 for (int i = 0; i < size2; i++) {
147 int proc2 = group2->index(i);
148 int proc1 = this->rank(proc2);
149 if (proc1 == MPI_UNDEFINED) {
154 *newgroup = MPI_GROUP_EMPTY;
156 *newgroup = new Group(size1);
157 size2 = this->size();
158 for (int i = 0; i < size2; i++) {
159 int proc1 = this->index(i);
160 (*newgroup)->set_mapping(proc1, i);
162 for (int i = size2; i < size1; i++) {
163 int proc2 = group2->index(i - size2);
164 (*newgroup)->set_mapping(proc2, i);
170 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
172 int size2 = group2->size();
173 for (int i = 0; i < size2; i++) {
174 int proc2 = group2->index(i);
175 int proc1 = this->rank(proc2);
176 if (proc1 == MPI_UNDEFINED) {
181 *newgroup = MPI_GROUP_EMPTY;
183 *newgroup = new Group(size2);
185 for (int i = 0; i < group2->size(); i++) {
186 int proc2 = group2->index(i);
187 int proc1 = this->rank(proc2);
188 if (proc1 != MPI_UNDEFINED) {
189 (*newgroup)->set_mapping(proc2, j);
197 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
201 for (int i = 0; i < size2; i++) {
202 int proc1 = this->index(i);
203 int proc2 = group2->rank(proc1);
204 if (proc2 != MPI_UNDEFINED) {
209 *newgroup = MPI_GROUP_EMPTY;
211 *newgroup = new Group(newsize);
212 for (int i = 0; i < size2; i++) {
213 int proc1 = this->index(i);
214 int proc2 = group2->rank(proc1);
215 if (proc2 == MPI_UNDEFINED) {
216 (*newgroup)->set_mapping(proc1, i);
223 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
225 int newsize = oldsize - n;
226 *newgroup = new Group(newsize);
227 int* to_exclude=xbt_new0(int, size_);
228 for (int i = 0; i < oldsize; i++)
230 for (int i = 0; i < n; i++)
231 to_exclude[ranks[i]]=1;
233 for (int i = 0; i < oldsize; i++) {
234 if(to_exclude[i]==0){
235 int index = this->index(i);
236 (*newgroup)->set_mapping(index, j);
240 xbt_free(to_exclude);
245 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
247 for (int i = 0; i < n; i++) {
248 for (int rank = ranges[i][0]; /* First */
249 rank >= 0 && rank < size_; /* Last */
252 if(rank == ranges[i][1]){/*already last ?*/
255 rank += ranges[i][2]; /* Stride */
256 if (ranges[i][0] < ranges[i][1]) {
257 if (rank > ranges[i][1])
260 if (rank < ranges[i][1])
265 *newgroup = new Group(newsize);
267 for (int i = 0; i < n; i++) {
268 for (int rank = ranges[i][0]; /* First */
269 rank >= 0 && rank < size_; /* Last */
271 int index = this->index(rank);
272 (*newgroup)->set_mapping(index, j);
274 if(rank == ranges[i][1]){/*already last ?*/
277 rank += ranges[i][2]; /* Stride */
278 if (ranges[i][0] < ranges[i][1]) {
279 if (rank > ranges[i][1])
282 if (rank < ranges[i][1])
290 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
292 for (int i = 0; i < n; i++) {
293 for (int rank = ranges[i][0]; /* First */
294 rank >= 0 && rank < size_; /* Last */
297 if(rank == ranges[i][1]){/*already last ?*/
300 rank += ranges[i][2]; /* Stride */
301 if (ranges[i][0] < ranges[i][1]) {
302 if (rank > ranges[i][1])
305 if (rank < ranges[i][1])
311 *newgroup = MPI_GROUP_EMPTY;
313 *newgroup = new Group(newsize);
316 while (newrank < newsize) {
318 for (int i = 0; i < n; i++) {
319 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
324 if(rank == ranges[i][1]){/*already last ?*/
327 rank += ranges[i][2]; /* Stride */
328 if (ranges[i][0]<ranges[i][1]){
329 if (rank > ranges[i][1])
332 if (rank < ranges[i][1])
338 int index = this->index(oldrank);
339 (*newgroup)->set_mapping(index, newrank);
348 MPI_Group Group::f2c(int id) {
350 return MPI_GROUP_EMPTY;
351 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
353 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
355 return static_cast<MPI_Group>(MPI_GROUP_NULL);