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 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
22 * support them, we have to add a field SMPI_Topo_type, and replace the MPI_Topology field by an union. */
27 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
28 int Comm::keyval_id_=0;
30 Comm::Comm(MPI_Group group, MPI_Topology topo, int smp) : group_(group), topo_(topo),is_smp_comm_(smp)
33 topoType_ = MPI_INVALID_TOPO;
34 intra_comm_ = MPI_COMM_NULL;
35 leaders_comm_ = MPI_COMM_NULL;
37 non_uniform_map_ = nullptr;
38 leaders_map_ = nullptr;
42 void Comm::destroy(Comm* comm)
44 if (comm == MPI_COMM_UNINITIALIZED){
45 Comm::destroy(smpi_process()->comm_world());
48 delete comm->topo_; // there's no use count on topos
52 int Comm::dup(MPI_Comm* newcomm){
53 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
54 // we need to switch as the called function may silently touch global variables
55 smpi_switch_data_segment(s4u::Actor::self());
57 MPI_Group cp = new Group(this->group());
58 (*newcomm) = new Comm(cp, this->topo());
59 int ret = MPI_SUCCESS;
61 if (not attributes()->empty()) {
63 void* value_out=nullptr;
64 for (auto const& it : *attributes()) {
65 smpi_key_elem elem = keyvals_.at(it.first);
67 if( elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN &&
68 elem->copy_fn.comm_copy_fn != MPI_COMM_DUP_FN)
69 ret = elem->copy_fn.comm_copy_fn(this, it.first, elem->extra_state, it.second, &value_out, &flag);
70 else if ( elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN &&
71 *(int*)*elem->copy_fn.comm_copy_fn_fort != 1){
72 value_out=(int*)xbt_malloc(sizeof(int));
73 elem->copy_fn.comm_copy_fn_fort(this, it.first, elem->extra_state, it.second, value_out, &flag,&ret);
75 if (ret != MPI_SUCCESS) {
76 Comm::destroy(*newcomm);
77 *newcomm = MPI_COMM_NULL;
80 if (elem->copy_fn.comm_copy_fn == MPI_COMM_DUP_FN ||
81 ((elem->copy_fn.comm_copy_fn_fort != MPI_NULL_COPY_FN) && *(int*)*elem->copy_fn.comm_copy_fn_fort == 1)){
83 (*newcomm)->attributes()->insert({it.first, it.second});
86 (*newcomm)->attributes()->insert({it.first, value_out});
94 MPI_Group Comm::group()
96 if (this == MPI_COMM_UNINITIALIZED)
97 return smpi_process()->comm_world()->group();
103 if (this == MPI_COMM_UNINITIALIZED)
104 return smpi_process()->comm_world()->size();
105 return group_->size();
110 if (this == MPI_COMM_UNINITIALIZED)
111 return smpi_process()->comm_world()->rank();
112 return group_->rank(s4u::Actor::self());
115 void Comm::get_name (char* name, int* len)
117 if (this == MPI_COMM_UNINITIALIZED){
118 smpi_process()->comm_world()->get_name(name, len);
121 if(this == MPI_COMM_WORLD) {
122 strncpy(name, "WORLD", 6);
125 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
129 void Comm::set_leaders_comm(MPI_Comm leaders){
130 if (this == MPI_COMM_UNINITIALIZED){
131 smpi_process()->comm_world()->set_leaders_comm(leaders);
134 leaders_comm_=leaders;
137 int* Comm::get_non_uniform_map(){
138 if (this == MPI_COMM_UNINITIALIZED)
139 return smpi_process()->comm_world()->get_non_uniform_map();
140 return non_uniform_map_;
143 int* Comm::get_leaders_map(){
144 if (this == MPI_COMM_UNINITIALIZED)
145 return smpi_process()->comm_world()->get_leaders_map();
149 MPI_Comm Comm::get_leaders_comm(){
150 if (this == MPI_COMM_UNINITIALIZED)
151 return smpi_process()->comm_world()->get_leaders_comm();
152 return leaders_comm_;
155 MPI_Comm Comm::get_intra_comm(){
156 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
157 return smpi_process()->comm_intra();
158 else return intra_comm_;
161 int Comm::is_uniform(){
162 if (this == MPI_COMM_UNINITIALIZED)
163 return smpi_process()->comm_world()->is_uniform();
167 int Comm::is_blocked(){
168 if (this == MPI_COMM_UNINITIALIZED)
169 return smpi_process()->comm_world()->is_blocked();
173 int Comm::is_smp_comm(){
174 if (this == MPI_COMM_UNINITIALIZED)
175 return smpi_process()->comm_world()->is_smp_comm();
179 MPI_Comm Comm::split(int color, int key)
181 if (this == MPI_COMM_UNINITIALIZED)
182 return smpi_process()->comm_world()->split(color, key);
183 int system_tag = 123;
186 MPI_Group group_root = nullptr;
187 MPI_Group group_out = nullptr;
188 MPI_Group group = this->group();
189 int myrank = this->rank();
190 int size = this->size();
191 /* Gather all colors and keys on rank 0 */
192 int* sendbuf = xbt_new(int, 2);
196 recvbuf = xbt_new(int, 2 * size);
200 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
202 /* Do the actual job */
204 MPI_Group* group_snd = xbt_new(MPI_Group, size);
205 std::vector<std::pair<int, int>> rankmap;
206 rankmap.reserve(size);
207 for (int i = 0; i < size; i++) {
208 if (recvbuf[2 * i] != MPI_UNDEFINED) {
210 for (int j = i + 1; j < size; j++) {
211 if(recvbuf[2 * i] == recvbuf[2 * j]) {
212 recvbuf[2 * j] = MPI_UNDEFINED;
213 rankmap.push_back({recvbuf[2 * j + 1], j});
216 /* Add self in the group */
217 recvbuf[2 * i] = MPI_UNDEFINED;
218 rankmap.push_back({recvbuf[2 * i + 1], i});
219 std::sort(begin(rankmap), end(rankmap));
220 group_out = new Group(rankmap.size());
222 group_root = group_out; /* Save root's group */
224 for (unsigned j = 0; j < rankmap.size(); j++) {
225 s4u::ActorPtr actor = group->actor(rankmap[j].second);
226 group_out->set_mapping(actor, j);
228 MPI_Request* requests = xbt_new(MPI_Request, rankmap.size());
230 for (auto const& rank : rankmap) {
231 if (rank.second != 0) {
232 group_snd[reqs]=new Group(group_out);
233 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rank.second, system_tag, this);
237 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
238 Group::unref(group_out);
240 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
246 group_out = group_root; /* exit with root's group */
248 if(color != MPI_UNDEFINED) {
249 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
250 } /* otherwise, exit with group_out == nullptr */
252 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
256 if (this == MPI_COMM_UNINITIALIZED){
257 smpi_process()->comm_world()->ref();
264 void Comm::cleanup_smp(){
265 if (intra_comm_ != MPI_COMM_NULL)
266 Comm::unref(intra_comm_);
267 if (leaders_comm_ != MPI_COMM_NULL)
268 Comm::unref(leaders_comm_);
269 xbt_free(non_uniform_map_);
270 delete[] leaders_map_;
273 void Comm::unref(Comm* comm){
274 if (comm == MPI_COMM_UNINITIALIZED){
275 Comm::unref(smpi_process()->comm_world());
279 Group::unref(comm->group_);
281 if(comm->refcount_==0){
283 comm->cleanup_attr<Comm>();
288 void Comm::init_smp(){
291 if (this == MPI_COMM_UNINITIALIZED)
292 smpi_process()->comm_world()->init_smp();
294 int comm_size = this->size();
296 // If we are in replay - perform an ugly hack
297 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
298 bool replaying = false; //cache data to set it back again after
299 if(smpi_process()->replaying()){
301 smpi_process()->set_replaying(false);
304 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
305 // we need to switch as the called function may silently touch global variables
306 smpi_switch_data_segment(s4u::Actor::self());
308 //identify neighbours in comm
309 //get the indices of all processes sharing the same simix host
310 auto& process_list = sg_host_self()->pimpl_->process_list_;
311 int intra_comm_size = 0;
312 int min_index = INT_MAX; // the minimum index will be the leader
313 for (auto& actor : process_list) {
314 int index = actor.get_pid();
315 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) { // Is this process in the current group?
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);
324 for (auto& actor : process_list) {
325 if (this->group()->rank(actor.iface()) != MPI_UNDEFINED) {
326 group_intra->set_mapping(actor.iface(), i);
331 MPI_Comm comm_intra = new Comm(group_intra, nullptr, 1);
334 int* leaders_map = new int[comm_size];
335 int* leader_list = new int[comm_size];
336 std::fill_n(leaders_map, comm_size, 0);
337 std::fill_n(leader_list, comm_size, -1);
339 Coll_allgather_ring::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
341 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
342 // we need to switch as the called function may silently touch global variables
343 smpi_switch_data_segment(s4u::Actor::self());
346 if(leaders_map_==nullptr){
347 leaders_map_= leaders_map;
349 delete[] leaders_map;
351 int leader_group_size = 0;
352 for(i=0; i<comm_size; i++){
353 int already_done = 0;
354 for (int j = 0; j < leader_group_size; j++) {
355 if (leaders_map_[i] == leader_list[j]) {
359 if (already_done == 0) {
360 leader_list[leader_group_size] = leaders_map_[i];
364 std::sort(leader_list, leader_list + leader_group_size);
366 MPI_Group leaders_group = new Group(leader_group_size);
368 MPI_Comm leader_comm = MPI_COMM_NULL;
369 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
370 //create leader_communicator
371 for (i=0; i< leader_group_size;i++)
372 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
373 leader_comm = new Comm(leaders_group, nullptr,1);
374 this->set_leaders_comm(leader_comm);
375 this->set_intra_comm(comm_intra);
377 // create intracommunicator
379 for (i=0; i< leader_group_size;i++)
380 leaders_group->set_mapping(s4u::Actor::by_pid(leader_list[i]), i);
382 if(this->get_leaders_comm()==MPI_COMM_NULL){
383 leader_comm = new Comm(leaders_group, nullptr,1);
384 this->set_leaders_comm(leader_comm);
386 leader_comm=this->get_leaders_comm();
387 Group::unref(leaders_group);
389 smpi_process()->set_comm_intra(comm_intra);
392 // Are the nodes uniform ? = same number of process/node
393 int my_local_size=comm_intra->size();
394 if(comm_intra->rank()==0) {
396 int* non_uniform_map = xbt_new0(int,leader_group_size);
397 Coll_allgather_ring::allgather(&my_local_size, 1, MPI_INT,
398 non_uniform_map, 1, MPI_INT, leader_comm);
399 for(i=0; i < leader_group_size; i++) {
400 if(non_uniform_map[0] != non_uniform_map[i]) {
405 if(is_uniform==0 && this->is_uniform()!=0){
406 non_uniform_map_ = non_uniform_map;
408 xbt_free(non_uniform_map);
410 is_uniform_=is_uniform;
412 Coll_bcast_scatter_LR_allgather::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
414 if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
415 // we need to switch as the called function may silently touch global variables
416 smpi_switch_data_segment(s4u::Actor::self());
418 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
420 int prev=this->group()->rank(comm_intra->group()->actor(0));
421 for (i = 1; i < my_local_size; i++) {
422 int that = this->group()->rank(comm_intra->group()->actor(i));
423 if (that != prev + 1) {
431 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
433 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
435 is_blocked_ = global_blocked;
438 is_blocked_=global_blocked;
440 delete[] leader_list;
443 smpi_process()->set_replaying(true);
446 MPI_Comm Comm::f2c(int id) {
448 return MPI_COMM_SELF;
450 return MPI_COMM_WORLD;
451 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
453 const auto& lookup = F2C::f2c_lookup();
454 auto comm = lookup->find(get_key_id(key, id));
455 return comm == lookup->end() ? MPI_COMM_NULL : static_cast<MPI_Comm>(comm->second);
457 return MPI_COMM_NULL;
461 void Comm::free_f(int id) {
463 F2C::f2c_lookup()->erase(id == 0 ? get_key(key, id) : get_key_id(key, id));
467 if(F2C::f2c_lookup()==nullptr){
468 F2C::set_f2c_lookup(new std::unordered_map<std::string, F2C*>);
471 (*(F2C::f2c_lookup()))[this == MPI_COMM_WORLD ? get_key(key, F2C::f2c_id()) : get_key_id(key, F2C::f2c_id())] = this;
473 return F2C::f2c_id()-1;
476 void Comm::add_rma_win(MPI_Win win){
477 rma_wins_.push_back(win);
480 void Comm::remove_rma_win(MPI_Win win){
481 rma_wins_.remove(win);
484 void Comm::finish_rma_calls(){
485 for (auto const& it : rma_wins_) {
486 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
487 int finished = it->finish_comms();
488 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);
493 MPI_Comm Comm::split_type(int type, int key, MPI_Info info)
495 if(type != MPI_COMM_TYPE_SHARED){
496 return MPI_COMM_NULL;
500 this->get_intra_comm()->ref();
501 return this->get_intra_comm();
505 } // namespace simgrid