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 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)
77 return index_to_rank_map_.at(index);
78 } catch (std::out_of_range& unfound) {
88 void Group::unref(Group* group)
91 if (group->refcount_ <= 0) {
101 int Group::compare(MPI_Group group2)
106 if (size_ != group2->size()) {
107 result = MPI_UNEQUAL;
109 int sz = group2->size();
110 for (int i = 0; i < sz; i++) {
111 int index = this->index(i);
112 int rank = group2->rank(index);
113 if (rank == MPI_UNDEFINED) {
114 result = MPI_UNEQUAL;
118 result = MPI_SIMILAR;
125 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
130 *newgroup = MPI_GROUP_EMPTY;
131 } else if (n == size_) {
133 if(this!= MPI_COMM_WORLD->group()
134 && this != MPI_COMM_SELF->group()
135 && this != MPI_GROUP_EMPTY)
138 *newgroup = new Group(n);
139 for (i = 0; i < n; i++) {
140 index = this->index(ranks[i]);
141 (*newgroup)->set_mapping(index, i);
147 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
150 int size2 = group2->size();
151 for (int i = 0; i < size2; i++) {
152 int proc2 = group2->index(i);
153 int proc1 = this->rank(proc2);
154 if (proc1 == MPI_UNDEFINED) {
159 *newgroup = MPI_GROUP_EMPTY;
161 *newgroup = new Group(size1);
162 size2 = this->size();
163 for (int i = 0; i < size2; i++) {
164 int proc1 = this->index(i);
165 (*newgroup)->set_mapping(proc1, i);
167 for (int i = size2; i < size1; i++) {
168 int proc2 = group2->index(i - size2);
169 (*newgroup)->set_mapping(proc2, i);
175 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
177 int size2 = group2->size();
178 for (int i = 0; i < size2; i++) {
179 int proc2 = group2->index(i);
180 int proc1 = this->rank(proc2);
181 if (proc1 == MPI_UNDEFINED) {
186 *newgroup = MPI_GROUP_EMPTY;
188 *newgroup = new Group(size2);
190 for (int i = 0; i < group2->size(); i++) {
191 int proc2 = group2->index(i);
192 int proc1 = this->rank(proc2);
193 if (proc1 != MPI_UNDEFINED) {
194 (*newgroup)->set_mapping(proc2, j);
202 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
206 for (int i = 0; i < size2; i++) {
207 int proc1 = this->index(i);
208 int proc2 = group2->rank(proc1);
209 if (proc2 != MPI_UNDEFINED) {
214 *newgroup = MPI_GROUP_EMPTY;
216 *newgroup = new Group(newsize);
217 for (int i = 0; i < size2; i++) {
218 int proc1 = this->index(i);
219 int proc2 = group2->rank(proc1);
220 if (proc2 == MPI_UNDEFINED) {
221 (*newgroup)->set_mapping(proc1, i);
228 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
230 int newsize = oldsize - n;
231 *newgroup = new Group(newsize);
232 int* to_exclude=xbt_new0(int, size_);
233 for (int i = 0; i < oldsize; i++)
235 for (int i = 0; i < n; i++)
236 to_exclude[ranks[i]]=1;
238 for (int i = 0; i < oldsize; i++) {
239 if(to_exclude[i]==0){
240 int index = this->index(i);
241 (*newgroup)->set_mapping(index, j);
245 xbt_free(to_exclude);
250 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
252 for (int i = 0; i < n; i++) {
253 for (int rank = ranges[i][0]; /* First */
254 rank >= 0 && rank < size_; /* Last */
257 if(rank == ranges[i][1]){/*already last ?*/
260 rank += ranges[i][2]; /* Stride */
261 if (ranges[i][0] < ranges[i][1]) {
262 if (rank > ranges[i][1])
265 if (rank < ranges[i][1])
270 *newgroup = new Group(newsize);
272 for (int i = 0; i < n; i++) {
273 for (int rank = ranges[i][0]; /* First */
274 rank >= 0 && rank < size_; /* Last */
276 int index = this->index(rank);
277 (*newgroup)->set_mapping(index, j);
279 if(rank == ranges[i][1]){/*already last ?*/
282 rank += ranges[i][2]; /* Stride */
283 if (ranges[i][0] < ranges[i][1]) {
284 if (rank > ranges[i][1])
287 if (rank < ranges[i][1])
295 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
297 for (int i = 0; i < n; i++) {
298 for (int rank = ranges[i][0]; /* First */
299 rank >= 0 && rank < size_; /* Last */
302 if(rank == ranges[i][1]){/*already last ?*/
305 rank += ranges[i][2]; /* Stride */
306 if (ranges[i][0] < ranges[i][1]) {
307 if (rank > ranges[i][1])
310 if (rank < ranges[i][1])
316 *newgroup = MPI_GROUP_EMPTY;
318 *newgroup = new Group(newsize);
321 while (newrank < newsize) {
323 for (int i = 0; i < n; i++) {
324 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
329 if(rank == ranges[i][1]){/*already last ?*/
332 rank += ranges[i][2]; /* Stride */
333 if (ranges[i][0]<ranges[i][1]){
334 if (rank > ranges[i][1])
337 if (rank < ranges[i][1])
343 int index = this->index(oldrank);
344 (*newgroup)->set_mapping(index, newrank);
353 MPI_Group Group::f2c(int id) {
355 return MPI_GROUP_EMPTY;
356 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
358 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
360 return static_cast<MPI_Group>(MPI_GROUP_NULL);