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});
96 MPI_Group Comm::group()
98 if (this == MPI_COMM_UNINITIALIZED)
99 return smpi_process()->comm_world()->group();
105 if (this == MPI_COMM_UNINITIALIZED)
106 return smpi_process()->comm_world()->size();
107 return group_->size();
112 if (this == MPI_COMM_UNINITIALIZED)
113 return smpi_process()->comm_world()->rank();
114 return group_->rank(s4u::Actor::self());
117 void Comm::get_name (char* name, int* len)
119 if (this == MPI_COMM_UNINITIALIZED){
120 smpi_process()->comm_world()->get_name(name, len);
123 if(this == MPI_COMM_WORLD && name_.empty()) {
124 strncpy(name, "MPI_COMM_WORLD", 15);
126 } else if(this == MPI_COMM_SELF && name_.empty()) {
127 strncpy(name, "MPI_COMM_SELF", 14);
130 *len = snprintf(name, MPI_MAX_NAME_STRING+1, "%s", name_.c_str());
134 void Comm::set_name (const char* name)
136 if (this == MPI_COMM_UNINITIALIZED){
137 smpi_process()->comm_world()->set_name(name);
140 name_.replace (0, MPI_MAX_NAME_STRING+1, name);
144 void Comm::set_leaders_comm(MPI_Comm leaders){
145 if (this == MPI_COMM_UNINITIALIZED){
146 smpi_process()->comm_world()->set_leaders_comm(leaders);
149 leaders_comm_=leaders;
152 int* Comm::get_non_uniform_map(){
153 if (this == MPI_COMM_UNINITIALIZED)
154 return smpi_process()->comm_world()->get_non_uniform_map();
155 return non_uniform_map_;
158 int* Comm::get_leaders_map(){
159 if (this == MPI_COMM_UNINITIALIZED)
160 return smpi_process()->comm_world()->get_leaders_map();
164 MPI_Comm Comm::get_leaders_comm(){
165 if (this == MPI_COMM_UNINITIALIZED)
166 return smpi_process()->comm_world()->get_leaders_comm();
167 return leaders_comm_;
170 MPI_Comm Comm::get_intra_comm(){
171 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
172 return smpi_process()->comm_intra();
173 else return intra_comm_;
176 int Comm::is_uniform(){
177 if (this == MPI_COMM_UNINITIALIZED)
178 return smpi_process()->comm_world()->is_uniform();
182 int Comm::is_blocked(){
183 if (this == MPI_COMM_UNINITIALIZED)
184 return smpi_process()->comm_world()->is_blocked();
188 int Comm::is_smp_comm(){
189 if (this == MPI_COMM_UNINITIALIZED)
190 return smpi_process()->comm_world()->is_smp_comm();
194 MPI_Comm Comm::split(int color, int key)
196 if (this == MPI_COMM_UNINITIALIZED)
197 return smpi_process()->comm_world()->split(color, key);
198 int system_tag = 123;
201 MPI_Group group_root = nullptr;
202 MPI_Group group_out = nullptr;
203 MPI_Group group = this->group();
204 int myrank = this->rank();
205 int size = this->size();
206 /* Gather all colors and keys on rank 0 */
207 int* sendbuf = xbt_new(int, 2);
211 recvbuf = xbt_new(int, 2 * size);
215 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
217 /* Do the actual job */
219 MPI_Group* group_snd = xbt_new(MPI_Group, size);
220 std::vector<std::pair<int, int>> rankmap;
221 rankmap.reserve(size);
222 for (int i = 0; i < size; i++) {
223 if (recvbuf[2 * i] != MPI_UNDEFINED) {
225 for (int j = i + 1; j < size; j++) {
226 if(recvbuf[2 * i] == recvbuf[2 * j]) {
227 recvbuf[2 * j] = MPI_UNDEFINED;
228 rankmap.push_back({recvbuf[2 * j + 1], j});
231 /* Add self in the group */
232 recvbuf[2 * i] = MPI_UNDEFINED;
233 rankmap.push_back({recvbuf[2 * i + 1], i});
234 std::sort(begin(rankmap), end(rankmap));
235 group_out = new Group(rankmap.size());
237 group_root = group_out; /* Save root's group */
239 for (unsigned j = 0; j < rankmap.size(); j++) {
240 s4u::ActorPtr actor = group->actor(rankmap[j].second);
241 group_out->set_mapping(actor, j);
243 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
245 for (auto const& rank : rankmap) {
246 if (rank.second != 0) {
247 group_snd[reqs]=new Group(group_out);
248 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
252 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
253 Group::unref(group_out);
255 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
261 group_out = group_root; /* exit with root's group */
263 if(color != MPI_UNDEFINED) {
264 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
265 } /* otherwise, exit with group_out == nullptr */
267 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
271 if (this == MPI_COMM_UNINITIALIZED){
272 smpi_process()->comm_world()->ref();
279 void Comm::cleanup_smp(){
280 if (intra_comm_ != MPI_COMM_NULL)
281 Comm::unref(intra_comm_);
282 if (leaders_comm_ != MPI_COMM_NULL)
283 Comm::unref(leaders_comm_);
284 xbt_free(non_uniform_map_);
285 delete[] leaders_map_;
288 void Comm::unref(Comm* comm){
289 if (comm == MPI_COMM_UNINITIALIZED){
290 Comm::unref(smpi_process()->comm_world());
294 Group::unref(comm->group_);
296 if(comm->refcount_==0){
298 comm->cleanup_attr<Comm>();
303 void Comm::init_smp(){
306 if (this == MPI_COMM_UNINITIALIZED)
307 smpi_process()->comm_world()->init_smp();
309 int comm_size = this->size();
311 // If we are in replay - perform an ugly hack
312 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
313 bool replaying = false; //cache data to set it back again after
314 if(smpi_process()->replaying()){
316 smpi_process()->set_replaying(false);
319 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
320 // we need to switch as the called function may silently touch global variables
321 smpi_switch_data_segment(s4u::Actor::self());
323 //identify neighbours in comm
324 //get the indices of all processes sharing the same simix host
325 auto& process_list = sg_host_self()->pimpl_->process_list_;
326 int intra_comm_size = 0;
327 int min_index = INT_MAX; // the minimum index will be the leader
328 for (auto& actor : process_list) {
329 int index = actor.get_pid();
330 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
332 if (index < min_index)
336 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
337 MPI_Group group_intra = new Group(intra_comm_size);
339 for (auto& actor : process_list) {
340 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
341 group_intra->set_mapping(actor.iface(), i);
346 MPI_Comm comm_intra = new Comm(group_intra, nullptr, 1);
349 int* leaders_map = new int[comm_size];
350 int* leader_list = new int[comm_size];
351 std::fill_n(leaders_map, comm_size, 0);
352 std::fill_n(leader_list, comm_size, -1);
354 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
356 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
357 // we need to switch as the called function may silently touch global variables
358 smpi_switch_data_segment(s4u::Actor::self());
361 if(leaders_map_==nullptr){
362 leaders_map_= leaders_map;
364 delete[] leaders_map;
366 int leader_group_size = 0;
367 for(i=0; i<comm_size; i++){
368 int already_done = 0;
369 for (int j = 0; j < leader_group_size; j++) {
370 if (leaders_map_[i] == leader_list[j]) {
374 if (already_done == 0) {
375 leader_list[leader_group_size] = leaders_map_[i];
379 std::sort(leader_list, leader_list + leader_group_size);
381 MPI_Group leaders_group = new Group(leader_group_size);
383 MPI_Comm leader_comm = MPI_COMM_NULL;
384 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
385 //create leader_communicator
386 for (i=0; i< leader_group_size;i++)
387 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
388 leader_comm = new Comm(leaders_group, nullptr,1);
389 this->set_leaders_comm(leader_comm);
390 this->set_intra_comm(comm_intra);
392 // create intracommunicator
394 for (i=0; i< leader_group_size;i++)
395 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
397 if(this->get_leaders_comm()==MPI_COMM_NULL){
398 leader_comm = new Comm(leaders_group, nullptr,1);
399 this->set_leaders_comm(leader_comm);
401 leader_comm=this->get_leaders_comm();
402 Group::unref(leaders_group);
404 smpi_process()->set_comm_intra(comm_intra);
407 // Are the nodes uniform ? = same number of process/node
408 int my_local_size=comm_intra->size();
409 if(comm_intra->rank()==0) {
411 int* non_uniform_map = xbt_new0(int,leader_group_size);
412 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
413 non_uniform_map, 1, MPI_INT, leader_comm);
414 for(i=0; i < leader_group_size; i++) {
415 if(non_uniform_map[0] != non_uniform_map[i]) {
420 if(is_uniform==0 && this->is_uniform()!=0){
421 non_uniform_map_ = non_uniform_map;
423 xbt_free(non_uniform_map);
425 is_uniform_=is_uniform;
427 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
429 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
430 // we need to switch as the called function may silently touch global variables
431 smpi_switch_data_segment(s4u::Actor::self());
433 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
435 int prev=this->group()->rank(comm_intra->group()->actor(0));
436 for (i = 1; i < my_local_size; i++) {
437 int that = this->group()->rank(comm_intra->group()->actor(i));
438 if (that != prev + 1) {
446 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
448 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
450 is_blocked_ = global_blocked;
453 is_blocked_=global_blocked;
455 delete[] leader_list;
458 smpi_process()->set_replaying(true);
461 MPI_Comm Comm::f2c(int id) {
463 return MPI_COMM_SELF;
465 return MPI_COMM_WORLD;
466 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
468 const auto& lookup = F2C::f2c_lookup();
469 auto comm = lookup->find(get_key_id(key, id));
470 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
472 return MPI_COMM_NULL;
476 void Comm::free_f(int id) {
478 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
482 if(F2C::f2c_lookup()==nullptr){
483 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
486 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
488 return F2C::f2c_id()-1;
491 void Comm::add_rma_win(MPI_Win win){
492 rma_wins_.push_back(win);
495 void Comm::remove_rma_win(MPI_Win win){
496 rma_wins_.remove(win);
499 void Comm::finish_rma_calls(){
500 for (auto const& it : rma_wins_) {
501 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
502 int finished = it->finish_comms();
503 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
508 MPI_Info Comm::info(){
509 if(info_== MPI_INFO_NULL)
515 void Comm::set_info(MPI_Info info){
516 if(info_!= MPI_INFO_NULL)
521 MPI_Comm Comm::split_type(int type, int /*key*/, MPI_Info)
523 //MPI_UNDEFINED can be given to some nodes... but we need them to still perform the smp part which is collective
524 if(type != MPI_COMM_TYPE_SHARED && type != MPI_UNDEFINED){
525 return MPI_COMM_NULL;
529 this->get_intra_comm()->ref();
530 if(type != MPI_UNDEFINED)
531 return this->get_intra_comm();
533 return MPI_COMM_NULL;
537 } // namespace simgrid