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 errhandler_ = MPI_ERRORS_ARE_FATAL;
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 ){
47 static int global_id_ = 0;
51 colls::bcast(&id, 1, MPI_INT, 0, this);
52 XBT_DEBUG("Communicator %p has id %d", this, id);
53 id_=id;//only set here, as we don't want to change it in the middle of the bcast
58 void Comm::destroy(Comm* comm)
60 if (comm == MPI_COMM_UNINITIALIZED){
61 Comm::destroy(smpi_process()->comm_world());
67 int Comm::dup(MPI_Comm* newcomm){
68 if (smpi_cfg_privatization() == 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_);
109 //duplicate errhandler
110 (*newcomm)->set_errhandler(errhandler_);
114 int Comm::dup_with_info(MPI_Info info, MPI_Comm* newcomm){
115 int ret = dup(newcomm);
116 if(ret != MPI_SUCCESS)
118 if((*newcomm)->info_!=MPI_INFO_NULL){
119 simgrid::smpi::Info::unref((*newcomm)->info_);
120 (*newcomm)->info_=MPI_INFO_NULL;
122 if(info != MPI_INFO_NULL){
123 (*newcomm)->info_=info;
128 MPI_Group Comm::group()
130 if (this == MPI_COMM_UNINITIALIZED)
131 return smpi_process()->comm_world()->group();
137 if (this == MPI_COMM_UNINITIALIZED)
138 return smpi_process()->comm_world()->size();
139 return group_->size();
144 if (this == MPI_COMM_UNINITIALIZED)
145 return smpi_process()->comm_world()->rank();
146 return group_->rank(s4u::Actor::self());
154 void Comm::get_name (char* name, int* len)
156 if (this == MPI_COMM_UNINITIALIZED){
157 smpi_process()->comm_world()->get_name(name, len);
160 if(this == MPI_COMM_WORLD && name_.empty()) {
161 strncpy(name, "MPI_COMM_WORLD", 15);
163 } else if(this == MPI_COMM_SELF && name_.empty()) {
164 strncpy(name, "MPI_COMM_SELF", 14);
167 *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
171 void Comm::set_name (const char* name)
173 if (this == MPI_COMM_UNINITIALIZED){
174 smpi_process()->comm_world()->set_name(name);
177 name_.replace (0, MPI_MAX_NAME_STRING+1, name);
181 void Comm::set_leaders_comm(MPI_Comm leaders){
182 if (this == MPI_COMM_UNINITIALIZED){
183 smpi_process()->comm_world()->set_leaders_comm(leaders);
186 leaders_comm_=leaders;
189 int* Comm::get_non_uniform_map(){
190 if (this == MPI_COMM_UNINITIALIZED)
191 return smpi_process()->comm_world()->get_non_uniform_map();
192 return non_uniform_map_;
195 int* Comm::get_leaders_map(){
196 if (this == MPI_COMM_UNINITIALIZED)
197 return smpi_process()->comm_world()->get_leaders_map();
201 MPI_Comm Comm::get_leaders_comm(){
202 if (this == MPI_COMM_UNINITIALIZED)
203 return smpi_process()->comm_world()->get_leaders_comm();
204 return leaders_comm_;
207 MPI_Comm Comm::get_intra_comm(){
208 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
209 return smpi_process()->comm_intra();
210 else return intra_comm_;
213 int Comm::is_uniform(){
214 if (this == MPI_COMM_UNINITIALIZED)
215 return smpi_process()->comm_world()->is_uniform();
219 int Comm::is_blocked(){
220 if (this == MPI_COMM_UNINITIALIZED)
221 return smpi_process()->comm_world()->is_blocked();
225 int Comm::is_smp_comm(){
226 if (this == MPI_COMM_UNINITIALIZED)
227 return smpi_process()->comm_world()->is_smp_comm();
231 MPI_Comm Comm::split(int color, int key)
233 if (this == MPI_COMM_UNINITIALIZED)
234 return smpi_process()->comm_world()->split(color, key);
235 int system_tag = -123;
238 MPI_Group group_root = nullptr;
239 MPI_Group group_out = nullptr;
240 MPI_Group group = this->group();
241 int myrank = this->rank();
242 int size = this->size();
243 /* Gather all colors and keys on rank 0 */
244 int* sendbuf = xbt_new(int, 2);
248 recvbuf = xbt_new(int, 2 * size);
252 gather__default(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
254 /* Do the actual job */
256 MPI_Group* group_snd = xbt_new(MPI_Group, size);
257 std::vector<std::pair<int, int>> rankmap;
258 rankmap.reserve(size);
259 for (int i = 0; i < size; i++) {
260 if (recvbuf[2 * i] != MPI_UNDEFINED) {
262 for (int j = i + 1; j < size; j++) {
263 if(recvbuf[2 * i] == recvbuf[2 * j]) {
264 recvbuf[2 * j] = MPI_UNDEFINED;
265 rankmap.push_back({recvbuf[2 * j + 1], j});
268 /* Add self in the group */
269 recvbuf[2 * i] = MPI_UNDEFINED;
270 rankmap.push_back({recvbuf[2 * i + 1], i});
271 std::sort(begin(rankmap), end(rankmap));
272 group_out = new Group(rankmap.size());
274 group_root = group_out; /* Save root's group */
276 for (unsigned j = 0; j < rankmap.size(); j++) {
277 s4u::Actor* actor = group->actor(rankmap[j].second);
278 group_out->set_mapping(actor, j);
280 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
282 for (auto const& rank : rankmap) {
283 if (rank.second != 0) {
284 group_snd[reqs]=new Group(group_out);
285 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
289 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
290 Group::unref(group_out);
292 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
298 group_out = group_root; /* exit with root's group */
300 if(color != MPI_UNDEFINED) {
301 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
302 } /* otherwise, exit with group_out == nullptr */
304 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
308 if (this == MPI_COMM_UNINITIALIZED){
309 smpi_process()->comm_world()->ref();
316 void Comm::cleanup_smp(){
317 if (intra_comm_ != MPI_COMM_NULL)
318 Comm::unref(intra_comm_);
319 if (leaders_comm_ != MPI_COMM_NULL)
320 Comm::unref(leaders_comm_);
321 xbt_free(non_uniform_map_);
322 delete[] leaders_map_;
325 void Comm::unref(Comm* comm){
326 if (comm == MPI_COMM_UNINITIALIZED){
327 Comm::unref(smpi_process()->comm_world());
331 Group::unref(comm->group_);
333 if(comm->refcount_==0){
335 comm->cleanup_attr<Comm>();
336 delete comm->topo_; // there's no use count on topos
341 MPI_Comm Comm::find_intra_comm(int * leader){
342 //get the indices of all processes sharing the same simix host
343 auto& actor_list = sg_host_self()->pimpl_->actor_list_;
344 int intra_comm_size = 0;
345 int min_index = INT_MAX; // the minimum index will be the leader
346 for (auto& actor : actor_list) {
347 int index = actor.get_pid();
348 if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) { // Is this process in the current group?
350 if (index < min_index)
354 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
355 MPI_Group group_intra = new Group(intra_comm_size);
357 for (auto& actor : actor_list) {
358 if (this->group()->rank(actor.ciface()) != MPI_UNDEFINED) {
359 group_intra->set_mapping(actor.ciface(), i);
364 return new Comm(group_intra, nullptr, 1);
367 void Comm::init_smp(){
370 if (this == MPI_COMM_UNINITIALIZED)
371 smpi_process()->comm_world()->init_smp();
373 int comm_size = this->size();
375 // If we are in replay - perform an ugly hack
376 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
377 bool replaying = false; //cache data to set it back again after
378 if(smpi_process()->replaying()){
380 smpi_process()->set_replaying(false);
383 if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
384 // we need to switch as the called function may silently touch global variables
385 smpi_switch_data_segment(s4u::Actor::self());
387 // identify neighbors in comm
388 MPI_Comm comm_intra = find_intra_comm(&leader);
391 int* leaders_map = new int[comm_size];
392 int* leader_list = new int[comm_size];
393 std::fill_n(leaders_map, comm_size, 0);
394 std::fill_n(leader_list, comm_size, -1);
396 allgather__ring(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
398 if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
399 // we need to switch as the called function may silently touch global variables
400 smpi_switch_data_segment(s4u::Actor::self());
403 if(leaders_map_==nullptr){
404 leaders_map_= leaders_map;
406 delete[] leaders_map;
408 int leader_group_size = 0;
409 for(i=0; i<comm_size; i++){
410 int already_done = 0;
411 for (int j = 0; j < leader_group_size; j++) {
412 if (leaders_map_[i] == leader_list[j]) {
416 if (already_done == 0) {
417 leader_list[leader_group_size] = leaders_map_[i];
421 std::sort(leader_list, leader_list + leader_group_size);
423 MPI_Group leaders_group = new Group(leader_group_size);
425 MPI_Comm leader_comm = MPI_COMM_NULL;
426 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
427 //create leader_communicator
428 for (i=0; i< leader_group_size;i++)
429 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]).get(), i);
430 leader_comm = new Comm(leaders_group, nullptr,1);
431 this->set_leaders_comm(leader_comm);
432 this->set_intra_comm(comm_intra);
434 // create intracommunicator
436 for (i=0; i< leader_group_size;i++)
437 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]).get(), i);
439 if(this->get_leaders_comm()==MPI_COMM_NULL){
440 leader_comm = new Comm(leaders_group, nullptr,1);
441 this->set_leaders_comm(leader_comm);
443 leader_comm=this->get_leaders_comm();
444 Group::unref(leaders_group);
446 smpi_process()->set_comm_intra(comm_intra);
449 // Are the nodes uniform ? = same number of process/node
450 int my_local_size=comm_intra->size();
451 if(comm_intra->rank()==0) {
453 int* non_uniform_map = xbt_new0(int,leader_group_size);
454 allgather__ring(&my_local_size, 1, MPI_INT,
455 non_uniform_map, 1, MPI_INT, leader_comm);
456 for(i=0; i < leader_group_size; i++) {
457 if(non_uniform_map[0] != non_uniform_map[i]) {
462 if(is_uniform==0 && this->is_uniform()!=0){
463 non_uniform_map_ = non_uniform_map;
465 xbt_free(non_uniform_map);
467 is_uniform_=is_uniform;
469 bcast__scatter_LR_allgather(&(is_uniform_),1, MPI_INT, 0, comm_intra );
471 if (smpi_cfg_privatization() == SmpiPrivStrategies::MMAP) {
472 // we need to switch as the called function may silently touch global variables
473 smpi_switch_data_segment(s4u::Actor::self());
475 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
477 int prev=this->group()->rank(comm_intra->group()->actor(0));
478 for (i = 1; i < my_local_size; i++) {
479 int that = this->group()->rank(comm_intra->group()->actor(i));
480 if (that != prev + 1) {
488 allreduce__default(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
490 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
492 is_blocked_ = global_blocked;
495 is_blocked_=global_blocked;
497 delete[] leader_list;
500 smpi_process()->set_replaying(true);
503 MPI_Comm Comm::f2c(int id) {
505 return MPI_COMM_SELF;
507 return MPI_COMM_WORLD;
508 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
510 const auto& lookup = F2C::f2c_lookup();
511 auto comm = lookup->find(get_key(key, id));
512 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
514 return MPI_COMM_NULL;
518 void Comm::free_f(int id) {
520 F2C::f2c_lookup()->erase(get_key(key, id));
523 void Comm::add_rma_win(MPI_Win win){
524 rma_wins_.push_back(win);
527 void Comm::remove_rma_win(MPI_Win win){
528 rma_wins_.remove(win);
531 void Comm::finish_rma_calls(){
532 for (auto const& it : rma_wins_) {
533 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
534 int finished = it->finish_comms();
535 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
540 MPI_Info Comm::info(){
541 if(info_== MPI_INFO_NULL)
547 void Comm::set_info(MPI_Info info){
548 if(info_!= MPI_INFO_NULL)
553 MPI_Errhandler Comm::errhandler(){
557 void Comm::set_errhandler(MPI_Errhandler errhandler){
558 errhandler_=errhandler;
559 if(errhandler_!= MPI_ERRHANDLER_NULL)
563 MPI_Comm Comm::split_type(int type, int /*key*/, MPI_Info)
565 //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
566 if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
567 return MPI_COMM_NULL;
570 MPI_Comm res= this->find_intra_comm(&leader);
571 if(type != MPI_UNDEFINED)
574 xbt_assert(res->refcount_ == 1); // ensure the next call to Comm::destroy really frees the comm
576 return MPI_COMM_NULL;
581 } // namespace simgrid