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 "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();
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(simgrid::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) {
124 strncpy(name, "WORLD", 6);
127 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
131 void Comm::set_leaders_comm(MPI_Comm leaders){
132 if (this == MPI_COMM_UNINITIALIZED){
133 smpi_process()->comm_world()->set_leaders_comm(leaders);
136 leaders_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 int Comm::is_smp_comm(){
176 if (this == MPI_COMM_UNINITIALIZED)
177 return smpi_process()->comm_world()->is_smp_comm();
181 MPI_Comm Comm::split(int color, int key)
183 if (this == MPI_COMM_UNINITIALIZED)
184 return smpi_process()->comm_world()->split(color, key);
185 int system_tag = 123;
188 MPI_Group group_root = nullptr;
189 MPI_Group group_out = nullptr;
190 MPI_Group group = this->group();
191 int myrank = this->rank();
192 int size = this->size();
193 /* Gather all colors and keys on rank 0 */
194 int* sendbuf = xbt_new(int, 2);
198 recvbuf = xbt_new(int, 2 * size);
202 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
204 /* Do the actual job */
206 MPI_Group* group_snd = xbt_new(MPI_Group, size);
207 std::vector<std::pair<int, int>> rankmap;
208 rankmap.reserve(size);
209 for (int i = 0; i < size; i++) {
210 if (recvbuf[2 * i] != MPI_UNDEFINED) {
212 for (int j = i + 1; j < size; j++) {
213 if(recvbuf[2 * i] == recvbuf[2 * j]) {
214 recvbuf[2 * j] = MPI_UNDEFINED;
215 rankmap.push_back({recvbuf[2 * j + 1], j});
218 /* Add self in the group */
219 recvbuf[2 * i] = MPI_UNDEFINED;
220 rankmap.push_back({recvbuf[2 * i + 1], i});
221 std::sort(begin(rankmap), end(rankmap));
222 group_out = new Group(rankmap.size());
224 group_root = group_out; /* Save root's group */
226 for (unsigned j = 0; j < rankmap.size(); j++) {
227 ActorPtr actor = group->actor(rankmap[j].second);
228 group_out->set_mapping(actor, j);
230 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
232 for (auto const& rank : rankmap) {
233 if (rank.second != 0) {
234 group_snd[reqs]=new Group(group_out);
235 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
239 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
240 Group::unref(group_out);
242 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
248 group_out = group_root; /* exit with root's group */
250 if(color != MPI_UNDEFINED) {
251 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
252 } /* otherwise, exit with group_out == nullptr */
254 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
258 if (this == MPI_COMM_UNINITIALIZED){
259 smpi_process()->comm_world()->ref();
266 void Comm::cleanup_smp(){
267 if (intra_comm_ != MPI_COMM_NULL)
268 Comm::unref(intra_comm_);
269 if (leaders_comm_ != MPI_COMM_NULL)
270 Comm::unref(leaders_comm_);
271 xbt_free(non_uniform_map_);
272 delete[] leaders_map_;
275 void Comm::unref(Comm* comm){
276 if (comm == MPI_COMM_UNINITIALIZED){
277 Comm::unref(smpi_process()->comm_world());
281 Group::unref(comm->group_);
283 if(comm->refcount_==0){
285 comm->cleanup_attr<Comm>();
290 void Comm::init_smp(){
293 if (this == MPI_COMM_UNINITIALIZED)
294 smpi_process()->comm_world()->init_smp();
296 int comm_size = this->size();
298 // If we are in replay - perform an ugly hack
299 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
300 bool replaying = false; //cache data to set it back again after
301 if(smpi_process()->replaying()){
303 smpi_process()->set_replaying(false);
306 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
307 // we need to switch as the called function may silently touch global variables
308 smpi_switch_data_segment(simgrid::s4u::Actor::self());
310 //identify neighbours in comm
311 //get the indices of all processes sharing the same simix host
312 auto& process_list = sg_host_self()->pimpl_->process_list_;
313 int intra_comm_size = 0;
314 int min_index = INT_MAX; // the minimum index will be the leader
315 for (auto& actor : process_list) {
316 int index = actor.get_pid();
317 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
319 if (index < min_index)
323 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
324 MPI_Group group_intra = new Group(intra_comm_size);
326 for (auto& actor : process_list) {
327 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
328 group_intra->set_mapping(actor.iface(), i);
333 MPI_Comm comm_intra = new Comm(group_intra, nullptr, 1);
336 int* leaders_map = new int[comm_size];
337 int* leader_list = new int[comm_size];
338 std::fill_n(leaders_map, comm_size, 0);
339 std::fill_n(leader_list, comm_size, -1);
341 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
343 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
344 // we need to switch as the called function may silently touch global variables
345 smpi_switch_data_segment(simgrid::s4u::Actor::self());
348 if(leaders_map_==nullptr){
349 leaders_map_= leaders_map;
351 delete[] leaders_map;
353 int leader_group_size = 0;
354 for(i=0; i<comm_size; i++){
355 int already_done = 0;
356 for (int j = 0; j < leader_group_size; j++) {
357 if (leaders_map_[i] == leader_list[j]) {
361 if (already_done == 0) {
362 leader_list[leader_group_size] = leaders_map_[i];
366 std::sort(leader_list, leader_list + leader_group_size);
368 MPI_Group leaders_group = new Group(leader_group_size);
370 MPI_Comm leader_comm = MPI_COMM_NULL;
371 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
372 //create leader_communicator
373 for (i=0; i< leader_group_size;i++)
374 leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
375 leader_comm = new Comm(leaders_group, nullptr,1);
376 this->set_leaders_comm(leader_comm);
377 this->set_intra_comm(comm_intra);
379 // create intracommunicator
381 for (i=0; i< leader_group_size;i++)
382 leaders_group->set_mapping(simgrid::s4u::Actor::by_pid(leader_list[i]), i);
384 if(this->get_leaders_comm()==MPI_COMM_NULL){
385 leader_comm = new Comm(leaders_group, nullptr,1);
386 this->set_leaders_comm(leader_comm);
388 leader_comm=this->get_leaders_comm();
389 Group::unref(leaders_group);
391 smpi_process()->set_comm_intra(comm_intra);
394 // Are the nodes uniform ? = same number of process/node
395 int my_local_size=comm_intra->size();
396 if(comm_intra->rank()==0) {
398 int* non_uniform_map = xbt_new0(int,leader_group_size);
399 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
400 non_uniform_map, 1, MPI_INT, leader_comm);
401 for(i=0; i < leader_group_size; i++) {
402 if(non_uniform_map[0] != non_uniform_map[i]) {
407 if(is_uniform==0 && this->is_uniform()!=0){
408 non_uniform_map_ = non_uniform_map;
410 xbt_free(non_uniform_map);
412 is_uniform_=is_uniform;
414 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
416 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
417 // we need to switch as the called function may silently touch global variables
418 smpi_switch_data_segment(simgrid::s4u::Actor::self());
420 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
422 int prev=this->group()->rank(comm_intra->group()->actor(0));
423 for (i = 1; i < my_local_size; i++) {
424 int that = this->group()->rank(comm_intra->group()->actor(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 delete[] 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 const& 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);
495 MPI_Comm Comm::split_type(int type, int key, MPI_Info info)
497 if(type != MPI_COMM_TYPE_SHARED){
498 return MPI_COMM_NULL;
502 this->get_intra_comm()->ref();
503 return this->get_intra_comm();
507 } // namespace simgrid