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_;
40 void Group::set_mapping(int index, int rank)
42 if (0 <= rank && rank < size_) {
43 rank_to_index_map_[rank] = index;
44 if (index != MPI_UNDEFINED) {
45 if ((unsigned)index >= index_to_rank_map_.size())
46 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
47 index_to_rank_map_[index] = rank;
52 int Group::index(int rank)
55 if (0 <= rank && rank < size_)
56 index = rank_to_index_map_[rank];
58 index = MPI_UNDEFINED;
62 int Group::rank(int index)
65 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
66 rank = index_to_rank_map_[index];
77 void Group::unref(Group* group)
80 if (group->refcount_ <= 0) {
90 int Group::compare(MPI_Group group2)
95 if (size_ != group2->size()) {
98 int sz = group2->size();
99 for (int i = 0; i < sz; i++) {
100 int index = this->index(i);
101 int rank = group2->rank(index);
102 if (rank == MPI_UNDEFINED) {
103 result = MPI_UNEQUAL;
107 result = MPI_SIMILAR;
114 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
119 *newgroup = MPI_GROUP_EMPTY;
120 } else if (n == size_) {
122 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
125 *newgroup = new Group(n);
126 for (i = 0; i < n; i++) {
127 index = this->index(ranks[i]);
128 (*newgroup)->set_mapping(index, i);
134 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
137 int size2 = group2->size();
138 for (int i = 0; i < size2; i++) {
139 int proc2 = group2->index(i);
140 int proc1 = this->rank(proc2);
141 if (proc1 == MPI_UNDEFINED) {
146 *newgroup = MPI_GROUP_EMPTY;
148 *newgroup = new Group(size1);
149 size2 = this->size();
150 for (int i = 0; i < size2; i++) {
151 int proc1 = this->index(i);
152 (*newgroup)->set_mapping(proc1, i);
154 for (int i = size2; i < size1; i++) {
155 int proc2 = group2->index(i - size2);
156 (*newgroup)->set_mapping(proc2, i);
162 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
164 int size2 = group2->size();
165 for (int i = 0; i < size2; i++) {
166 int proc2 = group2->index(i);
167 int proc1 = this->rank(proc2);
168 if (proc1 == MPI_UNDEFINED) {
173 *newgroup = MPI_GROUP_EMPTY;
175 *newgroup = new Group(size2);
177 for (int i = 0; i < group2->size(); i++) {
178 int proc2 = group2->index(i);
179 int proc1 = this->rank(proc2);
180 if (proc1 != MPI_UNDEFINED) {
181 (*newgroup)->set_mapping(proc2, j);
189 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
193 for (int i = 0; i < size2; i++) {
194 int proc1 = this->index(i);
195 int proc2 = group2->rank(proc1);
196 if (proc2 != MPI_UNDEFINED) {
201 *newgroup = MPI_GROUP_EMPTY;
203 *newgroup = new Group(newsize);
204 for (int i = 0; i < size2; i++) {
205 int proc1 = this->index(i);
206 int proc2 = group2->rank(proc1);
207 if (proc2 == MPI_UNDEFINED) {
208 (*newgroup)->set_mapping(proc1, i);
215 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
217 int newsize = oldsize - n;
218 *newgroup = new Group(newsize);
219 int* to_exclude = new int[size_];
220 for (int i = 0; i < oldsize; i++)
222 for (int i = 0; i < n; i++)
223 to_exclude[ranks[i]]=1;
225 for (int i = 0; i < oldsize; i++) {
226 if(to_exclude[i]==0){
227 int index = this->index(i);
228 (*newgroup)->set_mapping(index, j);
237 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
239 for (int i = 0; i < n; i++) {
240 for (int rank = ranges[i][0]; /* First */
241 rank >= 0 && rank < size_; /* Last */
244 if(rank == ranges[i][1]){/*already last ?*/
247 rank += ranges[i][2]; /* Stride */
248 if (ranges[i][0] < ranges[i][1]) {
249 if (rank > ranges[i][1])
252 if (rank < ranges[i][1])
257 *newgroup = new Group(newsize);
259 for (int i = 0; i < n; i++) {
260 for (int rank = ranges[i][0]; /* First */
261 rank >= 0 && rank < size_; /* Last */
263 int index = this->index(rank);
264 (*newgroup)->set_mapping(index, j);
266 if(rank == ranges[i][1]){/*already last ?*/
269 rank += ranges[i][2]; /* Stride */
270 if (ranges[i][0] < ranges[i][1]) {
271 if (rank > ranges[i][1])
274 if (rank < ranges[i][1])
282 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
284 for (int i = 0; i < n; i++) {
285 for (int rank = ranges[i][0]; /* First */
286 rank >= 0 && rank < size_; /* Last */
289 if(rank == ranges[i][1]){/*already last ?*/
292 rank += ranges[i][2]; /* Stride */
293 if (ranges[i][0] < ranges[i][1]) {
294 if (rank > ranges[i][1])
297 if (rank < ranges[i][1])
303 *newgroup = MPI_GROUP_EMPTY;
305 *newgroup = new Group(newsize);
308 while (newrank < newsize) {
310 for (int i = 0; i < n; i++) {
311 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
316 if(rank == ranges[i][1]){/*already last ?*/
319 rank += ranges[i][2]; /* Stride */
320 if (ranges[i][0]<ranges[i][1]){
321 if (rank > ranges[i][1])
324 if (rank < ranges[i][1])
330 int index = this->index(oldrank);
331 (*newgroup)->set_mapping(index, newrank);
340 MPI_Group Group::f2c(int id) {
342 return MPI_GROUP_EMPTY;
343 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
345 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
347 return static_cast<MPI_Group>(MPI_GROUP_NULL);