1 /* Copyright (c) 2010, 2013-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
10 Group mpi_MPI_GROUP_EMPTY;
11 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
19 rank_to_index_map_=nullptr; /* rank_to_index_map_ */
20 index_to_rank_map_=nullptr; /* index_to_rank_map_ */
21 refcount_=1; /* refcount_: start > 0 so that this group never gets freed */
24 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 (i = 0; i < size_; i++) {
31 rank_to_index_map_[i] = MPI_UNDEFINED;
35 Group::Group(MPI_Group origin)
39 xbt_dict_cursor_t cursor = nullptr;
42 if(origin != MPI_GROUP_NULL
43 && origin != MPI_GROUP_EMPTY)
45 size_ = origin->size();
46 rank_to_index_map_ = xbt_new(int, size_);
47 index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
49 for (i = 0; i < size_; i++) {
50 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
53 xbt_dict_foreach(origin->index_to_rank_map_, cursor, key, ptr_rank) {
54 int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
55 *cp=*reinterpret_cast<int*>(ptr_rank);
56 xbt_dict_set(index_to_rank_map_, key, cp, nullptr);
63 xbt_free(rank_to_index_map_);
64 xbt_dict_free(&index_to_rank_map_);
69 if(this != MPI_COMM_WORLD->group()
70 && this != MPI_GROUP_EMPTY)
74 void Group::set_mapping(int index, int rank)
79 rank_to_index_map_[rank] = index;
80 if (index!=MPI_UNDEFINED ) {
81 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
84 char * key = bprintf("%d", index);
85 xbt_dict_set(index_to_rank_map_, key, val_rank, nullptr);
91 int Group::index(int rank)
93 int index = MPI_UNDEFINED;
95 if (0 <= rank && rank < size_) {
96 index = rank_to_index_map_[rank];
101 int Group::rank(int index)
103 int * ptr_rank = nullptr;
104 if (this==MPI_GROUP_EMPTY)
105 return MPI_UNDEFINED;
106 char * key = bprintf("%d", index);
107 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(index_to_rank_map_, key));
110 if (ptr_rank==nullptr)
111 return MPI_UNDEFINED;
124 if (refcount_ <= 0) {
136 int Group::compare(MPI_Group group2)
145 if (size_ != group2->size()) {
146 result = MPI_UNEQUAL;
149 for (i = 0; i < sz; i++) {
150 index = this->index(i);
151 rank = group2->rank(index);
152 if (rank == MPI_UNDEFINED) {
153 result = MPI_UNEQUAL;
157 result = MPI_SIMILAR;
164 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
169 *newgroup = MPI_GROUP_EMPTY;
170 } else if (n == size_) {
172 if(this!= MPI_COMM_WORLD->group()
173 && this != MPI_COMM_SELF->group()
174 && this != MPI_GROUP_EMPTY)
177 *newgroup = new Group(n);
178 for (i = 0; i < n; i++) {
179 index = this->index(ranks[i]);
180 (*newgroup)->set_mapping(index, i);
186 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
189 int size2 = group2->size();
190 for (int i = 0; i < size2; i++) {
191 int proc2 = group2->index(i);
192 int proc1 = this->rank(proc2);
193 if (proc1 == MPI_UNDEFINED) {
198 *newgroup = MPI_GROUP_EMPTY;
200 *newgroup = new Group(size1);
201 size2 = this->size();
202 for (int i = 0; i < size2; i++) {
203 int proc1 = this->index(i);
204 (*newgroup)->set_mapping(proc1, i);
206 for (int i = size2; i < size1; i++) {
207 int proc2 = group2->index(i - size2);
208 (*newgroup)->set_mapping(proc2, i);
214 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
216 int size2 = group2->size();
217 for (int i = 0; i < size2; i++) {
218 int proc2 = group2->index(i);
219 int proc1 = this->rank(proc2);
220 if (proc1 == MPI_UNDEFINED) {
225 *newgroup = MPI_GROUP_EMPTY;
227 *newgroup = new Group(size2);
229 for (int i = 0; i < group2->size(); i++) {
230 int proc2 = group2->index(i);
231 int proc1 = this->rank(proc2);
232 if (proc1 != MPI_UNDEFINED) {
233 (*newgroup)->set_mapping(proc2, j);
241 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
245 for (int i = 0; i < size2; i++) {
246 int proc1 = this->index(i);
247 int proc2 = group2->rank(proc1);
248 if (proc2 != MPI_UNDEFINED) {
253 *newgroup = MPI_GROUP_EMPTY;
255 *newgroup = new Group(newsize);
256 for (int i = 0; i < size2; i++) {
257 int proc1 = this->index(i);
258 int proc2 = group2->rank(proc1);
259 if (proc2 == MPI_UNDEFINED) {
260 (*newgroup)->set_mapping(proc1, i);
267 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
269 int newsize = oldsize - n;
270 *newgroup = new Group(newsize);
271 int* to_exclude=xbt_new0(int, size_);
272 for (int i = 0; i < oldsize; i++)
274 for (int i = 0; i < n; i++)
275 to_exclude[ranks[i]]=1;
277 for (int i = 0; i < oldsize; i++) {
278 if(to_exclude[i]==0){
279 int index = this->index(i);
280 (*newgroup)->set_mapping(index, j);
284 xbt_free(to_exclude);
289 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
291 for (int i = 0; i < n; i++) {
292 for (int rank = ranges[i][0]; /* First */
293 rank >= 0 && rank < size_; /* Last */
296 if(rank == ranges[i][1]){/*already last ?*/
299 rank += ranges[i][2]; /* Stride */
300 if (ranges[i][0] < ranges[i][1]) {
301 if (rank > ranges[i][1])
304 if (rank < ranges[i][1])
309 *newgroup = new Group(newsize);
311 for (int i = 0; i < n; i++) {
312 for (int rank = ranges[i][0]; /* First */
313 rank >= 0 && rank < size_; /* Last */
315 int index = this->index(rank);
316 (*newgroup)->set_mapping(index, j);
318 if(rank == ranges[i][1]){/*already last ?*/
321 rank += ranges[i][2]; /* Stride */
322 if (ranges[i][0] < ranges[i][1]) {
323 if (rank > ranges[i][1])
326 if (rank < ranges[i][1])
334 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
336 for (int i = 0; i < n; i++) {
337 for (int rank = ranges[i][0]; /* First */
338 rank >= 0 && rank < size_; /* Last */
341 if(rank == ranges[i][1]){/*already last ?*/
344 rank += ranges[i][2]; /* Stride */
345 if (ranges[i][0] < ranges[i][1]) {
346 if (rank > ranges[i][1])
349 if (rank < ranges[i][1])
355 *newgroup = MPI_GROUP_EMPTY;
357 *newgroup = new Group(newsize);
360 while (newrank < newsize) {
362 for (int i = 0; i < n; i++) {
363 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
368 if(rank == ranges[i][1]){/*already last ?*/
371 rank += ranges[i][2]; /* Stride */
372 if (ranges[i][0]<ranges[i][1]){
373 if (rank > ranges[i][1])
376 if (rank < ranges[i][1])
382 int index = this->index(oldrank);
383 (*newgroup)->set_mapping(index, newrank);