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)
132 if (size_ != group2->size()) {
133 result = MPI_UNEQUAL;
135 int sz = group2->size();
136 for (i = 0; i < sz; i++) {
137 index = this->index(i);
138 rank = group2->rank(index);
139 if (rank == MPI_UNDEFINED) {
140 result = MPI_UNEQUAL;
144 result = MPI_SIMILAR;
151 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
156 *newgroup = MPI_GROUP_EMPTY;
157 } else if (n == size_) {
159 if(this!= MPI_COMM_WORLD->group()
160 && this != MPI_COMM_SELF->group()
161 && this != MPI_GROUP_EMPTY)
164 *newgroup = new Group(n);
165 for (i = 0; i < n; i++) {
166 index = this->index(ranks[i]);
167 (*newgroup)->set_mapping(index, i);
173 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
176 int size2 = group2->size();
177 for (int i = 0; i < size2; i++) {
178 int proc2 = group2->index(i);
179 int proc1 = this->rank(proc2);
180 if (proc1 == MPI_UNDEFINED) {
185 *newgroup = MPI_GROUP_EMPTY;
187 *newgroup = new Group(size1);
188 size2 = this->size();
189 for (int i = 0; i < size2; i++) {
190 int proc1 = this->index(i);
191 (*newgroup)->set_mapping(proc1, i);
193 for (int i = size2; i < size1; i++) {
194 int proc2 = group2->index(i - size2);
195 (*newgroup)->set_mapping(proc2, i);
201 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
203 int size2 = group2->size();
204 for (int i = 0; i < size2; i++) {
205 int proc2 = group2->index(i);
206 int proc1 = this->rank(proc2);
207 if (proc1 == MPI_UNDEFINED) {
212 *newgroup = MPI_GROUP_EMPTY;
214 *newgroup = new Group(size2);
216 for (int i = 0; i < group2->size(); i++) {
217 int proc2 = group2->index(i);
218 int proc1 = this->rank(proc2);
219 if (proc1 != MPI_UNDEFINED) {
220 (*newgroup)->set_mapping(proc2, j);
228 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
232 for (int i = 0; i < size2; i++) {
233 int proc1 = this->index(i);
234 int proc2 = group2->rank(proc1);
235 if (proc2 != MPI_UNDEFINED) {
240 *newgroup = MPI_GROUP_EMPTY;
242 *newgroup = new Group(newsize);
243 for (int i = 0; i < size2; i++) {
244 int proc1 = this->index(i);
245 int proc2 = group2->rank(proc1);
246 if (proc2 == MPI_UNDEFINED) {
247 (*newgroup)->set_mapping(proc1, i);
254 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
256 int newsize = oldsize - n;
257 *newgroup = new Group(newsize);
258 int* to_exclude=xbt_new0(int, size_);
259 for (int i = 0; i < oldsize; i++)
261 for (int i = 0; i < n; i++)
262 to_exclude[ranks[i]]=1;
264 for (int i = 0; i < oldsize; i++) {
265 if(to_exclude[i]==0){
266 int index = this->index(i);
267 (*newgroup)->set_mapping(index, j);
271 xbt_free(to_exclude);
276 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
278 for (int i = 0; i < n; i++) {
279 for (int rank = ranges[i][0]; /* First */
280 rank >= 0 && rank < size_; /* Last */
283 if(rank == ranges[i][1]){/*already last ?*/
286 rank += ranges[i][2]; /* Stride */
287 if (ranges[i][0] < ranges[i][1]) {
288 if (rank > ranges[i][1])
291 if (rank < ranges[i][1])
296 *newgroup = new Group(newsize);
298 for (int i = 0; i < n; i++) {
299 for (int rank = ranges[i][0]; /* First */
300 rank >= 0 && rank < size_; /* Last */
302 int index = this->index(rank);
303 (*newgroup)->set_mapping(index, j);
305 if(rank == ranges[i][1]){/*already last ?*/
308 rank += ranges[i][2]; /* Stride */
309 if (ranges[i][0] < ranges[i][1]) {
310 if (rank > ranges[i][1])
313 if (rank < ranges[i][1])
321 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
323 for (int i = 0; i < n; i++) {
324 for (int rank = ranges[i][0]; /* First */
325 rank >= 0 && rank < size_; /* Last */
328 if(rank == ranges[i][1]){/*already last ?*/
331 rank += ranges[i][2]; /* Stride */
332 if (ranges[i][0] < ranges[i][1]) {
333 if (rank > ranges[i][1])
336 if (rank < ranges[i][1])
342 *newgroup = MPI_GROUP_EMPTY;
344 *newgroup = new Group(newsize);
347 while (newrank < newsize) {
349 for (int i = 0; i < n; i++) {
350 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
355 if(rank == ranges[i][1]){/*already last ?*/
358 rank += ranges[i][2]; /* Stride */
359 if (ranges[i][0]<ranges[i][1]){
360 if (rank > ranges[i][1])
363 if (rank < ranges[i][1])
369 int index = this->index(oldrank);
370 (*newgroup)->set_mapping(index, newrank);
379 MPI_Group Group::f2c(int id) {
381 return MPI_GROUP_EMPTY;
382 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
384 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
386 return static_cast<MPI_Group>(MPI_GROUP_NULL);