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()
131 && this != MPI_COMM_SELF->group()
132 && this != MPI_GROUP_EMPTY)
135 *newgroup = new Group(n);
136 for (i = 0; i < n; i++) {
137 index = this->index(ranks[i]);
138 (*newgroup)->set_mapping(index, i);
144 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
147 int size2 = group2->size();
148 for (int i = 0; i < size2; i++) {
149 int proc2 = group2->index(i);
150 int proc1 = this->rank(proc2);
151 if (proc1 == MPI_UNDEFINED) {
156 *newgroup = MPI_GROUP_EMPTY;
158 *newgroup = new Group(size1);
159 size2 = this->size();
160 for (int i = 0; i < size2; i++) {
161 int proc1 = this->index(i);
162 (*newgroup)->set_mapping(proc1, i);
164 for (int i = size2; i < size1; i++) {
165 int proc2 = group2->index(i - size2);
166 (*newgroup)->set_mapping(proc2, i);
172 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
174 int size2 = group2->size();
175 for (int i = 0; i < size2; i++) {
176 int proc2 = group2->index(i);
177 int proc1 = this->rank(proc2);
178 if (proc1 == MPI_UNDEFINED) {
183 *newgroup = MPI_GROUP_EMPTY;
185 *newgroup = new Group(size2);
187 for (int i = 0; i < group2->size(); i++) {
188 int proc2 = group2->index(i);
189 int proc1 = this->rank(proc2);
190 if (proc1 != MPI_UNDEFINED) {
191 (*newgroup)->set_mapping(proc2, j);
199 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
203 for (int i = 0; i < size2; i++) {
204 int proc1 = this->index(i);
205 int proc2 = group2->rank(proc1);
206 if (proc2 != MPI_UNDEFINED) {
211 *newgroup = MPI_GROUP_EMPTY;
213 *newgroup = new Group(newsize);
214 for (int i = 0; i < size2; i++) {
215 int proc1 = this->index(i);
216 int proc2 = group2->rank(proc1);
217 if (proc2 == MPI_UNDEFINED) {
218 (*newgroup)->set_mapping(proc1, i);
225 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
227 int newsize = oldsize - n;
228 *newgroup = new Group(newsize);
229 int* to_exclude=xbt_new0(int, size_);
230 for (int i = 0; i < oldsize; i++)
232 for (int i = 0; i < n; i++)
233 to_exclude[ranks[i]]=1;
235 for (int i = 0; i < oldsize; i++) {
236 if(to_exclude[i]==0){
237 int index = this->index(i);
238 (*newgroup)->set_mapping(index, j);
242 xbt_free(to_exclude);
247 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
249 for (int i = 0; i < n; i++) {
250 for (int rank = ranges[i][0]; /* First */
251 rank >= 0 && rank < size_; /* Last */
254 if(rank == ranges[i][1]){/*already last ?*/
257 rank += ranges[i][2]; /* Stride */
258 if (ranges[i][0] < ranges[i][1]) {
259 if (rank > ranges[i][1])
262 if (rank < ranges[i][1])
267 *newgroup = new Group(newsize);
269 for (int i = 0; i < n; i++) {
270 for (int rank = ranges[i][0]; /* First */
271 rank >= 0 && rank < size_; /* Last */
273 int index = this->index(rank);
274 (*newgroup)->set_mapping(index, j);
276 if(rank == ranges[i][1]){/*already last ?*/
279 rank += ranges[i][2]; /* Stride */
280 if (ranges[i][0] < ranges[i][1]) {
281 if (rank > ranges[i][1])
284 if (rank < ranges[i][1])
292 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
294 for (int i = 0; i < n; i++) {
295 for (int rank = ranges[i][0]; /* First */
296 rank >= 0 && rank < size_; /* Last */
299 if(rank == ranges[i][1]){/*already last ?*/
302 rank += ranges[i][2]; /* Stride */
303 if (ranges[i][0] < ranges[i][1]) {
304 if (rank > ranges[i][1])
307 if (rank < ranges[i][1])
313 *newgroup = MPI_GROUP_EMPTY;
315 *newgroup = new Group(newsize);
318 while (newrank < newsize) {
320 for (int i = 0; i < n; i++) {
321 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
326 if(rank == ranges[i][1]){/*already last ?*/
329 rank += ranges[i][2]; /* Stride */
330 if (ranges[i][0]<ranges[i][1]){
331 if (rank > ranges[i][1])
334 if (rank < ranges[i][1])
340 int index = this->index(oldrank);
341 (*newgroup)->set_mapping(index, newrank);
350 MPI_Group Group::f2c(int id) {
352 return MPI_GROUP_EMPTY;
353 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
355 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
357 return static_cast<MPI_Group>(MPI_GROUP_NULL);