Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
89abd06941fcb5b40abcf05ac8f93db22a0e165e
[simgrid.git] / src / smpi / mpi / smpi_group.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "smpi_comm.hpp"
7 #include "smpi_group.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
10
11 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
12 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
13
14 namespace simgrid{
15 namespace smpi{
16
17 Group::Group()
18 {
19   size_              = 0;       /* size */
20   rank_to_index_map_ = nullptr; /* rank_to_index_map_ */
21   refcount_          = 1;       /* refcount_: start > 0 so that this group never gets freed */
22 }
23
24 Group::Group(int n) : size_(n)
25 {
26   rank_to_index_map_ = new int[size_];
27   refcount_ = 1;
28   for (int i              = 0; i < size_; i++)
29     rank_to_index_map_[i] = MPI_UNDEFINED;
30 }
31
32 Group::Group(MPI_Group origin)
33 {
34   if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
35     size_              = origin->size();
36     rank_to_index_map_ = new int[size_];
37     refcount_          = 1;
38     for (int i = 0; i < size_; i++) {
39       rank_to_index_map_[i] = origin->rank_to_index_map_[i];
40     }
41
42     for (auto elm : origin->index_to_rank_map_) {
43       index_to_rank_map_.insert({elm.first, elm.second});
44     }
45   }
46 }
47
48 Group::~Group()
49 {
50   delete[] rank_to_index_map_;
51 }
52
53 void Group::set_mapping(int index, int rank)
54 {
55   if (rank < size_) {
56     rank_to_index_map_[rank] = index;
57     if (index != MPI_UNDEFINED)
58       index_to_rank_map_.insert({index, rank});
59   }
60 }
61
62 int Group::index(int rank)
63 {
64   int index = MPI_UNDEFINED;
65
66   if (0 <= rank && rank < size_) {
67     index = rank_to_index_map_[rank];
68   }
69   return index;
70 }
71
72 int Group::rank(int index)
73 {
74   if (this == MPI_GROUP_EMPTY)
75     return MPI_UNDEFINED;
76   if (index_to_rank_map_.find(index) == index_to_rank_map_.end())
77     return MPI_UNDEFINED;
78   else
79     return index_to_rank_map_.at(index);
80 }
81
82 void Group::ref()
83 {
84   refcount_++;
85 }
86
87 void Group::unref(Group* group)
88 {
89   group->refcount_--;
90   if (group->refcount_ <= 0) {
91     delete group;
92   }
93 }
94
95 int Group::size()
96 {
97   return size_;
98 }
99
100 int Group::compare(MPI_Group group2)
101 {
102   int result;
103
104   result = MPI_IDENT;
105   if (size_ != group2->size()) {
106     result = MPI_UNEQUAL;
107   } else {
108     int sz = group2->size();
109     for (int i = 0; i < sz; i++) {
110       int index = this->index(i);
111       int rank = group2->rank(index);
112       if (rank == MPI_UNDEFINED) {
113         result = MPI_UNEQUAL;
114         break;
115       }
116       if (rank != i) {
117         result = MPI_SIMILAR;
118       }
119     }
120   }
121   return result;
122 }
123
124 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
125 {
126   int i=0;
127   int index=0;
128   if (n == 0) {
129     *newgroup = MPI_GROUP_EMPTY;
130   } else if (n == size_) {
131     *newgroup = this;
132     if(this!= MPI_COMM_WORLD->group()
133               && this != MPI_COMM_SELF->group()
134               && this != MPI_GROUP_EMPTY)
135     this->ref();
136   } else {
137     *newgroup = new Group(n);
138     for (i = 0; i < n; i++) {
139       index = this->index(ranks[i]);
140       (*newgroup)->set_mapping(index, i);
141     }
142   }
143   return MPI_SUCCESS;
144 }
145
146 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
147 {
148   int size1 = size_;
149   int size2 = group2->size();
150   for (int i = 0; i < size2; i++) {
151     int proc2 = group2->index(i);
152     int proc1 = this->rank(proc2);
153     if (proc1 == MPI_UNDEFINED) {
154       size1++;
155     }
156   }
157   if (size1 == 0) {
158     *newgroup = MPI_GROUP_EMPTY;
159   } else {
160     *newgroup = new  Group(size1);
161     size2 = this->size();
162     for (int i = 0; i < size2; i++) {
163       int proc1 = this->index(i);
164       (*newgroup)->set_mapping(proc1, i);
165     }
166     for (int i = size2; i < size1; i++) {
167       int proc2 = group2->index(i - size2);
168       (*newgroup)->set_mapping(proc2, i);
169     }
170   }
171   return MPI_SUCCESS;
172 }
173
174 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
175 {
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) {
181       size2--;
182     }
183   }
184   if (size2 == 0) {
185     *newgroup = MPI_GROUP_EMPTY;
186   } else {
187     *newgroup = new  Group(size2);
188     int j=0;
189     for (int i = 0; i < group2->size(); i++) {
190       int proc2 = group2->index(i);
191       int proc1 = this->rank(proc2);
192       if (proc1 != MPI_UNDEFINED) {
193         (*newgroup)->set_mapping(proc2, j);
194         j++;
195       }
196     }
197   }
198   return MPI_SUCCESS;
199 }
200
201 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
202 {
203   int newsize = size_;
204   int size2 = size_;
205   for (int i = 0; i < size2; i++) {
206     int proc1 = this->index(i);
207     int proc2 = group2->rank(proc1);
208     if (proc2 != MPI_UNDEFINED) {
209       newsize--;
210     }
211   }
212   if (newsize == 0) {
213     *newgroup = MPI_GROUP_EMPTY;
214   } else {
215     *newgroup = new  Group(newsize);
216     for (int i = 0; i < size2; i++) {
217       int proc1 = this->index(i);
218       int proc2 = group2->rank(proc1);
219       if (proc2 == MPI_UNDEFINED) {
220         (*newgroup)->set_mapping(proc1, i);
221       }
222     }
223   }
224   return MPI_SUCCESS;
225 }
226
227 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
228   int oldsize = size_;
229   int newsize = oldsize - n;
230   *newgroup = new  Group(newsize);
231   int* to_exclude=xbt_new0(int, size_);
232   for (int i     = 0; i < oldsize; i++)
233     to_exclude[i]=0;
234   for (int i            = 0; i < n; i++)
235     to_exclude[ranks[i]]=1;
236   int j = 0;
237   for (int i = 0; i < oldsize; i++) {
238     if(to_exclude[i]==0){
239       int index = this->index(i);
240       (*newgroup)->set_mapping(index, j);
241       j++;
242     }
243   }
244   xbt_free(to_exclude);
245   return MPI_SUCCESS;
246
247 }
248
249 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
250   int newsize = 0;
251   for (int i = 0; i < n; i++) {
252     for (int rank = ranges[i][0];                    /* First */
253          rank >= 0 && rank < size_; /* Last */
254          ) {
255       newsize++;
256       if(rank == ranges[i][1]){/*already last ?*/
257         break;
258       }
259       rank += ranges[i][2]; /* Stride */
260       if (ranges[i][0] < ranges[i][1]) {
261         if (rank > ranges[i][1])
262           break;
263       } else {
264         if (rank < ranges[i][1])
265           break;
266       }
267     }
268   }
269   *newgroup = new  Group(newsize);
270   int j     = 0;
271   for (int i = 0; i < n; i++) {
272     for (int rank = ranges[i][0];                    /* First */
273          rank >= 0 && rank < size_; /* Last */
274          ) {
275       int index = this->index(rank);
276       (*newgroup)->set_mapping(index, j);
277       j++;
278       if(rank == ranges[i][1]){/*already last ?*/
279         break;
280       }
281       rank += ranges[i][2]; /* Stride */
282       if (ranges[i][0] < ranges[i][1]) {
283         if (rank > ranges[i][1])
284           break;
285       } else {
286         if (rank < ranges[i][1])
287           break;
288       }
289     }
290   }
291   return MPI_SUCCESS;
292 }
293
294 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
295   int newsize = size_;
296   for (int i = 0; i < n; i++) {
297     for (int rank = ranges[i][0];                    /* First */
298          rank >= 0 && rank < size_; /* Last */
299          ) {
300       newsize--;
301       if(rank == ranges[i][1]){/*already last ?*/
302         break;
303       }
304       rank += ranges[i][2]; /* Stride */
305       if (ranges[i][0] < ranges[i][1]) {
306         if (rank > ranges[i][1])
307           break;
308       } else {
309         if (rank < ranges[i][1])
310           break;
311       }
312     }
313   }
314   if (newsize == 0) {
315     *newgroup = MPI_GROUP_EMPTY;
316   } else {
317     *newgroup = new  Group(newsize);
318     int newrank = 0;
319     int oldrank = 0;
320     while (newrank < newsize) {
321       int add = 1;
322       for (int i = 0; i < n; i++) {
323         for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
324           if(rank==oldrank){
325             add = 0;
326             break;
327           }
328           if(rank == ranges[i][1]){/*already last ?*/
329             break;
330           }
331           rank += ranges[i][2]; /* Stride */
332           if (ranges[i][0]<ranges[i][1]){
333             if (rank > ranges[i][1])
334               break;
335           }else{
336             if (rank < ranges[i][1])
337               break;
338           }
339         }
340       }
341       if(add==1){
342         int index = this->index(oldrank);
343         (*newgroup)->set_mapping(index, newrank);
344         newrank++;
345       }
346       oldrank++;
347     }
348   }
349   return MPI_SUCCESS;
350 }
351
352 MPI_Group Group::f2c(int id) {
353   if(id == -2) {
354     return MPI_GROUP_EMPTY;
355   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
356     char key[KEY_SIZE];
357     return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
358   } else {
359     return static_cast<MPI_Group>(MPI_GROUP_NULL);
360   }
361 }
362
363 }
364 }