1 /* Copyright (c) 2010-2017. 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"
9 #include "simgrid/s4u/Host.hpp"
10 #include "smpi_coll.hpp"
11 #include "smpi_datatype.hpp"
12 #include "smpi_process.hpp"
13 #include "smpi_request.hpp"
14 #include "smpi_status.hpp"
15 #include "smpi_win.hpp"
16 #include "src/simix/smx_private.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
23 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
24 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
26 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
27 * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
32 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
33 int Comm::keyval_id_=0;
35 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
38 topoType_ = MPI_INVALID_TOPO;
39 intra_comm_ = MPI_COMM_NULL;
40 leaders_comm_ = MPI_COMM_NULL;
42 non_uniform_map_ = nullptr;
43 leaders_map_ = nullptr;
47 void Comm::destroy(Comm* comm)
49 if (comm == MPI_COMM_UNINITIALIZED){
50 Comm::destroy(smpi_process()->comm_world());
53 delete comm->topo_; // there's no use count on topos
57 int Comm::dup(MPI_Comm* newcomm){
58 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
59 smpi_switch_data_segment(smpi_process()->index());
61 MPI_Group cp = new Group(this->group());
62 (*newcomm) = new Comm(cp, this->topo());
63 int ret = MPI_SUCCESS;
65 if (not attributes()->empty()) {
68 for(auto it : *attributes()){
69 smpi_key_elem elem = keyvals_.at(it.first);
70 if (elem != nullptr && elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN) {
71 ret = elem->copy_fn.comm_copy_fn(this, it.first, nullptr, it.second, &value_out, &flag);
72 if (ret != MPI_SUCCESS) {
73 Comm::destroy(*newcomm);
74 *newcomm = MPI_COMM_NULL;
79 (*newcomm)->attributes()->insert({it.first, value_out});
87 MPI_Group Comm::group()
89 if (this == MPI_COMM_UNINITIALIZED)
90 return smpi_process()->comm_world()->group();
94 MPI_Topology Comm::topo() {
100 if (this == MPI_COMM_UNINITIALIZED)
101 return smpi_process()->comm_world()->size();
102 return group_->size();
107 if (this == MPI_COMM_UNINITIALIZED)
108 return smpi_process()->comm_world()->rank();
109 return group_->rank(smpi_process()->index());
112 void Comm::get_name (char* name, int* len)
114 if (this == MPI_COMM_UNINITIALIZED){
115 smpi_process()->comm_world()->get_name(name, len);
118 if(this == MPI_COMM_WORLD) {
119 strncpy(name, "WORLD",5);
122 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
126 void Comm::set_leaders_comm(MPI_Comm leaders){
127 if (this == MPI_COMM_UNINITIALIZED){
128 smpi_process()->comm_world()->set_leaders_comm(leaders);
131 leaders_comm_=leaders;
134 void Comm::set_intra_comm(MPI_Comm leaders){
138 int* Comm::get_non_uniform_map(){
139 if (this == MPI_COMM_UNINITIALIZED)
140 return smpi_process()->comm_world()->get_non_uniform_map();
141 return non_uniform_map_;
144 int* Comm::get_leaders_map(){
145 if (this == MPI_COMM_UNINITIALIZED)
146 return smpi_process()->comm_world()->get_leaders_map();
150 MPI_Comm Comm::get_leaders_comm(){
151 if (this == MPI_COMM_UNINITIALIZED)
152 return smpi_process()->comm_world()->get_leaders_comm();
153 return leaders_comm_;
156 MPI_Comm Comm::get_intra_comm(){
157 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
158 return smpi_process()->comm_intra();
159 else return intra_comm_;
162 int Comm::is_uniform(){
163 if (this == MPI_COMM_UNINITIALIZED)
164 return smpi_process()->comm_world()->is_uniform();
168 int Comm::is_blocked(){
169 if (this == MPI_COMM_UNINITIALIZED)
170 return smpi_process()->comm_world()->is_blocked();
174 MPI_Comm Comm::split(int color, int key)
176 if (this == MPI_COMM_UNINITIALIZED)
177 return smpi_process()->comm_world()->split(color, key);
178 int system_tag = 123;
181 MPI_Group group_root = nullptr;
182 MPI_Group group_out = nullptr;
183 MPI_Group group = this->group();
184 int rank = this->rank();
185 int size = this->size();
186 /* Gather all colors and keys on rank 0 */
187 int* sendbuf = xbt_new(int, 2);
191 recvbuf = xbt_new(int, 2 * size);
195 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
197 /* Do the actual job */
199 MPI_Group* group_snd = xbt_new(MPI_Group, size);
200 std::vector<std::pair<int, int>> rankmap;
201 rankmap.reserve(size);
202 for (int i = 0; i < size; i++) {
203 if (recvbuf[2 * i] != MPI_UNDEFINED) {
205 for (int j = i + 1; j < size; j++) {
206 if(recvbuf[2 * i] == recvbuf[2 * j]) {
207 recvbuf[2 * j] = MPI_UNDEFINED;
208 rankmap.push_back({recvbuf[2 * j + 1], j});
211 /* Add self in the group */
212 recvbuf[2 * i] = MPI_UNDEFINED;
213 rankmap.push_back({recvbuf[2 * i + 1], i});
214 std::sort(begin(rankmap), end(rankmap));
215 group_out = new Group(rankmap.size());
217 group_root = group_out; /* Save root's group */
219 for (unsigned j = 0; j < rankmap.size(); j++) {
220 int index = group->index(rankmap[j].second);
221 group_out->set_mapping(index, j);
223 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
225 for (const auto& rank : rankmap) {
226 if (rank.second != 0) {
227 group_snd[reqs]=new Group(group_out);
228 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
232 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
233 Group::unref(group_out);
235 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
241 group_out = group_root; /* exit with root's group */
243 if(color != MPI_UNDEFINED) {
244 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
245 } /* otherwise, exit with group_out == nullptr */
247 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
251 if (this == MPI_COMM_UNINITIALIZED){
252 smpi_process()->comm_world()->ref();
259 void Comm::cleanup_smp(){
260 if (intra_comm_ != MPI_COMM_NULL)
261 Comm::unref(intra_comm_);
262 if (leaders_comm_ != MPI_COMM_NULL)
263 Comm::unref(leaders_comm_);
264 if (non_uniform_map_ != nullptr)
265 xbt_free(non_uniform_map_);
266 if (leaders_map_ != nullptr)
267 xbt_free(leaders_map_);
270 void Comm::unref(Comm* comm){
271 if (comm == MPI_COMM_UNINITIALIZED){
272 Comm::unref(smpi_process()->comm_world());
276 Group::unref(comm->group_);
278 if(comm->refcount_==0){
280 comm->cleanup_attr<Comm>();
285 void Comm::init_smp(){
288 if (this == MPI_COMM_UNINITIALIZED)
289 smpi_process()->comm_world()->init_smp();
291 int comm_size = this->size();
293 // If we are in replay - perform an ugly hack
294 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
295 bool replaying = false; //cache data to set it back again after
296 if(smpi_process()->replaying()){
298 smpi_process()->set_replaying(false);
301 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
302 smpi_switch_data_segment(smpi_process()->index());
304 //identify neighbours in comm
305 //get the indexes of all processes sharing the same simix host
306 xbt_swag_t process_list = sg_host_self()->extension<simgrid::simix::Host>()->process_list;
307 int intra_comm_size = 0;
308 int min_index = INT_MAX; // the minimum index will be the leader
309 smx_actor_t actor = nullptr;
310 xbt_swag_foreach(actor, process_list)
312 int index = actor->pid - 1;
314 if (this->group()->rank(index) != MPI_UNDEFINED) {
316 // the process is in the comm
317 if (index < min_index)
321 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
322 MPI_Group group_intra = new Group(intra_comm_size);
325 xbt_swag_foreach(actor, process_list) {
326 int index = actor->pid -1;
327 if(this->group()->rank(index)!=MPI_UNDEFINED){
328 group_intra->set_mapping(index, i);
333 MPI_Comm comm_intra = new Comm(group_intra, nullptr);
336 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
337 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
338 for(i=0; i<comm_size; i++){
342 Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
344 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
345 smpi_switch_data_segment(smpi_process()->index());
348 if(leaders_map_==nullptr){
349 leaders_map_= leaders_map;
351 xbt_free(leaders_map);
354 int leader_group_size = 0;
355 for(i=0; i<comm_size; i++){
356 int already_done = 0;
357 for (j = 0; j < leader_group_size; j++) {
358 if (leaders_map_[i] == leader_list[j]) {
362 if (already_done == 0) {
363 leader_list[leader_group_size] = leaders_map_[i];
367 std::sort(leader_list, leader_list + leader_group_size);
369 MPI_Group leaders_group = new Group(leader_group_size);
371 MPI_Comm leader_comm = MPI_COMM_NULL;
372 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
373 //create leader_communicator
374 for (i=0; i< leader_group_size;i++)
375 leaders_group->set_mapping(leader_list[i], i);
376 leader_comm = new Comm(leaders_group, nullptr);
377 this->set_leaders_comm(leader_comm);
378 this->set_intra_comm(comm_intra);
380 // create intracommunicator
382 for (i=0; i< leader_group_size;i++)
383 leaders_group->set_mapping(leader_list[i], i);
385 if(this->get_leaders_comm()==MPI_COMM_NULL){
386 leader_comm = new Comm(leaders_group, nullptr);
387 this->set_leaders_comm(leader_comm);
389 leader_comm=this->get_leaders_comm();
390 Group::unref(leaders_group);
392 smpi_process()->set_comm_intra(comm_intra);
395 // Are the nodes uniform ? = same number of process/node
396 int my_local_size=comm_intra->size();
397 if(comm_intra->rank()==0) {
399 int* non_uniform_map = xbt_new0(int,leader_group_size);
400 Coll_allgather_mpich::allgather(&my_local_size, 1, MPI_INT,
401 non_uniform_map, 1, MPI_INT, leader_comm);
402 for(i=0; i < leader_group_size; i++) {
403 if(non_uniform_map[0] != non_uniform_map[i]) {
408 if(is_uniform==0 && this->is_uniform()!=0){
409 non_uniform_map_ = non_uniform_map;
411 xbt_free(non_uniform_map);
413 is_uniform_=is_uniform;
415 Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
417 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
418 smpi_switch_data_segment(smpi_process()->index());
420 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
422 int prev=this->group()->rank(comm_intra->group()->index(0));
423 for (i = 1; i < my_local_size; i++) {
424 int that = this->group()->rank(comm_intra->group()->index(i));
425 if (that != prev + 1) {
433 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
435 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
437 is_blocked_ = global_blocked;
440 is_blocked_=global_blocked;
442 xbt_free(leader_list);
445 smpi_process()->set_replaying(true);
448 MPI_Comm Comm::f2c(int id) {
450 return MPI_COMM_SELF;
452 return MPI_COMM_WORLD;
453 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
455 const auto& lookup = F2C::f2c_lookup();
456 auto comm = lookup->find(get_key_id(key, id));
457 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
459 return MPI_COMM_NULL;
463 void Comm::free_f(int id) {
465 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
469 if(F2C::f2c_lookup()==nullptr){
470 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
473 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
475 return F2C::f2c_id()-1;
478 void Comm::add_rma_win(MPI_Win win){
479 rma_wins_.push_back(win);
482 void Comm::remove_rma_win(MPI_Win win){
483 rma_wins_.remove(win);
486 void Comm::finish_rma_calls(){
487 for(auto it : rma_wins_){
488 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
489 int finished = it->finish_comms();
490 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);