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 "simgrid/s4u/Host.hpp"
8 #include "src/simix/smx_private.h"
9 #include "src/smpi/smpi_comm.hpp"
10 #include "src/smpi/smpi_coll.hpp"
11 #include "src/smpi/smpi_datatype.hpp"
12 #include "src/smpi/smpi_process.hpp"
13 #include "src/smpi/smpi_request.hpp"
14 #include "src/smpi/smpi_status.hpp"
15 #include "src/smpi/smpi_win.hpp"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
19 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
20 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
22 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
23 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
25 static int smpi_compare_rankmap(const void *a, const void *b)
27 const int* x = static_cast<const int*>(a);
28 const int* y = static_cast<const int*>(b);
48 std::unordered_map<int, smpi_key_elem> Comm::keyvals_;
49 int Comm::keyval_id_=0;
51 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
54 topoType_ = MPI_INVALID_TOPO;
55 intra_comm_ = MPI_COMM_NULL;
56 leaders_comm_ = MPI_COMM_NULL;
58 non_uniform_map_ = nullptr;
59 leaders_map_ = nullptr;
63 void Comm::destroy(Comm* comm)
65 if (comm == MPI_COMM_UNINITIALIZED){
66 Comm::destroy(smpi_process()->comm_world());
69 delete comm->topo_; // there's no use count on topos
73 int Comm::dup(MPI_Comm* newcomm){
74 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
75 smpi_switch_data_segment(smpi_process()->index());
77 MPI_Group cp = new Group(this->group());
78 (*newcomm) = new Comm(cp, this->topo());
79 int ret = MPI_SUCCESS;
81 if(!attributes()->empty()){
84 for(auto it : *attributes()){
85 smpi_key_elem elem = keyvals_.at(it.first);
86 if (elem != nullptr && elem->copy_fn.comm_copy_fn != MPI_NULL_COPY_FN) {
87 ret = elem->copy_fn.comm_copy_fn(this, it.first, nullptr, it.second, &value_out, &flag);
88 if (ret != MPI_SUCCESS) {
89 Comm::destroy(*newcomm);
90 *newcomm = MPI_COMM_NULL;
95 (*newcomm)->attributes()->insert({it.first, value_out});
103 MPI_Group Comm::group()
105 if (this == MPI_COMM_UNINITIALIZED)
106 return smpi_process()->comm_world()->group();
110 MPI_Topology Comm::topo() {
116 if (this == MPI_COMM_UNINITIALIZED)
117 return smpi_process()->comm_world()->size();
118 return group_->size();
123 if (this == MPI_COMM_UNINITIALIZED)
124 return smpi_process()->comm_world()->rank();
125 return group_->rank(smpi_process()->index());
128 void Comm::get_name (char* name, int* len)
130 if (this == MPI_COMM_UNINITIALIZED){
131 smpi_process()->comm_world()->get_name(name, len);
134 if(this == MPI_COMM_WORLD) {
135 strncpy(name, "WORLD",5);
138 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
142 void Comm::set_leaders_comm(MPI_Comm leaders){
143 if (this == MPI_COMM_UNINITIALIZED){
144 smpi_process()->comm_world()->set_leaders_comm(leaders);
147 leaders_comm_=leaders;
150 void Comm::set_intra_comm(MPI_Comm leaders){
154 int* Comm::get_non_uniform_map(){
155 if (this == MPI_COMM_UNINITIALIZED)
156 return smpi_process()->comm_world()->get_non_uniform_map();
157 return non_uniform_map_;
160 int* Comm::get_leaders_map(){
161 if (this == MPI_COMM_UNINITIALIZED)
162 return smpi_process()->comm_world()->get_leaders_map();
166 MPI_Comm Comm::get_leaders_comm(){
167 if (this == MPI_COMM_UNINITIALIZED)
168 return smpi_process()->comm_world()->get_leaders_comm();
169 return leaders_comm_;
172 MPI_Comm Comm::get_intra_comm(){
173 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
174 return smpi_process()->comm_intra();
175 else return intra_comm_;
178 int Comm::is_uniform(){
179 if (this == MPI_COMM_UNINITIALIZED)
180 return smpi_process()->comm_world()->is_uniform();
184 int Comm::is_blocked(){
185 if (this == MPI_COMM_UNINITIALIZED)
186 return smpi_process()->comm_world()->is_blocked();
190 MPI_Comm Comm::split(int color, int key)
192 if (this == MPI_COMM_UNINITIALIZED)
193 return smpi_process()->comm_world()->split(color, key);
194 int system_tag = 123;
197 MPI_Group group_root = nullptr;
198 MPI_Group group_out = nullptr;
199 MPI_Group group = this->group();
200 int rank = this->rank();
201 int size = this->size();
202 /* Gather all colors and keys on rank 0 */
203 int* sendbuf = xbt_new(int, 2);
207 recvbuf = xbt_new(int, 2 * size);
211 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
213 /* Do the actual job */
215 MPI_Group* group_snd = xbt_new(MPI_Group, size);
216 int* rankmap = xbt_new(int, 2 * 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[2 * count] = j;
224 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
228 /* Add self in the group */
229 recvbuf[2 * i] = MPI_UNDEFINED;
230 rankmap[2 * count] = i;
231 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
233 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
234 group_out = new Group(count);
236 group_root = group_out; /* Save root's group */
238 for (int j = 0; j < count; j++) {
239 int index = group->index(rankmap[2 * j]);
240 group_out->set_mapping(index, j);
242 MPI_Request* requests = xbt_new(MPI_Request, count);
244 for (int j = 0; j < count; j++) {
245 if(rankmap[2 * j] != 0) {
246 group_snd[reqs]=new Group(group_out);
247 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
251 if(i != 0 && group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
252 Group::unref(group_out);
254 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
261 group_out = group_root; /* exit with root's group */
263 if(color != MPI_UNDEFINED) {
264 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
265 } /* otherwise, exit with group_out == nullptr */
267 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
271 if (this == MPI_COMM_UNINITIALIZED){
272 smpi_process()->comm_world()->ref();
279 void Comm::cleanup_smp(){
280 if (intra_comm_ != MPI_COMM_NULL)
281 Comm::unref(intra_comm_);
282 if (leaders_comm_ != MPI_COMM_NULL)
283 Comm::unref(leaders_comm_);
284 if (non_uniform_map_ != nullptr)
285 xbt_free(non_uniform_map_);
286 if (leaders_map_ != nullptr)
287 xbt_free(leaders_map_);
290 void Comm::unref(Comm* comm){
291 if (comm == MPI_COMM_UNINITIALIZED){
292 Comm::unref(smpi_process()->comm_world());
296 Group::unref(comm->group_);
298 if(comm->refcount_==0){
300 comm->cleanup_attr<Comm>();
305 static int compare_ints (const void *a, const void *b)
307 const int *da = static_cast<const int *>(a);
308 const int *db = static_cast<const int *>(b);
310 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
313 void Comm::init_smp(){
316 if (this == MPI_COMM_UNINITIALIZED)
317 smpi_process()->comm_world()->init_smp();
319 int comm_size = this->size();
321 // If we are in replay - perform an ugly hack
322 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
323 bool replaying = false; //cache data to set it back again after
324 if(smpi_process()->replaying()){
326 smpi_process()->set_replaying(false);
329 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
330 smpi_switch_data_segment(smpi_process()->index());
332 //identify neighbours in comm
333 //get the indexes of all processes sharing the same simix host
334 xbt_swag_t process_list = SIMIX_host_self()->extension<simgrid::simix::Host>()->process_list;
335 int intra_comm_size = 0;
336 int min_index = INT_MAX;//the minimum index will be the leader
337 smx_actor_t actor = nullptr;
338 xbt_swag_foreach(actor, process_list) {
339 int index = actor->pid -1;
341 if(this->group()->rank(index)!=MPI_UNDEFINED){
343 //the process is in the comm
344 if(index < min_index)
348 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
349 MPI_Group group_intra = new Group(intra_comm_size);
352 xbt_swag_foreach(actor, process_list) {
353 int index = actor->pid -1;
354 if(this->group()->rank(index)!=MPI_UNDEFINED){
355 group_intra->set_mapping(index, i);
360 MPI_Comm comm_intra = new Comm(group_intra, nullptr);
363 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
364 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
365 for(i=0; i<comm_size; i++){
369 Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
371 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
372 smpi_switch_data_segment(smpi_process()->index());
375 if(leaders_map_==nullptr){
376 leaders_map_= leaders_map;
378 xbt_free(leaders_map);
381 int leader_group_size = 0;
382 for(i=0; i<comm_size; i++){
384 for(j=0;j<leader_group_size; j++){
385 if(leaders_map_[i]==leader_list[j]){
390 leader_list[leader_group_size]=leaders_map_[i];
394 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
396 MPI_Group leaders_group = new Group(leader_group_size);
398 MPI_Comm leader_comm = MPI_COMM_NULL;
399 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
400 //create leader_communicator
401 for (i=0; i< leader_group_size;i++)
402 leaders_group->set_mapping(leader_list[i], i);
403 leader_comm = new Comm(leaders_group, nullptr);
404 this->set_leaders_comm(leader_comm);
405 this->set_intra_comm(comm_intra);
407 //create intracommunicator
409 for (i=0; i< leader_group_size;i++)
410 leaders_group->set_mapping(leader_list[i], i);
412 if(this->get_leaders_comm()==MPI_COMM_NULL){
413 leader_comm = new Comm(leaders_group, nullptr);
414 this->set_leaders_comm(leader_comm);
416 leader_comm=this->get_leaders_comm();
417 Group::unref(leaders_group);
419 smpi_process()->set_comm_intra(comm_intra);
424 // Are the nodes uniform ? = same number of process/node
425 int my_local_size=comm_intra->size();
426 if(comm_intra->rank()==0) {
427 int* non_uniform_map = xbt_new0(int,leader_group_size);
428 Coll_allgather_mpich::allgather(&my_local_size, 1, MPI_INT,
429 non_uniform_map, 1, MPI_INT, leader_comm);
430 for(i=0; i < leader_group_size; i++) {
431 if(non_uniform_map[0] != non_uniform_map[i]) {
436 if(is_uniform==0 && this->is_uniform()!=0){
437 non_uniform_map_= non_uniform_map;
439 xbt_free(non_uniform_map);
441 is_uniform_=is_uniform;
443 Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
445 if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){ //we need to switch as the called function may silently touch global variables
446 smpi_switch_data_segment(smpi_process()->index());
448 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
450 int prev=this->group()->rank(comm_intra->group()->index(0));
451 for (i=1; i<my_local_size; i++){
452 int that=this->group()->rank(comm_intra->group()->index(i));
461 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
463 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
465 is_blocked_=global_blocked;
468 is_blocked_=global_blocked;
470 xbt_free(leader_list);
473 smpi_process()->set_replaying(true);
476 MPI_Comm Comm::f2c(int id) {
478 return MPI_COMM_SELF;
480 return MPI_COMM_WORLD;
481 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
483 MPI_Comm tmp = static_cast<MPI_Comm>(xbt_dict_get_or_null(F2C::f2c_lookup(),get_key_id(key, id)));
484 return tmp != nullptr ? tmp : MPI_COMM_NULL ;
486 return MPI_COMM_NULL;
490 void Comm::free_f(int id) {
492 xbt_dict_remove(F2C::f2c_lookup(), id==0? get_key(key, id) : get_key_id(key, id));
496 if(F2C::f2c_lookup()==nullptr){
497 F2C::set_f2c_lookup(xbt_dict_new_homogeneous(nullptr));
500 xbt_dict_set(F2C::f2c_lookup(), this==MPI_COMM_WORLD? get_key(key, F2C::f2c_id()) : get_key_id(key,F2C::f2c_id()), this, nullptr);
502 return F2C::f2c_id()-1;
506 void Comm::add_rma_win(MPI_Win win){
507 rma_wins_.push_back(win);
510 void Comm::remove_rma_win(MPI_Win win){
511 rma_wins_.remove(win);
514 void Comm::finish_rma_calls(){
515 for(auto it : rma_wins_){
516 if(it->rank()==this->rank()){//is it ours (for MPI_COMM_WORLD)?
517 int finished = it->finish_comms();
518 XBT_DEBUG("Barrier for rank %d - Finished %d RMA calls",this->rank(), finished);