Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move resized to its own method
[simgrid.git] / src / smpi / mpi / smpi_comm.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 "private.hpp"
8 #include "simgrid/s4u/Host.hpp"
9 #include "smpi_coll.hpp"
10 #include "smpi_datatype.hpp"
11 #include "smpi_process.hpp"
12 #include "smpi_request.hpp"
13 #include "smpi_status.hpp"
14 #include "smpi_win.hpp"
15 #include "src/simix/smx_private.hpp"
16 #include <algorithm>
17 #include <climits>
18 #include <vector>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
21
22 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
23 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
24
25 using simgrid::s4u::ActorPtr;
26
27 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
28  * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
29
30 namespace simgrid{
31 namespace smpi{
32
33 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
34 int Comm::keyval_id_=0;
35
36 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
37 {
38   refcount_        = 1;
39   topoType_        = MPI_INVALID_TOPO;
40   intra_comm_      = MPI_COMM_NULL;
41   leaders_comm_    = MPI_COMM_NULL;
42   is_uniform_      = 1;
43   non_uniform_map_ = nullptr;
44   leaders_map_     = nullptr;
45   is_blocked_      = 0;
46 }
47
48 void Comm::destroy(Comm* comm)
49 {
50   if (comm == MPI_COMM_UNINITIALIZED){
51     Comm::destroy(smpi_process()->comm_world());
52     return;
53   }
54   delete comm->topo_; // there's no use count on topos
55   Comm::unref(comm);
56 }
57
58 int Comm::dup(MPI_Comm* newcomm){
59   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
60     smpi_switch_data_segment(simgrid::s4u::Actor::self()->getPid());
61   }
62   MPI_Group cp = new  Group(this->group());
63   (*newcomm)   = new  Comm(cp, this->topo());
64   int ret      = MPI_SUCCESS;
65
66   if (not attributes()->empty()) {
67     int flag;
68     void* value_out;
69     for (auto const& it : *attributes()) {
70       smpi_key_elem elem = keyvals_.at(it.first);
71       if (elem != nullptr && elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN) {
72         ret = elem->copy_fn.comm_copy_fn(this, it.first, nullptr, it.second, &value_out, &flag);
73         if (ret != MPI_SUCCESS) {
74           Comm::destroy(*newcomm);
75           *newcomm = MPI_COMM_NULL;
76           return ret;
77         }
78         if (flag){
79           elem->refcount++;
80           (*newcomm)->attributes()->insert({it.first, value_out});
81         }
82       }
83     }
84   }
85   return ret;
86 }
87
88 MPI_Group Comm::group()
89 {
90   if (this == MPI_COMM_UNINITIALIZED)
91     return smpi_process()->comm_world()->group();
92   return group_;
93 }
94
95 MPI_Topology Comm::topo() {
96   return topo_;
97 }
98
99 int Comm::size()
100 {
101   if (this == MPI_COMM_UNINITIALIZED)
102     return smpi_process()->comm_world()->size();
103   return group_->size();
104 }
105
106 int Comm::rank()
107 {
108   if (this == MPI_COMM_UNINITIALIZED)
109     return smpi_process()->comm_world()->rank();
110   return group_->rank(simgrid::s4u::Actor::self());
111 }
112
113 void Comm::get_name (char* name, int* len)
114 {
115   if (this == MPI_COMM_UNINITIALIZED){
116     smpi_process()->comm_world()->get_name(name, len);
117     return;
118   }
119   if(this == MPI_COMM_WORLD) {
120     strncpy(name, "WORLD",5);
121     *len = 5;
122   } else {
123     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
124   }
125 }
126
127 void Comm::set_leaders_comm(MPI_Comm leaders){
128   if (this == MPI_COMM_UNINITIALIZED){
129     smpi_process()->comm_world()->set_leaders_comm(leaders);
130     return;
131   }
132   leaders_comm_=leaders;
133 }
134
135 void Comm::set_intra_comm(MPI_Comm leaders){
136   intra_comm_=leaders;
137 }
138
139 int* Comm::get_non_uniform_map(){
140   if (this == MPI_COMM_UNINITIALIZED)
141     return smpi_process()->comm_world()->get_non_uniform_map();
142   return non_uniform_map_;
143 }
144
145 int* Comm::get_leaders_map(){
146   if (this == MPI_COMM_UNINITIALIZED)
147     return smpi_process()->comm_world()->get_leaders_map();
148   return leaders_map_;
149 }
150
151 MPI_Comm Comm::get_leaders_comm(){
152   if (this == MPI_COMM_UNINITIALIZED)
153     return smpi_process()->comm_world()->get_leaders_comm();
154   return leaders_comm_;
155 }
156
157 MPI_Comm Comm::get_intra_comm(){
158   if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
159     return smpi_process()->comm_intra();
160   else return intra_comm_;
161 }
162
163 int Comm::is_uniform(){
164   if (this == MPI_COMM_UNINITIALIZED)
165     return smpi_process()->comm_world()->is_uniform();
166   return is_uniform_;
167 }
168
169 int Comm::is_blocked(){
170   if (this == MPI_COMM_UNINITIALIZED)
171     return smpi_process()->comm_world()->is_blocked();
172   return is_blocked_;
173 }
174
175 MPI_Comm Comm::split(int color, int key)
176 {
177   if (this == MPI_COMM_UNINITIALIZED)
178     return smpi_process()->comm_world()->split(color, key);
179   int system_tag = 123;
180   int* recvbuf;
181
182   MPI_Group group_root = nullptr;
183   MPI_Group group_out  = nullptr;
184   MPI_Group group      = this->group();
185   int rank             = this->rank();
186   int size             = this->size();
187   /* Gather all colors and keys on rank 0 */
188   int* sendbuf = xbt_new(int, 2);
189   sendbuf[0] = color;
190   sendbuf[1] = key;
191   if(rank == 0) {
192     recvbuf = xbt_new(int, 2 * size);
193   } else {
194     recvbuf = nullptr;
195   }
196   Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
197   xbt_free(sendbuf);
198   /* Do the actual job */
199   if(rank == 0) {
200     MPI_Group* group_snd = xbt_new(MPI_Group, size);
201     std::vector<std::pair<int, int>> rankmap;
202     rankmap.reserve(size);
203     for (int i = 0; i < size; i++) {
204       if (recvbuf[2 * i] != MPI_UNDEFINED) {
205         rankmap.clear();
206         for (int j = i + 1; j < size; j++) {
207           if(recvbuf[2 * i] == recvbuf[2 * j]) {
208             recvbuf[2 * j] = MPI_UNDEFINED;
209             rankmap.push_back({recvbuf[2 * j + 1], j});
210           }
211         }
212         /* Add self in the group */
213         recvbuf[2 * i] = MPI_UNDEFINED;
214         rankmap.push_back({recvbuf[2 * i + 1], i});
215         std::sort(begin(rankmap), end(rankmap));
216         group_out = new Group(rankmap.size());
217         if (i == 0) {
218           group_root = group_out; /* Save root's group */
219         }
220         for (unsigned j = 0; j < rankmap.size(); j++) {
221           ActorPtr actor = group->actor(rankmap[j].second);
222           group_out->set_mapping(actor, j);
223         }
224         MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
225         int reqs              = 0;
226         for (auto const& rank : rankmap) {
227           if (rank.second != 0) {
228             group_snd[reqs]=new  Group(group_out);
229             requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
230             reqs++;
231           }
232         }
233         if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
234           Group::unref(group_out);
235
236         Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
237         xbt_free(requests);
238       }
239     }
240     xbt_free(recvbuf);
241     xbt_free(group_snd);
242     group_out = group_root; /* exit with root's group */
243   } else {
244     if(color != MPI_UNDEFINED) {
245       Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
246     } /* otherwise, exit with group_out == nullptr */
247   }
248   return group_out!=nullptr ? new  Comm(group_out, nullptr) : MPI_COMM_NULL;
249 }
250
251 void Comm::ref(){
252   if (this == MPI_COMM_UNINITIALIZED){
253     smpi_process()->comm_world()->ref();
254     return;
255   }
256   group_->ref();
257   refcount_++;
258 }
259
260 void Comm::cleanup_smp(){
261   if (intra_comm_ != MPI_COMM_NULL)
262     Comm::unref(intra_comm_);
263   if (leaders_comm_ != MPI_COMM_NULL)
264     Comm::unref(leaders_comm_);
265   if (non_uniform_map_ != nullptr)
266     xbt_free(non_uniform_map_);
267   if (leaders_map_ != nullptr)
268     delete[] leaders_map_;
269 }
270
271 void Comm::unref(Comm* comm){
272   if (comm == MPI_COMM_UNINITIALIZED){
273     Comm::unref(smpi_process()->comm_world());
274     return;
275   }
276   comm->refcount_--;
277   Group::unref(comm->group_);
278
279   if(comm->refcount_==0){
280     comm->cleanup_smp();
281     comm->cleanup_attr<Comm>();
282     delete comm;
283   }
284 }
285
286 void Comm::init_smp(){
287   int leader = -1;
288
289   if (this == MPI_COMM_UNINITIALIZED)
290     smpi_process()->comm_world()->init_smp();
291
292   int comm_size = this->size();
293
294   // If we are in replay - perform an ugly hack
295   // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
296   bool replaying = false; //cache data to set it back again after
297   if(smpi_process()->replaying()){
298     replaying = true;
299     smpi_process()->set_replaying(false);
300   }
301
302   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
303     smpi_switch_data_segment(simgrid::s4u::Actor::self()->getPid());
304   }
305   //identify neighbours in comm
306   //get the indices of all processes sharing the same simix host
307   auto& process_list      = sg_host_self()->extension<simgrid::simix::Host>()->process_list;
308   int intra_comm_size     = 0;
309   int min_index           = INT_MAX; // the minimum index will be the leader
310   for (auto& actor : process_list) {
311     int index = actor.pid;
312     if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
313       intra_comm_size++;
314       if (index < min_index)
315         min_index = index;
316     }
317   }
318   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
319   MPI_Group group_intra = new  Group(intra_comm_size);
320   int i = 0;
321   for (auto& actor : process_list) {
322     if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
323       group_intra->set_mapping(actor.iface(), i);
324       i++;
325     }
326   }
327
328   MPI_Comm comm_intra = new  Comm(group_intra, nullptr);
329   leader=min_index;
330
331   int* leaders_map = new int[comm_size];
332   int* leader_list = new int[comm_size];
333   std::fill_n(leaders_map, comm_size, 0);
334   std::fill_n(leader_list, comm_size, -1);
335
336   Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
337
338   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
339     smpi_switch_data_segment(simgrid::s4u::Actor::self()->getPid());
340   }
341
342   if(leaders_map_==nullptr){
343     leaders_map_= leaders_map;
344   }else{
345     delete[] leaders_map;
346   }
347   int leader_group_size = 0;
348   for(i=0; i<comm_size; i++){
349     int already_done = 0;
350     for (int j = 0; j < leader_group_size; j++) {
351       if (leaders_map_[i] == leader_list[j]) {
352         already_done = 1;
353       }
354     }
355     if (already_done == 0) {
356       leader_list[leader_group_size] = leaders_map_[i];
357       leader_group_size++;
358     }
359   }
360   std::sort(leader_list, leader_list + leader_group_size);
361
362   MPI_Group leaders_group = new  Group(leader_group_size);
363
364   MPI_Comm leader_comm = MPI_COMM_NULL;
365   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
366     //create leader_communicator
367     for (i=0; i< leader_group_size;i++)
368       leaders_group->set_mapping(simgrid::s4u::Actor::byPid(leader_list[i]), i);
369     leader_comm = new  Comm(leaders_group, nullptr);
370     this->set_leaders_comm(leader_comm);
371     this->set_intra_comm(comm_intra);
372
373     // create intracommunicator
374   }else{
375     for (i=0; i< leader_group_size;i++)
376       leaders_group->set_mapping(simgrid::s4u::Actor::byPid(leader_list[i]), i);
377
378     if(this->get_leaders_comm()==MPI_COMM_NULL){
379       leader_comm = new  Comm(leaders_group, nullptr);
380       this->set_leaders_comm(leader_comm);
381     }else{
382       leader_comm=this->get_leaders_comm();
383       Group::unref(leaders_group);
384     }
385     smpi_process()->set_comm_intra(comm_intra);
386   }
387
388   // Are the nodes uniform ? = same number of process/node
389   int my_local_size=comm_intra->size();
390   if(comm_intra->rank()==0) {
391     int is_uniform       = 1;
392     int* non_uniform_map = xbt_new0(int,leader_group_size);
393     Coll_allgather_mpich::allgather(&my_local_size, 1, MPI_INT,
394         non_uniform_map, 1, MPI_INT, leader_comm);
395     for(i=0; i < leader_group_size; i++) {
396       if(non_uniform_map[0] != non_uniform_map[i]) {
397         is_uniform = 0;
398         break;
399       }
400     }
401     if(is_uniform==0 && this->is_uniform()!=0){
402       non_uniform_map_ = non_uniform_map;
403     }else{
404       xbt_free(non_uniform_map);
405     }
406     is_uniform_=is_uniform;
407   }
408   Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
409
410   if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
411     smpi_switch_data_segment(simgrid::s4u::Actor::self()->getPid());
412   }
413   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
414   int is_blocked=1;
415   int prev=this->group()->rank(comm_intra->group()->actor(0));
416   for (i = 1; i < my_local_size; i++) {
417     int that = this->group()->rank(comm_intra->group()->actor(i));
418     if (that != prev + 1) {
419       is_blocked = 0;
420       break;
421     }
422     prev = that;
423   }
424
425   int global_blocked;
426   Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
427
428   if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
429     if(this->rank()==0){
430       is_blocked_ = global_blocked;
431     }
432   }else{
433     is_blocked_=global_blocked;
434   }
435   delete[] leader_list;
436
437   if(replaying)
438     smpi_process()->set_replaying(true);
439 }
440
441 MPI_Comm Comm::f2c(int id) {
442   if(id == -2) {
443     return MPI_COMM_SELF;
444   } else if(id==0){
445     return MPI_COMM_WORLD;
446   } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
447     char key[KEY_SIZE];
448     const auto& lookup = F2C::f2c_lookup();
449     auto comm          = lookup->find(get_key_id(key, id));
450     return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
451   } else {
452     return MPI_COMM_NULL;
453   }
454 }
455
456 void Comm::free_f(int id) {
457   char key[KEY_SIZE];
458   F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
459 }
460
461 int Comm::add_f() {
462   if(F2C::f2c_lookup()==nullptr){
463     F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
464   }
465   char key[KEY_SIZE];
466   (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
467   f2c_id_increment();
468   return F2C::f2c_id()-1;
469 }
470
471 void Comm::add_rma_win(MPI_Win win){
472   rma_wins_.push_back(win);
473 }
474
475 void Comm::remove_rma_win(MPI_Win win){
476   rma_wins_.remove(win);
477 }
478
479 void Comm::finish_rma_calls(){
480   for (auto const& it : rma_wins_) {
481     if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
482       int finished = it->finish_comms();
483       XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
484     }
485   }
486 }
487
488 }
489 }
490
491