1 /* Copyright (c) 2010-2018. 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"
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"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
22 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
23 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
25 using simgrid::s4u::ActorPtr;
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. */
33 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
34 int Comm::keyval_id_=0;
36 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
39 topoType_ = MPI_INVALID_TOPO;
40 intra_comm_ = MPI_COMM_NULL;
41 leaders_comm_ = MPI_COMM_NULL;
43 non_uniform_map_ = nullptr;
44 leaders_map_ = nullptr;
48 void Comm::destroy(Comm* comm)
50 if (comm == MPI_COMM_UNINITIALIZED){
51 Comm::destroy(smpi_process()->comm_world());
54 delete comm->topo_; // there's no use count on topos
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());
62 MPI_Group cp = new Group(this->group());
63 (*newcomm) = new Comm(cp, this->topo());
64 int ret = MPI_SUCCESS;
66 if (not attributes()->empty()) {
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;
80 (*newcomm)->attributes()->insert({it.first, value_out});
88 MPI_Group Comm::group()
90 if (this == MPI_COMM_UNINITIALIZED)
91 return smpi_process()->comm_world()->group();
95 MPI_Topology Comm::topo() {
101 if (this == MPI_COMM_UNINITIALIZED)
102 return smpi_process()->comm_world()->size();
103 return group_->size();
108 if (this == MPI_COMM_UNINITIALIZED)
109 return smpi_process()->comm_world()->rank();
110 return group_->rank(simgrid::s4u::Actor::self());
113 void Comm::get_name (char* name, int* len)
115 if (this == MPI_COMM_UNINITIALIZED){
116 smpi_process()->comm_world()->get_name(name, len);
119 if(this == MPI_COMM_WORLD) {
120 strncpy(name, "WORLD",5);
123 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
127 void Comm::set_leaders_comm(MPI_Comm leaders){
128 if (this == MPI_COMM_UNINITIALIZED){
129 smpi_process()->comm_world()->set_leaders_comm(leaders);
132 leaders_comm_=leaders;
135 void Comm::set_intra_comm(MPI_Comm leaders){
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_;
145 int* Comm::get_leaders_map(){
146 if (this == MPI_COMM_UNINITIALIZED)
147 return smpi_process()->comm_world()->get_leaders_map();
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_;
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_;
163 int Comm::is_uniform(){
164 if (this == MPI_COMM_UNINITIALIZED)
165 return smpi_process()->comm_world()->is_uniform();
169 int Comm::is_blocked(){
170 if (this == MPI_COMM_UNINITIALIZED)
171 return smpi_process()->comm_world()->is_blocked();
175 MPI_Comm Comm::split(int color, int key)
177 if (this == MPI_COMM_UNINITIALIZED)
178 return smpi_process()->comm_world()->split(color, key);
179 int system_tag = 123;
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);
192 recvbuf = xbt_new(int, 2 * size);
196 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
198 /* Do the actual job */
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) {
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});
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());
218 group_root = group_out; /* Save root's group */
220 for (unsigned j = 0; j < rankmap.size(); j++) {
221 ActorPtr actor = group->actor(rankmap[j].second);
222 group_out->set_mapping(actor, j);
224 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
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);
233 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
234 Group::unref(group_out);
236 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
242 group_out = group_root; /* exit with root's group */
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 */
248 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
252 if (this == MPI_COMM_UNINITIALIZED){
253 smpi_process()->comm_world()->ref();
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_;
271 void Comm::unref(Comm* comm){
272 if (comm == MPI_COMM_UNINITIALIZED){
273 Comm::unref(smpi_process()->comm_world());
277 Group::unref(comm->group_);
279 if(comm->refcount_==0){
281 comm->cleanup_attr<Comm>();
286 void Comm::init_smp(){
289 if (this == MPI_COMM_UNINITIALIZED)
290 smpi_process()->comm_world()->init_smp();
292 int comm_size = this->size();
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()){
299 smpi_process()->set_replaying(false);
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());
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?
314 if (index < min_index)
318 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
319 MPI_Group group_intra = new Group(intra_comm_size);
321 for (auto& actor : process_list) {
322 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
323 group_intra->set_mapping(actor.iface(), i);
328 MPI_Comm comm_intra = new Comm(group_intra, nullptr);
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);
336 Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
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());
342 if(leaders_map_==nullptr){
343 leaders_map_= leaders_map;
345 delete[] leaders_map;
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]) {
355 if (already_done == 0) {
356 leader_list[leader_group_size] = leaders_map_[i];
360 std::sort(leader_list, leader_list + leader_group_size);
362 MPI_Group leaders_group = new Group(leader_group_size);
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);
373 // create intracommunicator
375 for (i=0; i< leader_group_size;i++)
376 leaders_group->set_mapping(simgrid::s4u::Actor::byPid(leader_list[i]), i);
378 if(this->get_leaders_comm()==MPI_COMM_NULL){
379 leader_comm = new Comm(leaders_group, nullptr);
380 this->set_leaders_comm(leader_comm);
382 leader_comm=this->get_leaders_comm();
383 Group::unref(leaders_group);
385 smpi_process()->set_comm_intra(comm_intra);
388 // Are the nodes uniform ? = same number of process/node
389 int my_local_size=comm_intra->size();
390 if(comm_intra->rank()==0) {
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]) {
401 if(is_uniform==0 && this->is_uniform()!=0){
402 non_uniform_map_ = non_uniform_map;
404 xbt_free(non_uniform_map);
406 is_uniform_=is_uniform;
408 Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
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());
413 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
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) {
426 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
428 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
430 is_blocked_ = global_blocked;
433 is_blocked_=global_blocked;
435 delete[] leader_list;
438 smpi_process()->set_replaying(true);
441 MPI_Comm Comm::f2c(int id) {
443 return MPI_COMM_SELF;
445 return MPI_COMM_WORLD;
446 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
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);
452 return MPI_COMM_NULL;
456 void Comm::free_f(int id) {
458 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
462 if(F2C::f2c_lookup()==nullptr){
463 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
466 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
468 return F2C::f2c_id()-1;
471 void Comm::add_rma_win(MPI_Win win){
472 rma_wins_.push_back(win);
475 void Comm::remove_rma_win(MPI_Win win){
476 rma_wins_.remove(win);
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);