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_, MPI_UNDEFINED)
30 Group::Group(MPI_Group origin)
32 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
33 size_ = origin->size();
35 rank_to_index_map_ = origin->rank_to_index_map_;
36 index_to_rank_map_ = origin->index_to_rank_map_;
44 void Group::set_mapping(int index, int rank)
46 if (0 <= rank && rank < size_) {
47 rank_to_index_map_[rank] = index;
48 if (index != MPI_UNDEFINED) {
49 if ((unsigned)index >= index_to_rank_map_.size())
50 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
51 index_to_rank_map_[index] = rank;
56 int Group::index(int rank)
59 if (0 <= rank && rank < size_)
60 index = rank_to_index_map_[rank];
62 index = MPI_UNDEFINED;
66 int Group::rank(int index)
69 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
70 rank = index_to_rank_map_[index];
81 void Group::unref(Group* group)
84 if (group->refcount_ <= 0) {
94 int Group::compare(MPI_Group group2)
99 if (size_ != group2->size()) {
100 result = MPI_UNEQUAL;
102 int sz = group2->size();
103 for (int i = 0; i < sz; i++) {
104 int index = this->index(i);
105 int rank = group2->rank(index);
106 if (rank == MPI_UNDEFINED) {
107 result = MPI_UNEQUAL;
111 result = MPI_SIMILAR;
118 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
123 *newgroup = MPI_GROUP_EMPTY;
124 } else if (n == size_) {
126 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
129 *newgroup = new Group(n);
130 for (i = 0; i < n; i++) {
131 index = this->index(ranks[i]);
132 (*newgroup)->set_mapping(index, i);
138 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
141 int size2 = group2->size();
142 for (int i = 0; i < size2; i++) {
143 int proc2 = group2->index(i);
144 int proc1 = this->rank(proc2);
145 if (proc1 == MPI_UNDEFINED) {
150 *newgroup = MPI_GROUP_EMPTY;
152 *newgroup = new Group(size1);
153 size2 = this->size();
154 for (int i = 0; i < size2; i++) {
155 int proc1 = this->index(i);
156 (*newgroup)->set_mapping(proc1, i);
158 for (int i = size2; i < size1; i++) {
159 int proc2 = group2->index(i - size2);
160 (*newgroup)->set_mapping(proc2, i);
166 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
168 int size2 = group2->size();
169 for (int i = 0; i < size2; i++) {
170 int proc2 = group2->index(i);
171 int proc1 = this->rank(proc2);
172 if (proc1 == MPI_UNDEFINED) {
177 *newgroup = MPI_GROUP_EMPTY;
179 *newgroup = new Group(size2);
181 for (int i = 0; i < group2->size(); i++) {
182 int proc2 = group2->index(i);
183 int proc1 = this->rank(proc2);
184 if (proc1 != MPI_UNDEFINED) {
185 (*newgroup)->set_mapping(proc2, j);
193 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
197 for (int i = 0; i < size2; i++) {
198 int proc1 = this->index(i);
199 int proc2 = group2->rank(proc1);
200 if (proc2 != MPI_UNDEFINED) {
205 *newgroup = MPI_GROUP_EMPTY;
207 *newgroup = new Group(newsize);
208 for (int i = 0; i < size2; i++) {
209 int proc1 = this->index(i);
210 int proc2 = group2->rank(proc1);
211 if (proc2 == MPI_UNDEFINED) {
212 (*newgroup)->set_mapping(proc1, i);
219 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
221 int newsize = oldsize - n;
222 *newgroup = new Group(newsize);
223 int* to_exclude = new int[size_];
224 for (int i = 0; i < oldsize; i++)
226 for (int i = 0; i < n; i++)
227 to_exclude[ranks[i]]=1;
229 for (int i = 0; i < oldsize; i++) {
230 if(to_exclude[i]==0){
231 int index = this->index(i);
232 (*newgroup)->set_mapping(index, j);
241 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
243 for (int i = 0; i < n; i++) {
244 for (int rank = ranges[i][0]; /* First */
245 rank >= 0 && rank < size_; /* Last */
248 if(rank == ranges[i][1]){/*already last ?*/
251 rank += ranges[i][2]; /* Stride */
252 if (ranges[i][0] < ranges[i][1]) {
253 if (rank > ranges[i][1])
256 if (rank < ranges[i][1])
261 *newgroup = new Group(newsize);
263 for (int i = 0; i < n; i++) {
264 for (int rank = ranges[i][0]; /* First */
265 rank >= 0 && rank < size_; /* Last */
267 int index = this->index(rank);
268 (*newgroup)->set_mapping(index, j);
270 if(rank == ranges[i][1]){/*already last ?*/
273 rank += ranges[i][2]; /* Stride */
274 if (ranges[i][0] < ranges[i][1]) {
275 if (rank > ranges[i][1])
278 if (rank < ranges[i][1])
286 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
288 for (int i = 0; i < n; i++) {
289 for (int rank = ranges[i][0]; /* First */
290 rank >= 0 && rank < size_; /* Last */
293 if(rank == ranges[i][1]){/*already last ?*/
296 rank += ranges[i][2]; /* Stride */
297 if (ranges[i][0] < ranges[i][1]) {
298 if (rank > ranges[i][1])
301 if (rank < ranges[i][1])
307 *newgroup = MPI_GROUP_EMPTY;
309 *newgroup = new Group(newsize);
312 while (newrank < newsize) {
314 for (int i = 0; i < n; i++) {
315 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
320 if(rank == ranges[i][1]){/*already last ?*/
323 rank += ranges[i][2]; /* Stride */
324 if (ranges[i][0]<ranges[i][1]){
325 if (rank > ranges[i][1])
328 if (rank < ranges[i][1])
334 int index = this->index(oldrank);
335 (*newgroup)->set_mapping(index, newrank);
344 MPI_Group Group::f2c(int id) {
346 return MPI_GROUP_EMPTY;
347 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
349 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
351 return static_cast<MPI_Group>(MPI_GROUP_NULL);