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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
13 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
14 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
22 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
25 Group::Group(int n) : size_(n), rank_to_index_map_(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();
37 rank_to_index_map_ = origin->rank_to_index_map_;
39 for (auto const& elm : origin->index_to_rank_map_) {
40 index_to_rank_map_.insert({elm.first, elm.second});
47 rank_to_index_map_.clear();
50 void Group::set_mapping(int index, int rank)
53 rank_to_index_map_[rank] = index;
54 if (index != MPI_UNDEFINED)
55 index_to_rank_map_.insert({index, rank});
59 int Group::index(int rank)
61 int index = MPI_UNDEFINED;
63 if (0 <= rank && rank < size_) {
64 index = rank_to_index_map_[rank];
69 int Group::rank(int index)
71 if (this == MPI_GROUP_EMPTY)
73 auto rank = index_to_rank_map_.find(index);
74 return rank == index_to_rank_map_.end() ? MPI_UNDEFINED : rank->second;
82 void Group::unref(Group* group)
85 if (group->refcount_ <= 0) {
95 int Group::compare(MPI_Group group2)
100 if (size_ != group2->size()) {
101 result = MPI_UNEQUAL;
103 int sz = group2->size();
104 for (int i = 0; i < sz; i++) {
105 int index = this->index(i);
106 int rank = group2->rank(index);
107 if (rank == MPI_UNDEFINED) {
108 result = MPI_UNEQUAL;
112 result = MPI_SIMILAR;
119 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
124 *newgroup = MPI_GROUP_EMPTY;
125 } else if (n == size_) {
127 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
130 *newgroup = new Group(n);
131 for (i = 0; i < n; i++) {
132 index = this->index(ranks[i]);
133 (*newgroup)->set_mapping(index, i);
139 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
142 int size2 = group2->size();
143 for (int i = 0; i < size2; i++) {
144 int proc2 = group2->index(i);
145 int proc1 = this->rank(proc2);
146 if (proc1 == MPI_UNDEFINED) {
151 *newgroup = MPI_GROUP_EMPTY;
153 *newgroup = new Group(size1);
154 size2 = this->size();
155 for (int i = 0; i < size2; i++) {
156 int proc1 = this->index(i);
157 (*newgroup)->set_mapping(proc1, i);
159 for (int i = size2; i < size1; i++) {
160 int proc2 = group2->index(i - size2);
161 (*newgroup)->set_mapping(proc2, i);
167 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
169 int size2 = group2->size();
170 for (int i = 0; i < size2; i++) {
171 int proc2 = group2->index(i);
172 int proc1 = this->rank(proc2);
173 if (proc1 == MPI_UNDEFINED) {
178 *newgroup = MPI_GROUP_EMPTY;
180 *newgroup = new Group(size2);
182 for (int i = 0; i < group2->size(); i++) {
183 int proc2 = group2->index(i);
184 int proc1 = this->rank(proc2);
185 if (proc1 != MPI_UNDEFINED) {
186 (*newgroup)->set_mapping(proc2, j);
194 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
198 for (int i = 0; i < size2; i++) {
199 int proc1 = this->index(i);
200 int proc2 = group2->rank(proc1);
201 if (proc2 != MPI_UNDEFINED) {
206 *newgroup = MPI_GROUP_EMPTY;
208 *newgroup = new Group(newsize);
209 for (int i = 0; i < size2; i++) {
210 int proc1 = this->index(i);
211 int proc2 = group2->rank(proc1);
212 if (proc2 == MPI_UNDEFINED) {
213 (*newgroup)->set_mapping(proc1, i);
220 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
222 int newsize = oldsize - n;
223 *newgroup = new Group(newsize);
224 int* to_exclude = new int[size_];
225 for (int i = 0; i < oldsize; i++)
227 for (int i = 0; i < n; i++)
228 to_exclude[ranks[i]]=1;
230 for (int i = 0; i < oldsize; i++) {
231 if(to_exclude[i]==0){
232 int index = this->index(i);
233 (*newgroup)->set_mapping(index, j);
242 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
244 for (int i = 0; i < n; i++) {
245 for (int rank = ranges[i][0]; /* First */
246 rank >= 0 && rank < size_; /* Last */
249 if(rank == ranges[i][1]){/*already last ?*/
252 rank += ranges[i][2]; /* Stride */
253 if (ranges[i][0] < ranges[i][1]) {
254 if (rank > ranges[i][1])
257 if (rank < ranges[i][1])
262 *newgroup = new Group(newsize);
264 for (int i = 0; i < n; i++) {
265 for (int rank = ranges[i][0]; /* First */
266 rank >= 0 && rank < size_; /* Last */
268 int index = this->index(rank);
269 (*newgroup)->set_mapping(index, j);
271 if(rank == ranges[i][1]){/*already last ?*/
274 rank += ranges[i][2]; /* Stride */
275 if (ranges[i][0] < ranges[i][1]) {
276 if (rank > ranges[i][1])
279 if (rank < ranges[i][1])
287 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
289 for (int i = 0; i < n; i++) {
290 for (int rank = ranges[i][0]; /* First */
291 rank >= 0 && rank < size_; /* Last */
294 if(rank == ranges[i][1]){/*already last ?*/
297 rank += ranges[i][2]; /* Stride */
298 if (ranges[i][0] < ranges[i][1]) {
299 if (rank > ranges[i][1])
302 if (rank < ranges[i][1])
308 *newgroup = MPI_GROUP_EMPTY;
310 *newgroup = new Group(newsize);
313 while (newrank < newsize) {
315 for (int i = 0; i < n; i++) {
316 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
321 if(rank == ranges[i][1]){/*already last ?*/
324 rank += ranges[i][2]; /* Stride */
325 if (ranges[i][0]<ranges[i][1]){
326 if (rank > ranges[i][1])
329 if (rank < ranges[i][1])
335 int index = this->index(oldrank);
336 (*newgroup)->set_mapping(index, newrank);
345 MPI_Group Group::f2c(int id) {
347 return MPI_GROUP_EMPTY;
348 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
350 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
352 return static_cast<MPI_Group>(MPI_GROUP_NULL);