1 /* Copyright (c) 2010-2019. 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 "smpi_comm.hpp"
7 #include "smpi_coll.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_request.hpp"
10 #include "smpi_win.hpp"
11 #include "smpi_info.hpp"
12 #include "src/smpi/include/smpi_actor.hpp"
13 #include "src/surf/HostImpl.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
19 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
20 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
22 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
23 * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
28 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
29 int Comm::keyval_id_=0;
31 Comm::Comm(MPI_Group group, MPI_Topology topo, int smp, int in_id) : group_(group), topo_(topo),is_smp_comm_(smp), id_(in_id)
34 topoType_ = MPI_INVALID_TOPO;
35 intra_comm_ = MPI_COMM_NULL;
36 leaders_comm_ = MPI_COMM_NULL;
38 non_uniform_map_ = nullptr;
39 leaders_map_ = nullptr;
41 info_ = MPI_INFO_NULL;
42 static int global_id_=0;
43 //First creation of comm is done before SIMIX_run, so only do comms for others
44 if(in_id==MPI_UNDEFINED && smp==0 && this->rank()!=MPI_UNDEFINED ){
50 Colls::bcast(&id, 1, MPI_INT, 0, this);
51 XBT_DEBUG("Communicator %p has id %d", this, id);
52 id_=id;//only set here, as we don't want to change it in the middle of the bcast
57 void Comm::destroy(Comm* comm)
59 if (comm == MPI_COMM_UNINITIALIZED){
60 Comm::destroy(smpi_process()->comm_world());
63 delete comm->topo_; // there's no use count on topos
67 int Comm::dup(MPI_Comm* newcomm){
68 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
69 // we need to switch as the called function may silently touch global variables
70 smpi_switch_data_segment(s4u::Actor::self());
72 MPI_Group cp = new Group(this->group());
73 (*newcomm) = new Comm(cp, this->topo());
74 int ret = MPI_SUCCESS;
76 if (not attributes()->empty()) {
78 void* value_out=nullptr;
79 for (auto const& it : *attributes()) {
80 smpi_key_elem elem = keyvals_.at(it.first);
82 if( elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN &&
83 elem->copy_fn.comm_copy_fn != MPI_COMM_DUP_FN)
84 ret = elem->copy_fn.comm_copy_fn(this, it.first, elem->extra_state, it.second, &value_out, &flag);
85 else if ( elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN &&
86 *(int*)*elem->copy_fn.comm_copy_fn_fort != 1){
87 value_out=(int*)xbt_malloc(sizeof(int));
88 elem->copy_fn.comm_copy_fn_fort(this, it.first, elem->extra_state, it.second, value_out, &flag,&ret);
90 if (ret != MPI_SUCCESS) {
91 Comm::destroy(*newcomm);
92 *newcomm = MPI_COMM_NULL;
95 if (elem->copy_fn.comm_copy_fn == MPI_COMM_DUP_FN ||
96 ((elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN) && *(int*)*elem->copy_fn.comm_copy_fn_fort == 1)){
98 (*newcomm)->attributes()->insert({it.first, it.second});
101 (*newcomm)->attributes()->insert({it.first, value_out});
106 //duplicate info if present
107 if(info_!=MPI_INFO_NULL)
108 (*newcomm)->info_ = new simgrid::smpi::Info(info_);
112 int Comm::dup_with_info(MPI_Info info, MPI_Comm* newcomm){
113 int ret = dup(newcomm);
114 if(ret != MPI_SUCCESS)
116 if((*newcomm)->info_!=MPI_INFO_NULL){
117 simgrid::smpi::Info::unref((*newcomm)->info_);
118 (*newcomm)->info_=MPI_INFO_NULL;
120 if(info != MPI_INFO_NULL){
121 (*newcomm)->info_=info;
126 MPI_Group Comm::group()
128 if (this == MPI_COMM_UNINITIALIZED)
129 return smpi_process()->comm_world()->group();
135 if (this == MPI_COMM_UNINITIALIZED)
136 return smpi_process()->comm_world()->size();
137 return group_->size();
142 if (this == MPI_COMM_UNINITIALIZED)
143 return smpi_process()->comm_world()->rank();
144 return group_->rank(s4u::Actor::self());
152 void Comm::get_name (char* name, int* len)
154 if (this == MPI_COMM_UNINITIALIZED){
155 smpi_process()->comm_world()->get_name(name, len);
158 if(this == MPI_COMM_WORLD && name_.empty()) {
159 strncpy(name, "MPI_COMM_WORLD", 15);
161 } else if(this == MPI_COMM_SELF && name_.empty()) {
162 strncpy(name, "MPI_COMM_SELF", 14);
165 *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
169 void Comm::set_name (const char* name)
171 if (this == MPI_COMM_UNINITIALIZED){
172 smpi_process()->comm_world()->set_name(name);
175 name_.replace (0, MPI_MAX_NAME_STRING+1, name);
179 void Comm::set_leaders_comm(MPI_Comm leaders){
180 if (this == MPI_COMM_UNINITIALIZED){
181 smpi_process()->comm_world()->set_leaders_comm(leaders);
184 leaders_comm_=leaders;
187 int* Comm::get_non_uniform_map(){
188 if (this == MPI_COMM_UNINITIALIZED)
189 return smpi_process()->comm_world()->get_non_uniform_map();
190 return non_uniform_map_;
193 int* Comm::get_leaders_map(){
194 if (this == MPI_COMM_UNINITIALIZED)
195 return smpi_process()->comm_world()->get_leaders_map();
199 MPI_Comm Comm::get_leaders_comm(){
200 if (this == MPI_COMM_UNINITIALIZED)
201 return smpi_process()->comm_world()->get_leaders_comm();
202 return leaders_comm_;
205 MPI_Comm Comm::get_intra_comm(){
206 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
207 return smpi_process()->comm_intra();
208 else return intra_comm_;
211 int Comm::is_uniform(){
212 if (this == MPI_COMM_UNINITIALIZED)
213 return smpi_process()->comm_world()->is_uniform();
217 int Comm::is_blocked(){
218 if (this == MPI_COMM_UNINITIALIZED)
219 return smpi_process()->comm_world()->is_blocked();
223 int Comm::is_smp_comm(){
224 if (this == MPI_COMM_UNINITIALIZED)
225 return smpi_process()->comm_world()->is_smp_comm();
229 MPI_Comm Comm::split(int color, int key)
231 if (this == MPI_COMM_UNINITIALIZED)
232 return smpi_process()->comm_world()->split(color, key);
233 int system_tag = -123;
236 MPI_Group group_root = nullptr;
237 MPI_Group group_out = nullptr;
238 MPI_Group group = this->group();
239 int myrank = this->rank();
240 int size = this->size();
241 /* Gather all colors and keys on rank 0 */
242 int* sendbuf = xbt_new(int, 2);
246 recvbuf = xbt_new(int, 2 * size);
250 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
252 /* Do the actual job */
254 MPI_Group* group_snd = xbt_new(MPI_Group, size);
255 std::vector<std::pair<int, int>> rankmap;
256 rankmap.reserve(size);
257 for (int i = 0; i < size; i++) {
258 if (recvbuf[2 * i] != MPI_UNDEFINED) {
260 for (int j = i + 1; j < size; j++) {
261 if(recvbuf[2 * i] == recvbuf[2 * j]) {
262 recvbuf[2 * j] = MPI_UNDEFINED;
263 rankmap.push_back({recvbuf[2 * j + 1], j});
266 /* Add self in the group */
267 recvbuf[2 * i] = MPI_UNDEFINED;
268 rankmap.push_back({recvbuf[2 * i + 1], i});
269 std::sort(begin(rankmap), end(rankmap));
270 group_out = new Group(rankmap.size());
272 group_root = group_out; /* Save root's group */
274 for (unsigned j = 0; j < rankmap.size(); j++) {
275 s4u::ActorPtr actor = group->actor(rankmap[j].second);
276 group_out->set_mapping(actor, j);
278 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
280 for (auto const& rank : rankmap) {
281 if (rank.second != 0) {
282 group_snd[reqs]=new Group(group_out);
283 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
287 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
288 Group::unref(group_out);
290 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
296 group_out = group_root; /* exit with root's group */
298 if(color != MPI_UNDEFINED) {
299 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
300 } /* otherwise, exit with group_out == nullptr */
302 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
306 if (this == MPI_COMM_UNINITIALIZED){
307 smpi_process()->comm_world()->ref();
314 void Comm::cleanup_smp(){
315 if (intra_comm_ != MPI_COMM_NULL)
316 Comm::unref(intra_comm_);
317 if (leaders_comm_ != MPI_COMM_NULL)
318 Comm::unref(leaders_comm_);
319 xbt_free(non_uniform_map_);
320 delete[] leaders_map_;
323 void Comm::unref(Comm* comm){
324 if (comm == MPI_COMM_UNINITIALIZED){
325 Comm::unref(smpi_process()->comm_world());
329 Group::unref(comm->group_);
331 if(comm->refcount_==0){
333 comm->cleanup_attr<Comm>();
338 MPI_Comm Comm::find_intra_comm(int * leader){
339 //get the indices of all processes sharing the same simix host
340 auto& process_list = sg_host_self()->pimpl_->process_list_;
341 int intra_comm_size = 0;
342 int min_index = INT_MAX; // the minimum index will be the leader
343 for (auto& actor : process_list) {
344 int index = actor.get_pid();
345 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
347 if (index < min_index)
351 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
352 MPI_Group group_intra = new Group(intra_comm_size);
354 for (auto& actor : process_list) {
355 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
356 group_intra->set_mapping(actor.iface(), i);
361 return new Comm(group_intra, nullptr, 1);
364 void Comm::init_smp(){
367 if (this == MPI_COMM_UNINITIALIZED)
368 smpi_process()->comm_world()->init_smp();
370 int comm_size = this->size();
372 // If we are in replay - perform an ugly hack
373 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
374 bool replaying = false; //cache data to set it back again after
375 if(smpi_process()->replaying()){
377 smpi_process()->set_replaying(false);
380 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
381 // we need to switch as the called function may silently touch global variables
382 smpi_switch_data_segment(s4u::Actor::self());
384 //identify neighbours in comm
385 MPI_Comm comm_intra = find_intra_comm(&leader);
388 int* leaders_map = new int[comm_size];
389 int* leader_list = new int[comm_size];
390 std::fill_n(leaders_map, comm_size, 0);
391 std::fill_n(leader_list, comm_size, -1);
393 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
395 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
396 // we need to switch as the called function may silently touch global variables
397 smpi_switch_data_segment(s4u::Actor::self());
400 if(leaders_map_==nullptr){
401 leaders_map_= leaders_map;
403 delete[] leaders_map;
405 int leader_group_size = 0;
406 for(i=0; i<comm_size; i++){
407 int already_done = 0;
408 for (int j = 0; j < leader_group_size; j++) {
409 if (leaders_map_[i] == leader_list[j]) {
413 if (already_done == 0) {
414 leader_list[leader_group_size] = leaders_map_[i];
418 std::sort(leader_list, leader_list + leader_group_size);
420 MPI_Group leaders_group = new Group(leader_group_size);
422 MPI_Comm leader_comm = MPI_COMM_NULL;
423 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
424 //create leader_communicator
425 for (i=0; i< leader_group_size;i++)
426 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
427 leader_comm = new Comm(leaders_group, nullptr,1);
428 this->set_leaders_comm(leader_comm);
429 this->set_intra_comm(comm_intra);
431 // create intracommunicator
433 for (i=0; i< leader_group_size;i++)
434 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
436 if(this->get_leaders_comm()==MPI_COMM_NULL){
437 leader_comm = new Comm(leaders_group, nullptr,1);
438 this->set_leaders_comm(leader_comm);
440 leader_comm=this->get_leaders_comm();
441 Group::unref(leaders_group);
443 smpi_process()->set_comm_intra(comm_intra);
446 // Are the nodes uniform ? = same number of process/node
447 int my_local_size=comm_intra->size();
448 if(comm_intra->rank()==0) {
450 int* non_uniform_map = xbt_new0(int,leader_group_size);
451 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
452 non_uniform_map, 1, MPI_INT, leader_comm);
453 for(i=0; i < leader_group_size; i++) {
454 if(non_uniform_map[0] != non_uniform_map[i]) {
459 if(is_uniform==0 && this->is_uniform()!=0){
460 non_uniform_map_ = non_uniform_map;
462 xbt_free(non_uniform_map);
464 is_uniform_=is_uniform;
466 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
468 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
469 // we need to switch as the called function may silently touch global variables
470 smpi_switch_data_segment(s4u::Actor::self());
472 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
474 int prev=this->group()->rank(comm_intra->group()->actor(0));
475 for (i = 1; i < my_local_size; i++) {
476 int that = this->group()->rank(comm_intra->group()->actor(i));
477 if (that != prev + 1) {
485 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
487 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
489 is_blocked_ = global_blocked;
492 is_blocked_=global_blocked;
494 delete[] leader_list;
497 smpi_process()->set_replaying(true);
500 MPI_Comm Comm::f2c(int id) {
502 return MPI_COMM_SELF;
504 return MPI_COMM_WORLD;
505 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
507 const auto& lookup = F2C::f2c_lookup();
508 auto comm = lookup->find(get_key(key, id));
509 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
511 return MPI_COMM_NULL;
515 void Comm::free_f(int id) {
517 F2C::f2c_lookup()->erase(get_key(key, id));
520 void Comm::add_rma_win(MPI_Win win){
521 rma_wins_.push_back(win);
524 void Comm::remove_rma_win(MPI_Win win){
525 rma_wins_.remove(win);
528 void Comm::finish_rma_calls(){
529 for (auto const& it : rma_wins_) {
530 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
531 int finished = it->finish_comms();
532 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
537 MPI_Info Comm::info(){
538 if(info_== MPI_INFO_NULL)
544 void Comm::set_info(MPI_Info info){
545 if(info_!= MPI_INFO_NULL)
550 MPI_Comm Comm::split_type(int type, int /*key*/, MPI_Info)
552 //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
553 if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
554 return MPI_COMM_NULL;
557 MPI_Comm res= this->find_intra_comm(&leader);
558 if(type != MPI_UNDEFINED)
562 return MPI_COMM_NULL;
567 } // namespace simgrid