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) : group_(group), topo_(topo),is_smp_comm_(smp)
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;
44 void Comm::destroy(Comm* comm)
46 if (comm == MPI_COMM_UNINITIALIZED){
47 Comm::destroy(smpi_process()->comm_world());
50 delete comm->topo_; // there's no use count on topos
54 int Comm::dup(MPI_Comm* newcomm){
55 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
56 // we need to switch as the called function may silently touch global variables
57 smpi_switch_data_segment(s4u::Actor::self());
59 MPI_Group cp = new Group(this->group());
60 (*newcomm) = new Comm(cp, this->topo());
61 int ret = MPI_SUCCESS;
63 if (not attributes()->empty()) {
65 void* value_out=nullptr;
66 for (auto const& it : *attributes()) {
67 smpi_key_elem elem = keyvals_.at(it.first);
69 if( elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN &&
70 elem->copy_fn.comm_copy_fn != MPI_COMM_DUP_FN)
71 ret = elem->copy_fn.comm_copy_fn(this, it.first, elem->extra_state, it.second, &value_out, &flag);
72 else if ( elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN &&
73 *(int*)*elem->copy_fn.comm_copy_fn_fort != 1){
74 value_out=(int*)xbt_malloc(sizeof(int));
75 elem->copy_fn.comm_copy_fn_fort(this, it.first, elem->extra_state, it.second, value_out, &flag,&ret);
77 if (ret != MPI_SUCCESS) {
78 Comm::destroy(*newcomm);
79 *newcomm = MPI_COMM_NULL;
82 if (elem->copy_fn.comm_copy_fn == MPI_COMM_DUP_FN ||
83 ((elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN) && *(int*)*elem->copy_fn.comm_copy_fn_fort == 1)){
85 (*newcomm)->attributes()->insert({it.first, it.second});
88 (*newcomm)->attributes()->insert({it.first, value_out});
93 //duplicate info if present
94 if(info_!=MPI_INFO_NULL)
95 (*newcomm)->info_ = new simgrid::smpi::Info(info_);
99 int Comm::dup_with_info(MPI_Info info, MPI_Comm* newcomm){
100 int ret = dup(newcomm);
101 if((*newcomm)->info_!=MPI_INFO_NULL){
102 simgrid::smpi::Info::unref((*newcomm)->info_);
103 (*newcomm)->info_=MPI_INFO_NULL;
105 if(info != MPI_INFO_NULL){
106 (*newcomm)->info_=info;
111 MPI_Group Comm::group()
113 if (this == MPI_COMM_UNINITIALIZED)
114 return smpi_process()->comm_world()->group();
120 if (this == MPI_COMM_UNINITIALIZED)
121 return smpi_process()->comm_world()->size();
122 return group_->size();
127 if (this == MPI_COMM_UNINITIALIZED)
128 return smpi_process()->comm_world()->rank();
129 return group_->rank(s4u::Actor::self());
132 void Comm::get_name (char* name, int* len)
134 if (this == MPI_COMM_UNINITIALIZED){
135 smpi_process()->comm_world()->get_name(name, len);
138 if(this == MPI_COMM_WORLD && name_.empty()) {
139 strncpy(name, "MPI_COMM_WORLD", 15);
141 } else if(this == MPI_COMM_SELF && name_.empty()) {
142 strncpy(name, "MPI_COMM_SELF", 14);
145 *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
149 void Comm::set_name (const char* name)
151 if (this == MPI_COMM_UNINITIALIZED){
152 smpi_process()->comm_world()->set_name(name);
155 name_.replace (0, MPI_MAX_NAME_STRING+1, name);
159 void Comm::set_leaders_comm(MPI_Comm leaders){
160 if (this == MPI_COMM_UNINITIALIZED){
161 smpi_process()->comm_world()->set_leaders_comm(leaders);
164 leaders_comm_=leaders;
167 int* Comm::get_non_uniform_map(){
168 if (this == MPI_COMM_UNINITIALIZED)
169 return smpi_process()->comm_world()->get_non_uniform_map();
170 return non_uniform_map_;
173 int* Comm::get_leaders_map(){
174 if (this == MPI_COMM_UNINITIALIZED)
175 return smpi_process()->comm_world()->get_leaders_map();
179 MPI_Comm Comm::get_leaders_comm(){
180 if (this == MPI_COMM_UNINITIALIZED)
181 return smpi_process()->comm_world()->get_leaders_comm();
182 return leaders_comm_;
185 MPI_Comm Comm::get_intra_comm(){
186 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
187 return smpi_process()->comm_intra();
188 else return intra_comm_;
191 int Comm::is_uniform(){
192 if (this == MPI_COMM_UNINITIALIZED)
193 return smpi_process()->comm_world()->is_uniform();
197 int Comm::is_blocked(){
198 if (this == MPI_COMM_UNINITIALIZED)
199 return smpi_process()->comm_world()->is_blocked();
203 int Comm::is_smp_comm(){
204 if (this == MPI_COMM_UNINITIALIZED)
205 return smpi_process()->comm_world()->is_smp_comm();
209 MPI_Comm Comm::split(int color, int key)
211 if (this == MPI_COMM_UNINITIALIZED)
212 return smpi_process()->comm_world()->split(color, key);
213 int system_tag = 123;
216 MPI_Group group_root = nullptr;
217 MPI_Group group_out = nullptr;
218 MPI_Group group = this->group();
219 int myrank = this->rank();
220 int size = this->size();
221 /* Gather all colors and keys on rank 0 */
222 int* sendbuf = xbt_new(int, 2);
226 recvbuf = xbt_new(int, 2 * size);
230 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
232 /* Do the actual job */
234 MPI_Group* group_snd = xbt_new(MPI_Group, size);
235 std::vector<std::pair<int, int>> rankmap;
236 rankmap.reserve(size);
237 for (int i = 0; i < size; i++) {
238 if (recvbuf[2 * i] != MPI_UNDEFINED) {
240 for (int j = i + 1; j < size; j++) {
241 if(recvbuf[2 * i] == recvbuf[2 * j]) {
242 recvbuf[2 * j] = MPI_UNDEFINED;
243 rankmap.push_back({recvbuf[2 * j + 1], j});
246 /* Add self in the group */
247 recvbuf[2 * i] = MPI_UNDEFINED;
248 rankmap.push_back({recvbuf[2 * i + 1], i});
249 std::sort(begin(rankmap), end(rankmap));
250 group_out = new Group(rankmap.size());
252 group_root = group_out; /* Save root's group */
254 for (unsigned j = 0; j < rankmap.size(); j++) {
255 s4u::ActorPtr actor = group->actor(rankmap[j].second);
256 group_out->set_mapping(actor, j);
258 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
260 for (auto const& rank : rankmap) {
261 if (rank.second != 0) {
262 group_snd[reqs]=new Group(group_out);
263 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
267 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
268 Group::unref(group_out);
270 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
276 group_out = group_root; /* exit with root's group */
278 if(color != MPI_UNDEFINED) {
279 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
280 } /* otherwise, exit with group_out == nullptr */
282 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
286 if (this == MPI_COMM_UNINITIALIZED){
287 smpi_process()->comm_world()->ref();
294 void Comm::cleanup_smp(){
295 if (intra_comm_ != MPI_COMM_NULL)
296 Comm::unref(intra_comm_);
297 if (leaders_comm_ != MPI_COMM_NULL)
298 Comm::unref(leaders_comm_);
299 xbt_free(non_uniform_map_);
300 delete[] leaders_map_;
303 void Comm::unref(Comm* comm){
304 if (comm == MPI_COMM_UNINITIALIZED){
305 Comm::unref(smpi_process()->comm_world());
309 Group::unref(comm->group_);
311 if(comm->refcount_==0){
313 comm->cleanup_attr<Comm>();
318 void Comm::init_smp(){
321 if (this == MPI_COMM_UNINITIALIZED)
322 smpi_process()->comm_world()->init_smp();
324 int comm_size = this->size();
326 // If we are in replay - perform an ugly hack
327 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
328 bool replaying = false; //cache data to set it back again after
329 if(smpi_process()->replaying()){
331 smpi_process()->set_replaying(false);
334 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
335 // we need to switch as the called function may silently touch global variables
336 smpi_switch_data_segment(s4u::Actor::self());
338 //identify neighbours in comm
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 MPI_Comm comm_intra = new Comm(group_intra, nullptr, 1);
364 int* leaders_map = new int[comm_size];
365 int* leader_list = new int[comm_size];
366 std::fill_n(leaders_map, comm_size, 0);
367 std::fill_n(leader_list, comm_size, -1);
369 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
371 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
372 // we need to switch as the called function may silently touch global variables
373 smpi_switch_data_segment(s4u::Actor::self());
376 if(leaders_map_==nullptr){
377 leaders_map_= leaders_map;
379 delete[] leaders_map;
381 int leader_group_size = 0;
382 for(i=0; i<comm_size; i++){
383 int already_done = 0;
384 for (int j = 0; j < leader_group_size; j++) {
385 if (leaders_map_[i] == leader_list[j]) {
389 if (already_done == 0) {
390 leader_list[leader_group_size] = leaders_map_[i];
394 std::sort(leader_list, leader_list + leader_group_size);
396 MPI_Group leaders_group = new Group(leader_group_size);
398 MPI_Comm leader_comm = MPI_COMM_NULL;
399 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
400 //create leader_communicator
401 for (i=0; i< leader_group_size;i++)
402 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
403 leader_comm = new Comm(leaders_group, nullptr,1);
404 this->set_leaders_comm(leader_comm);
405 this->set_intra_comm(comm_intra);
407 // create intracommunicator
409 for (i=0; i< leader_group_size;i++)
410 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
412 if(this->get_leaders_comm()==MPI_COMM_NULL){
413 leader_comm = new Comm(leaders_group, nullptr,1);
414 this->set_leaders_comm(leader_comm);
416 leader_comm=this->get_leaders_comm();
417 Group::unref(leaders_group);
419 smpi_process()->set_comm_intra(comm_intra);
422 // Are the nodes uniform ? = same number of process/node
423 int my_local_size=comm_intra->size();
424 if(comm_intra->rank()==0) {
426 int* non_uniform_map = xbt_new0(int,leader_group_size);
427 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
428 non_uniform_map, 1, MPI_INT, leader_comm);
429 for(i=0; i < leader_group_size; i++) {
430 if(non_uniform_map[0] != non_uniform_map[i]) {
435 if(is_uniform==0 && this->is_uniform()!=0){
436 non_uniform_map_ = non_uniform_map;
438 xbt_free(non_uniform_map);
440 is_uniform_=is_uniform;
442 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
444 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
445 // we need to switch as the called function may silently touch global variables
446 smpi_switch_data_segment(s4u::Actor::self());
448 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
450 int prev=this->group()->rank(comm_intra->group()->actor(0));
451 for (i = 1; i < my_local_size; i++) {
452 int that = this->group()->rank(comm_intra->group()->actor(i));
453 if (that != prev + 1) {
461 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
463 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
465 is_blocked_ = global_blocked;
468 is_blocked_=global_blocked;
470 delete[] leader_list;
473 smpi_process()->set_replaying(true);
476 MPI_Comm Comm::f2c(int id) {
478 return MPI_COMM_SELF;
480 return MPI_COMM_WORLD;
481 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
483 const auto& lookup = F2C::f2c_lookup();
484 auto comm = lookup->find(get_key_id(key, id));
485 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
487 return MPI_COMM_NULL;
491 void Comm::free_f(int id) {
493 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
497 if(F2C::f2c_lookup()==nullptr){
498 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
501 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
503 return F2C::f2c_id()-1;
506 void Comm::add_rma_win(MPI_Win win){
507 rma_wins_.push_back(win);
510 void Comm::remove_rma_win(MPI_Win win){
511 rma_wins_.remove(win);
514 void Comm::finish_rma_calls(){
515 for (auto const& it : rma_wins_) {
516 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
517 int finished = it->finish_comms();
518 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
523 MPI_Info Comm::info(){
524 if(info_== MPI_INFO_NULL)
530 void Comm::set_info(MPI_Info info){
531 if(info_!= MPI_INFO_NULL)
536 MPI_Comm Comm::split_type(int type, int /*key*/, MPI_Info)
538 //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
539 if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
540 return MPI_COMM_NULL;
544 this->get_intra_comm()->ref();
545 if(type != MPI_UNDEFINED)
546 return this->get_intra_comm();
548 return MPI_COMM_NULL;
552 } // namespace simgrid