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_);
67 void Group::set_mapping(int index, int rank)
72 rank_to_index_map_[rank] = index;
73 if (index!=MPI_UNDEFINED ) {
74 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
77 char * key = bprintf("%d", index);
78 xbt_dict_set(index_to_rank_map_, key, val_rank, nullptr);
84 int Group::index(int rank)
86 int index = MPI_UNDEFINED;
88 if (0 <= rank && rank < size_) {
89 index = rank_to_index_map_[rank];
94 int Group::rank(int index)
96 int * ptr_rank = nullptr;
97 if (this==MPI_GROUP_EMPTY)
99 char * key = bprintf("%d", index);
100 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(index_to_rank_map_, key));
103 if (ptr_rank==nullptr)
104 return MPI_UNDEFINED;
113 void Group::unref(Group* group)
116 if (group->refcount_ <= 0) {
126 int Group::compare(MPI_Group group2)
135 if (size_ != group2->size()) {
136 result = MPI_UNEQUAL;
139 for (i = 0; i < sz; i++) {
140 index = this->index(i);
141 rank = group2->rank(index);
142 if (rank == MPI_UNDEFINED) {
143 result = MPI_UNEQUAL;
147 result = MPI_SIMILAR;
154 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
159 *newgroup = MPI_GROUP_EMPTY;
160 } else if (n == size_) {
162 if(this!= MPI_COMM_WORLD->group()
163 && this != MPI_COMM_SELF->group()
164 && this != MPI_GROUP_EMPTY)
167 *newgroup = new Group(n);
168 for (i = 0; i < n; i++) {
169 index = this->index(ranks[i]);
170 (*newgroup)->set_mapping(index, i);
176 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
179 int size2 = group2->size();
180 for (int i = 0; i < size2; i++) {
181 int proc2 = group2->index(i);
182 int proc1 = this->rank(proc2);
183 if (proc1 == MPI_UNDEFINED) {
188 *newgroup = MPI_GROUP_EMPTY;
190 *newgroup = new Group(size1);
191 size2 = this->size();
192 for (int i = 0; i < size2; i++) {
193 int proc1 = this->index(i);
194 (*newgroup)->set_mapping(proc1, i);
196 for (int i = size2; i < size1; i++) {
197 int proc2 = group2->index(i - size2);
198 (*newgroup)->set_mapping(proc2, i);
204 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
206 int size2 = group2->size();
207 for (int i = 0; i < size2; i++) {
208 int proc2 = group2->index(i);
209 int proc1 = this->rank(proc2);
210 if (proc1 == MPI_UNDEFINED) {
215 *newgroup = MPI_GROUP_EMPTY;
217 *newgroup = new Group(size2);
219 for (int i = 0; i < group2->size(); i++) {
220 int proc2 = group2->index(i);
221 int proc1 = this->rank(proc2);
222 if (proc1 != MPI_UNDEFINED) {
223 (*newgroup)->set_mapping(proc2, j);
231 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
235 for (int i = 0; i < size2; i++) {
236 int proc1 = this->index(i);
237 int proc2 = group2->rank(proc1);
238 if (proc2 != MPI_UNDEFINED) {
243 *newgroup = MPI_GROUP_EMPTY;
245 *newgroup = new Group(newsize);
246 for (int i = 0; i < size2; i++) {
247 int proc1 = this->index(i);
248 int proc2 = group2->rank(proc1);
249 if (proc2 == MPI_UNDEFINED) {
250 (*newgroup)->set_mapping(proc1, i);
257 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
259 int newsize = oldsize - n;
260 *newgroup = new Group(newsize);
261 int* to_exclude=xbt_new0(int, size_);
262 for (int i = 0; i < oldsize; i++)
264 for (int i = 0; i < n; i++)
265 to_exclude[ranks[i]]=1;
267 for (int i = 0; i < oldsize; i++) {
268 if(to_exclude[i]==0){
269 int index = this->index(i);
270 (*newgroup)->set_mapping(index, j);
274 xbt_free(to_exclude);
279 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
281 for (int i = 0; i < n; i++) {
282 for (int rank = ranges[i][0]; /* First */
283 rank >= 0 && rank < size_; /* Last */
286 if(rank == ranges[i][1]){/*already last ?*/
289 rank += ranges[i][2]; /* Stride */
290 if (ranges[i][0] < ranges[i][1]) {
291 if (rank > ranges[i][1])
294 if (rank < ranges[i][1])
299 *newgroup = new Group(newsize);
301 for (int i = 0; i < n; i++) {
302 for (int rank = ranges[i][0]; /* First */
303 rank >= 0 && rank < size_; /* Last */
305 int index = this->index(rank);
306 (*newgroup)->set_mapping(index, j);
308 if(rank == ranges[i][1]){/*already last ?*/
311 rank += ranges[i][2]; /* Stride */
312 if (ranges[i][0] < ranges[i][1]) {
313 if (rank > ranges[i][1])
316 if (rank < ranges[i][1])
324 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
326 for (int i = 0; i < n; i++) {
327 for (int rank = ranges[i][0]; /* First */
328 rank >= 0 && rank < size_; /* Last */
331 if(rank == ranges[i][1]){/*already last ?*/
334 rank += ranges[i][2]; /* Stride */
335 if (ranges[i][0] < ranges[i][1]) {
336 if (rank > ranges[i][1])
339 if (rank < ranges[i][1])
345 *newgroup = MPI_GROUP_EMPTY;
347 *newgroup = new Group(newsize);
350 while (newrank < newsize) {
352 for (int i = 0; i < n; i++) {
353 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
358 if(rank == ranges[i][1]){/*already last ?*/
361 rank += ranges[i][2]; /* Stride */
362 if (ranges[i][0]<ranges[i][1]){
363 if (rank > ranges[i][1])
366 if (rank < ranges[i][1])
372 int index = this->index(oldrank);
373 (*newgroup)->set_mapping(index, newrank);
382 MPI_Group Group::f2c(int id) {
384 return MPI_GROUP_EMPTY;
385 } else if(F2C::f2c_lookup_ != nullptr && id >= 0) {
387 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup_, get_key(key, id)));
389 return static_cast<MPI_Group>(MPI_GROUP_NULL);