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"
7 #include "smpi_coll.hpp"
8 #include "smpi_datatype.hpp"
9 #include "smpi_request.hpp"
10 #include "smpi_win.hpp"
11 #include "src/smpi/include/smpi_actor.hpp"
12 #include "src/surf/HostImpl.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
18 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
19 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
21 using simgrid::s4u::ActorPtr;
23 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
24 * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
29 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
30 int Comm::keyval_id_=0;
32 Comm::Comm(MPI_Group group, MPI_Topology topo, int smp) : group_(group), topo_(topo),is_smp_comm_(smp)
35 topoType_ = MPI_INVALID_TOPO;
36 intra_comm_ = MPI_COMM_NULL;
37 leaders_comm_ = MPI_COMM_NULL;
39 non_uniform_map_ = nullptr;
40 leaders_map_ = nullptr;
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(simgrid::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();
103 MPI_Topology Comm::topo() {
109 if (this == MPI_COMM_UNINITIALIZED)
110 return smpi_process()->comm_world()->size();
111 return group_->size();
116 if (this == MPI_COMM_UNINITIALIZED)
117 return smpi_process()->comm_world()->rank();
118 return group_->rank(simgrid::s4u::Actor::self());
121 void Comm::get_name (char* name, int* len)
123 if (this == MPI_COMM_UNINITIALIZED){
124 smpi_process()->comm_world()->get_name(name, len);
127 if(this == MPI_COMM_WORLD) {
128 strncpy(name, "WORLD", 6);
131 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
135 void Comm::set_leaders_comm(MPI_Comm leaders){
136 if (this == MPI_COMM_UNINITIALIZED){
137 smpi_process()->comm_world()->set_leaders_comm(leaders);
140 leaders_comm_=leaders;
143 void Comm::set_intra_comm(MPI_Comm leaders){
147 int* Comm::get_non_uniform_map(){
148 if (this == MPI_COMM_UNINITIALIZED)
149 return smpi_process()->comm_world()->get_non_uniform_map();
150 return non_uniform_map_;
153 int* Comm::get_leaders_map(){
154 if (this == MPI_COMM_UNINITIALIZED)
155 return smpi_process()->comm_world()->get_leaders_map();
159 MPI_Comm Comm::get_leaders_comm(){
160 if (this == MPI_COMM_UNINITIALIZED)
161 return smpi_process()->comm_world()->get_leaders_comm();
162 return leaders_comm_;
165 MPI_Comm Comm::get_intra_comm(){
166 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
167 return smpi_process()->comm_intra();
168 else return intra_comm_;
171 int Comm::is_uniform(){
172 if (this == MPI_COMM_UNINITIALIZED)
173 return smpi_process()->comm_world()->is_uniform();
177 int Comm::is_blocked(){
178 if (this == MPI_COMM_UNINITIALIZED)
179 return smpi_process()->comm_world()->is_blocked();
183 int Comm::is_smp_comm(){
184 if (this == MPI_COMM_UNINITIALIZED)
185 return smpi_process()->comm_world()->is_smp_comm();
189 MPI_Comm Comm::split(int color, int key)
191 if (this == MPI_COMM_UNINITIALIZED)
192 return smpi_process()->comm_world()->split(color, key);
193 int system_tag = 123;
196 MPI_Group group_root = nullptr;
197 MPI_Group group_out = nullptr;
198 MPI_Group group = this->group();
199 int rank = this->rank();
200 int size = this->size();
201 /* Gather all colors and keys on rank 0 */
202 int* sendbuf = xbt_new(int, 2);
206 recvbuf = xbt_new(int, 2 * size);
210 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
212 /* Do the actual job */
214 MPI_Group* group_snd = xbt_new(MPI_Group, size);
215 std::vector<std::pair<int, int>> rankmap;
216 rankmap.reserve(size);
217 for (int i = 0; i < size; i++) {
218 if (recvbuf[2 * i] != MPI_UNDEFINED) {
220 for (int j = i + 1; j < size; j++) {
221 if(recvbuf[2 * i] == recvbuf[2 * j]) {
222 recvbuf[2 * j] = MPI_UNDEFINED;
223 rankmap.push_back({recvbuf[2 * j + 1], j});
226 /* Add self in the group */
227 recvbuf[2 * i] = MPI_UNDEFINED;
228 rankmap.push_back({recvbuf[2 * i + 1], i});
229 std::sort(begin(rankmap), end(rankmap));
230 group_out = new Group(rankmap.size());
232 group_root = group_out; /* Save root's group */
234 for (unsigned j = 0; j < rankmap.size(); j++) {
235 ActorPtr actor = group->actor(rankmap[j].second);
236 group_out->set_mapping(actor, j);
238 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
240 for (auto const& rank : rankmap) {
241 if (rank.second != 0) {
242 group_snd[reqs]=new Group(group_out);
243 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
247 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
248 Group::unref(group_out);
250 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
256 group_out = group_root; /* exit with root's group */
258 if(color != MPI_UNDEFINED) {
259 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
260 } /* otherwise, exit with group_out == nullptr */
262 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
266 if (this == MPI_COMM_UNINITIALIZED){
267 smpi_process()->comm_world()->ref();
274 void Comm::cleanup_smp(){
275 if (intra_comm_ != MPI_COMM_NULL)
276 Comm::unref(intra_comm_);
277 if (leaders_comm_ != MPI_COMM_NULL)
278 Comm::unref(leaders_comm_);
279 xbt_free(non_uniform_map_);
280 delete[] leaders_map_;
283 void Comm::unref(Comm* comm){
284 if (comm == MPI_COMM_UNINITIALIZED){
285 Comm::unref(smpi_process()->comm_world());
289 Group::unref(comm->group_);
291 if(comm->refcount_==0){
293 comm->cleanup_attr<Comm>();
298 void Comm::init_smp(){
301 if (this == MPI_COMM_UNINITIALIZED)
302 smpi_process()->comm_world()->init_smp();
304 int comm_size = this->size();
306 // If we are in replay - perform an ugly hack
307 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
308 bool replaying = false; //cache data to set it back again after
309 if(smpi_process()->replaying()){
311 smpi_process()->set_replaying(false);
314 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
315 // we need to switch as the called function may silently touch global variables
316 smpi_switch_data_segment(simgrid::s4u::Actor::self());
318 //identify neighbours in comm
319 //get the indices of all processes sharing the same simix host
320 auto& process_list = sg_host_self()->pimpl_->process_list_;
321 int intra_comm_size = 0;
322 int min_index = INT_MAX; // the minimum index will be the leader
323 for (auto& actor : process_list) {
324 int index = actor.pid_;
325 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
327 if (index < min_index)
331 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
332 MPI_Group group_intra = new Group(intra_comm_size);
334 for (auto& actor : process_list) {
335 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
336 group_intra->set_mapping(actor.iface(), i);
341 MPI_Comm comm_intra = new Comm(group_intra, nullptr, 1);
344 int* leaders_map = new int[comm_size];
345 int* leader_list = new int[comm_size];
346 std::fill_n(leaders_map, comm_size, 0);
347 std::fill_n(leader_list, comm_size, -1);
349 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
351 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
352 // we need to switch as the called function may silently touch global variables
353 smpi_switch_data_segment(simgrid::s4u::Actor::self());
356 if(leaders_map_==nullptr){
357 leaders_map_= leaders_map;
359 delete[] leaders_map;
361 int leader_group_size = 0;
362 for(i=0; i<comm_size; i++){
363 int already_done = 0;
364 for (int j = 0; j < leader_group_size; j++) {
365 if (leaders_map_[i] == leader_list[j]) {
369 if (already_done == 0) {
370 leader_list[leader_group_size] = leaders_map_[i];
374 std::sort(leader_list, leader_list + leader_group_size);
376 MPI_Group leaders_group = new Group(leader_group_size);
378 MPI_Comm leader_comm = MPI_COMM_NULL;
379 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
380 //create leader_communicator
381 for (i=0; i< leader_group_size;i++)
382 leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
383 leader_comm = new Comm(leaders_group, nullptr,1);
384 this->set_leaders_comm(leader_comm);
385 this->set_intra_comm(comm_intra);
387 // create intracommunicator
389 for (i=0; i< leader_group_size;i++)
390 leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
392 if(this->get_leaders_comm()==MPI_COMM_NULL){
393 leader_comm = new Comm(leaders_group, nullptr,1);
394 this->set_leaders_comm(leader_comm);
396 leader_comm=this->get_leaders_comm();
397 Group::unref(leaders_group);
399 smpi_process()->set_comm_intra(comm_intra);
402 // Are the nodes uniform ? = same number of process/node
403 int my_local_size=comm_intra->size();
404 if(comm_intra->rank()==0) {
406 int* non_uniform_map = xbt_new0(int,leader_group_size);
407 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
408 non_uniform_map, 1, MPI_INT, leader_comm);
409 for(i=0; i < leader_group_size; i++) {
410 if(non_uniform_map[0] != non_uniform_map[i]) {
415 if(is_uniform==0 && this->is_uniform()!=0){
416 non_uniform_map_ = non_uniform_map;
418 xbt_free(non_uniform_map);
420 is_uniform_=is_uniform;
422 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
424 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
425 // we need to switch as the called function may silently touch global variables
426 smpi_switch_data_segment(simgrid::s4u::Actor::self());
428 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
430 int prev=this->group()->rank(comm_intra->group()->actor(0));
431 for (i = 1; i < my_local_size; i++) {
432 int that = this->group()->rank(comm_intra->group()->actor(i));
433 if (that != prev + 1) {
441 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
443 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
445 is_blocked_ = global_blocked;
448 is_blocked_=global_blocked;
450 delete[] leader_list;
453 smpi_process()->set_replaying(true);
456 MPI_Comm Comm::f2c(int id) {
458 return MPI_COMM_SELF;
460 return MPI_COMM_WORLD;
461 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
463 const auto& lookup = F2C::f2c_lookup();
464 auto comm = lookup->find(get_key_id(key, id));
465 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
467 return MPI_COMM_NULL;
471 void Comm::free_f(int id) {
473 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
477 if(F2C::f2c_lookup()==nullptr){
478 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
481 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
483 return F2C::f2c_id()-1;
486 void Comm::add_rma_win(MPI_Win win){
487 rma_wins_.push_back(win);
490 void Comm::remove_rma_win(MPI_Win win){
491 rma_wins_.remove(win);
494 void Comm::finish_rma_calls(){
495 for (auto const& it : rma_wins_) {
496 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
497 int finished = it->finish_comms();
498 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
503 MPI_Comm Comm::split_type(int type, int key, MPI_Info info)
505 if(type != MPI_COMM_TYPE_SHARED){
506 return MPI_COMM_NULL;
510 this->get_intra_comm()->ref();
511 return this->get_intra_comm();