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 "src/smpi/smpi_comm.hpp"
7 #include "src/smpi/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 index_to_rank_map_=nullptr; /* index_to_rank_map_ */
22 refcount_=1; /* refcount_: start > 0 so that this group never gets freed */
25 Group::Group(int n) : size_(n)
27 rank_to_index_map_ = xbt_new(int, size_);
28 index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
30 for (int i = 0; i < size_; i++) {
31 rank_to_index_map_[i] = MPI_UNDEFINED;
35 Group::Group(MPI_Group origin)
37 if(origin != MPI_GROUP_NULL
38 && origin != MPI_GROUP_EMPTY)
40 size_ = origin->size();
41 rank_to_index_map_ = xbt_new(int, size_);
42 index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
44 for (int i = 0; i < size_; i++) {
45 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
50 xbt_dict_cursor_t cursor = nullptr;
51 xbt_dict_foreach(origin->index_to_rank_map_, cursor, key, ptr_rank) {
52 int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
53 *cp=*reinterpret_cast<int*>(ptr_rank);
54 xbt_dict_set(index_to_rank_map_, key, cp, nullptr);
61 xbt_free(rank_to_index_map_);
62 xbt_dict_free(&index_to_rank_map_);
65 void Group::set_mapping(int index, int rank)
70 rank_to_index_map_[rank] = index;
71 if (index!=MPI_UNDEFINED ) {
72 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
75 char * key = bprintf("%d", index);
76 xbt_dict_set(index_to_rank_map_, key, val_rank, nullptr);
82 int Group::index(int rank)
84 int index = MPI_UNDEFINED;
86 if (0 <= rank && rank < size_) {
87 index = rank_to_index_map_[rank];
92 int Group::rank(int index)
94 int * ptr_rank = nullptr;
95 if (this==MPI_GROUP_EMPTY)
97 char * key = bprintf("%d", index);
98 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(index_to_rank_map_, key));
101 if (ptr_rank==nullptr)
102 return MPI_UNDEFINED;
111 void Group::unref(Group* group)
114 if (group->refcount_ <= 0) {
124 int Group::compare(MPI_Group group2)
131 if (size_ != group2->size()) {
132 result = MPI_UNEQUAL;
134 int sz = group2->size();
135 for (i = 0; i < sz; i++) {
136 index = this->index(i);
137 int rank = group2->rank(index);
138 if (rank == MPI_UNDEFINED) {
139 result = MPI_UNEQUAL;
143 result = MPI_SIMILAR;
150 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
155 *newgroup = MPI_GROUP_EMPTY;
156 } else if (n == size_) {
158 if(this!= MPI_COMM_WORLD->group()
159 && this != MPI_COMM_SELF->group()
160 && this != MPI_GROUP_EMPTY)
163 *newgroup = new Group(n);
164 for (i = 0; i < n; i++) {
165 index = this->index(ranks[i]);
166 (*newgroup)->set_mapping(index, i);
172 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
175 int size2 = group2->size();
176 for (int i = 0; i < size2; i++) {
177 int proc2 = group2->index(i);
178 int proc1 = this->rank(proc2);
179 if (proc1 == MPI_UNDEFINED) {
184 *newgroup = MPI_GROUP_EMPTY;
186 *newgroup = new Group(size1);
187 size2 = this->size();
188 for (int i = 0; i < size2; i++) {
189 int proc1 = this->index(i);
190 (*newgroup)->set_mapping(proc1, i);
192 for (int i = size2; i < size1; i++) {
193 int proc2 = group2->index(i - size2);
194 (*newgroup)->set_mapping(proc2, i);
200 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
202 int size2 = group2->size();
203 for (int i = 0; i < size2; i++) {
204 int proc2 = group2->index(i);
205 int proc1 = this->rank(proc2);
206 if (proc1 == MPI_UNDEFINED) {
211 *newgroup = MPI_GROUP_EMPTY;
213 *newgroup = new Group(size2);
215 for (int i = 0; i < group2->size(); i++) {
216 int proc2 = group2->index(i);
217 int proc1 = this->rank(proc2);
218 if (proc1 != MPI_UNDEFINED) {
219 (*newgroup)->set_mapping(proc2, j);
227 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
231 for (int i = 0; i < size2; i++) {
232 int proc1 = this->index(i);
233 int proc2 = group2->rank(proc1);
234 if (proc2 != MPI_UNDEFINED) {
239 *newgroup = MPI_GROUP_EMPTY;
241 *newgroup = new Group(newsize);
242 for (int i = 0; i < size2; i++) {
243 int proc1 = this->index(i);
244 int proc2 = group2->rank(proc1);
245 if (proc2 == MPI_UNDEFINED) {
246 (*newgroup)->set_mapping(proc1, i);
253 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
255 int newsize = oldsize - n;
256 *newgroup = new Group(newsize);
257 int* to_exclude=xbt_new0(int, size_);
258 for (int i = 0; i < oldsize; i++)
260 for (int i = 0; i < n; i++)
261 to_exclude[ranks[i]]=1;
263 for (int i = 0; i < oldsize; i++) {
264 if(to_exclude[i]==0){
265 int index = this->index(i);
266 (*newgroup)->set_mapping(index, j);
270 xbt_free(to_exclude);
275 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
277 for (int i = 0; i < n; i++) {
278 for (int rank = ranges[i][0]; /* First */
279 rank >= 0 && rank < size_; /* Last */
282 if(rank == ranges[i][1]){/*already last ?*/
285 rank += ranges[i][2]; /* Stride */
286 if (ranges[i][0] < ranges[i][1]) {
287 if (rank > ranges[i][1])
290 if (rank < ranges[i][1])
295 *newgroup = new Group(newsize);
297 for (int i = 0; i < n; i++) {
298 for (int rank = ranges[i][0]; /* First */
299 rank >= 0 && rank < size_; /* Last */
301 int index = this->index(rank);
302 (*newgroup)->set_mapping(index, j);
304 if(rank == ranges[i][1]){/*already last ?*/
307 rank += ranges[i][2]; /* Stride */
308 if (ranges[i][0] < ranges[i][1]) {
309 if (rank > ranges[i][1])
312 if (rank < ranges[i][1])
320 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
322 for (int i = 0; i < n; i++) {
323 for (int rank = ranges[i][0]; /* First */
324 rank >= 0 && rank < size_; /* Last */
327 if(rank == ranges[i][1]){/*already last ?*/
330 rank += ranges[i][2]; /* Stride */
331 if (ranges[i][0] < ranges[i][1]) {
332 if (rank > ranges[i][1])
335 if (rank < ranges[i][1])
341 *newgroup = MPI_GROUP_EMPTY;
343 *newgroup = new Group(newsize);
346 while (newrank < newsize) {
348 for (int i = 0; i < n; i++) {
349 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
354 if(rank == ranges[i][1]){/*already last ?*/
357 rank += ranges[i][2]; /* Stride */
358 if (ranges[i][0]<ranges[i][1]){
359 if (rank > ranges[i][1])
362 if (rank < ranges[i][1])
368 int index = this->index(oldrank);
369 (*newgroup)->set_mapping(index, newrank);
378 MPI_Group Group::f2c(int id) {
380 return MPI_GROUP_EMPTY;
381 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
383 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
385 return static_cast<MPI_Group>(MPI_GROUP_NULL);