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)
28 rank_to_index_map_ = xbt_new(int, size_);
29 index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
31 for (i = 0; i < size_; i++) {
32 rank_to_index_map_[i] = MPI_UNDEFINED;
36 Group::Group(MPI_Group origin)
40 xbt_dict_cursor_t cursor = nullptr;
43 if(origin != MPI_GROUP_NULL
44 && origin != MPI_GROUP_EMPTY)
46 size_ = origin->size();
47 rank_to_index_map_ = xbt_new(int, size_);
48 index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
50 for (i = 0; i < size_; i++) {
51 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
54 xbt_dict_foreach(origin->index_to_rank_map_, cursor, key, ptr_rank) {
55 int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
56 *cp=*reinterpret_cast<int*>(ptr_rank);
57 xbt_dict_set(index_to_rank_map_, key, cp, nullptr);
64 xbt_free(rank_to_index_map_);
65 xbt_dict_free(&index_to_rank_map_);
68 void Group::set_mapping(int index, int rank)
73 rank_to_index_map_[rank] = index;
74 if (index!=MPI_UNDEFINED ) {
75 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
78 char * key = bprintf("%d", index);
79 xbt_dict_set(index_to_rank_map_, key, val_rank, nullptr);
85 int Group::index(int rank)
87 int index = MPI_UNDEFINED;
89 if (0 <= rank && rank < size_) {
90 index = rank_to_index_map_[rank];
95 int Group::rank(int index)
97 int * ptr_rank = nullptr;
98 if (this==MPI_GROUP_EMPTY)
100 char * key = bprintf("%d", index);
101 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(index_to_rank_map_, key));
104 if (ptr_rank==nullptr)
105 return MPI_UNDEFINED;
114 void Group::unref(Group* group)
117 if (group->refcount_ <= 0) {
127 int Group::compare(MPI_Group group2)
136 if (size_ != group2->size()) {
137 result = MPI_UNEQUAL;
140 for (i = 0; i < sz; i++) {
141 index = this->index(i);
142 rank = group2->rank(index);
143 if (rank == MPI_UNDEFINED) {
144 result = MPI_UNEQUAL;
148 result = MPI_SIMILAR;
155 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
160 *newgroup = MPI_GROUP_EMPTY;
161 } else if (n == size_) {
163 if(this!= MPI_COMM_WORLD->group()
164 && this != MPI_COMM_SELF->group()
165 && this != MPI_GROUP_EMPTY)
168 *newgroup = new Group(n);
169 for (i = 0; i < n; i++) {
170 index = this->index(ranks[i]);
171 (*newgroup)->set_mapping(index, i);
177 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
180 int size2 = group2->size();
181 for (int i = 0; i < size2; i++) {
182 int proc2 = group2->index(i);
183 int proc1 = this->rank(proc2);
184 if (proc1 == MPI_UNDEFINED) {
189 *newgroup = MPI_GROUP_EMPTY;
191 *newgroup = new Group(size1);
192 size2 = this->size();
193 for (int i = 0; i < size2; i++) {
194 int proc1 = this->index(i);
195 (*newgroup)->set_mapping(proc1, i);
197 for (int i = size2; i < size1; i++) {
198 int proc2 = group2->index(i - size2);
199 (*newgroup)->set_mapping(proc2, i);
205 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
207 int size2 = group2->size();
208 for (int i = 0; i < size2; i++) {
209 int proc2 = group2->index(i);
210 int proc1 = this->rank(proc2);
211 if (proc1 == MPI_UNDEFINED) {
216 *newgroup = MPI_GROUP_EMPTY;
218 *newgroup = new Group(size2);
220 for (int i = 0; i < group2->size(); i++) {
221 int proc2 = group2->index(i);
222 int proc1 = this->rank(proc2);
223 if (proc1 != MPI_UNDEFINED) {
224 (*newgroup)->set_mapping(proc2, j);
232 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
236 for (int i = 0; i < size2; i++) {
237 int proc1 = this->index(i);
238 int proc2 = group2->rank(proc1);
239 if (proc2 != MPI_UNDEFINED) {
244 *newgroup = MPI_GROUP_EMPTY;
246 *newgroup = new Group(newsize);
247 for (int i = 0; i < size2; i++) {
248 int proc1 = this->index(i);
249 int proc2 = group2->rank(proc1);
250 if (proc2 == MPI_UNDEFINED) {
251 (*newgroup)->set_mapping(proc1, i);
258 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
260 int newsize = oldsize - n;
261 *newgroup = new Group(newsize);
262 int* to_exclude=xbt_new0(int, size_);
263 for (int i = 0; i < oldsize; i++)
265 for (int i = 0; i < n; i++)
266 to_exclude[ranks[i]]=1;
268 for (int i = 0; i < oldsize; i++) {
269 if(to_exclude[i]==0){
270 int index = this->index(i);
271 (*newgroup)->set_mapping(index, j);
275 xbt_free(to_exclude);
280 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
282 for (int i = 0; i < n; i++) {
283 for (int rank = ranges[i][0]; /* First */
284 rank >= 0 && rank < size_; /* Last */
287 if(rank == ranges[i][1]){/*already last ?*/
290 rank += ranges[i][2]; /* Stride */
291 if (ranges[i][0] < ranges[i][1]) {
292 if (rank > ranges[i][1])
295 if (rank < ranges[i][1])
300 *newgroup = new Group(newsize);
302 for (int i = 0; i < n; i++) {
303 for (int rank = ranges[i][0]; /* First */
304 rank >= 0 && rank < size_; /* Last */
306 int index = this->index(rank);
307 (*newgroup)->set_mapping(index, j);
309 if(rank == ranges[i][1]){/*already last ?*/
312 rank += ranges[i][2]; /* Stride */
313 if (ranges[i][0] < ranges[i][1]) {
314 if (rank > ranges[i][1])
317 if (rank < ranges[i][1])
325 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
327 for (int i = 0; i < n; i++) {
328 for (int rank = ranges[i][0]; /* First */
329 rank >= 0 && rank < size_; /* Last */
332 if(rank == ranges[i][1]){/*already last ?*/
335 rank += ranges[i][2]; /* Stride */
336 if (ranges[i][0] < ranges[i][1]) {
337 if (rank > ranges[i][1])
340 if (rank < ranges[i][1])
346 *newgroup = MPI_GROUP_EMPTY;
348 *newgroup = new Group(newsize);
351 while (newrank < newsize) {
353 for (int i = 0; i < n; i++) {
354 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
359 if(rank == ranges[i][1]){/*already last ?*/
362 rank += ranges[i][2]; /* Stride */
363 if (ranges[i][0]<ranges[i][1]){
364 if (rank > ranges[i][1])
367 if (rank < ranges[i][1])
373 int index = this->index(oldrank);
374 (*newgroup)->set_mapping(index, newrank);
383 MPI_Group Group::f2c(int id) {
385 return MPI_GROUP_EMPTY;
386 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
388 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
390 return static_cast<MPI_Group>(MPI_GROUP_NULL);