1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
14 #include <simgrid/s4u/host.hpp>
17 #include "src/simix/smx_private.h"
18 #include "colls/colls.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
22 xbt_dict_t smpi_comm_keyvals = nullptr;
23 int comm_keyval_id = 0;//avoid collisions
26 Comm mpi_MPI_COMM_UNINITIALIZED;
27 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
29 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
30 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
32 static int smpi_compare_rankmap(const void *a, const void *b)
34 const int* x = static_cast<const int*>(a);
35 const int* y = static_cast<const int*>(b);
57 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
60 topoType_ = MPI_INVALID_TOPO;
61 intra_comm_ = MPI_COMM_NULL;
62 leaders_comm_ = MPI_COMM_NULL;
64 non_uniform_map_ = nullptr;
65 leaders_map_ = nullptr;
70 void Comm::destroy(Comm* comm)
72 if (comm == MPI_COMM_UNINITIALIZED){
73 Comm::destroy(smpi_process_comm_world());
76 delete comm->topo_; // there's no use count on topos
80 int Comm::dup(MPI_Comm* newcomm){
81 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
82 smpi_switch_data_segment(smpi_process_index());
84 MPI_Group cp = new Group(this->group());
85 (*newcomm) = new Comm(cp, this->topo());
86 int ret = MPI_SUCCESS;
88 if(attributes_ !=nullptr){
89 (*newcomm)->attributes_ = xbt_dict_new_homogeneous(nullptr);
90 xbt_dict_cursor_t cursor = nullptr;
95 xbt_dict_foreach (attributes_, cursor, key, value_in) {
96 smpi_comm_key_elem elem =
97 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
98 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
99 ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
100 if (ret != MPI_SUCCESS) {
101 Comm::destroy(*newcomm);
102 *newcomm = MPI_COMM_NULL;
103 xbt_dict_cursor_free(&cursor);
107 xbt_dict_set_ext((*newcomm)->attributes_, key, sizeof(int), value_out, nullptr);
114 MPI_Group Comm::group()
116 if (this == MPI_COMM_UNINITIALIZED)
117 return smpi_process_comm_world()->group();
121 MPI_Topology Comm::topo() {
127 if (this == MPI_COMM_UNINITIALIZED)
128 return smpi_process_comm_world()->size();
129 return group_->size();
134 if (this == MPI_COMM_UNINITIALIZED)
135 return smpi_process_comm_world()->rank();
136 return group_->rank(smpi_process_index());
139 void Comm::get_name (char* name, int* len)
141 if (this == MPI_COMM_UNINITIALIZED){
142 smpi_process_comm_world()->get_name(name, len);
145 if(this == MPI_COMM_WORLD) {
146 strncpy(name, "WORLD",5);
149 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
153 void Comm::set_leaders_comm(MPI_Comm leaders){
154 if (this == MPI_COMM_UNINITIALIZED){
155 smpi_process_comm_world()->set_leaders_comm(leaders);
158 leaders_comm_=leaders;
161 void Comm::set_intra_comm(MPI_Comm leaders){
165 int* Comm::get_non_uniform_map(){
166 if (this == MPI_COMM_UNINITIALIZED)
167 return smpi_process_comm_world()->get_non_uniform_map();
168 return non_uniform_map_;
171 int* Comm::get_leaders_map(){
172 if (this == MPI_COMM_UNINITIALIZED)
173 return smpi_process_comm_world()->get_leaders_map();
177 MPI_Comm Comm::get_leaders_comm(){
178 if (this == MPI_COMM_UNINITIALIZED)
179 return smpi_process_comm_world()->get_leaders_comm();
180 return leaders_comm_;
183 MPI_Comm Comm::get_intra_comm(){
184 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
185 return smpi_process_get_comm_intra();
186 else return intra_comm_;
189 int Comm::is_uniform(){
190 if (this == MPI_COMM_UNINITIALIZED)
191 return smpi_process_comm_world()->is_uniform();
195 int Comm::is_blocked(){
196 if (this == MPI_COMM_UNINITIALIZED)
197 return smpi_process_comm_world()->is_blocked();
201 MPI_Comm Comm::split(int color, int key)
203 if (this == MPI_COMM_UNINITIALIZED)
204 return smpi_process_comm_world()->split(color, key);
205 int system_tag = 123;
208 MPI_Group group_root = nullptr;
209 MPI_Group group_out = nullptr;
210 MPI_Group group = this->group();
211 int rank = this->rank();
212 int size = this->size();
213 /* Gather all colors and keys on rank 0 */
214 int* sendbuf = xbt_new(int, 2);
218 recvbuf = xbt_new(int, 2 * size);
222 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
224 /* Do the actual job */
226 MPI_Group* group_snd = xbt_new(MPI_Group, size);
227 int* rankmap = xbt_new(int, 2 * size);
228 for (int i = 0; i < size; i++) {
229 if (recvbuf[2 * i] != MPI_UNDEFINED) {
231 for (int j = i + 1; j < size; j++) {
232 if(recvbuf[2 * i] == recvbuf[2 * j]) {
233 recvbuf[2 * j] = MPI_UNDEFINED;
234 rankmap[2 * count] = j;
235 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
239 /* Add self in the group */
240 recvbuf[2 * i] = MPI_UNDEFINED;
241 rankmap[2 * count] = i;
242 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
244 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
245 group_out = new Group(count);
247 group_root = group_out; /* Save root's group */
249 for (int j = 0; j < count; j++) {
250 int index = group->index(rankmap[2 * j]);
251 group_out->set_mapping(index, j);
253 MPI_Request* requests = xbt_new(MPI_Request, count);
255 for (int j = 0; j < count; j++) {
256 if(rankmap[2 * j] != 0) {
257 group_snd[reqs]=new Group(group_out);
258 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
263 if(group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
264 Group::unref(group_out);
266 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
273 group_out = group_root; /* exit with root's group */
275 if(color != MPI_UNDEFINED) {
276 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
277 } /* otherwise, exit with group_out == nullptr */
279 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
283 if (this == MPI_COMM_UNINITIALIZED){
284 smpi_process_comm_world()->ref();
291 void Comm::cleanup_attributes(){
292 if(attributes_ !=nullptr){
293 xbt_dict_cursor_t cursor = nullptr;
297 xbt_dict_foreach (attributes_, cursor, key, value) {
298 smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
299 if (elem != nullptr && elem->delete_fn != nullptr)
300 elem->delete_fn(this, atoi(key), value, &flag);
302 xbt_dict_free(&attributes_);
306 void Comm::cleanup_smp(){
307 if (intra_comm_ != MPI_COMM_NULL)
308 Comm::unref(intra_comm_);
309 if (leaders_comm_ != MPI_COMM_NULL)
310 Comm::unref(leaders_comm_);
311 if (non_uniform_map_ != nullptr)
312 xbt_free(non_uniform_map_);
313 if (leaders_map_ != nullptr)
314 xbt_free(leaders_map_);
317 void Comm::unref(Comm* comm){
318 if (comm == MPI_COMM_UNINITIALIZED){
319 Comm::unref(smpi_process_comm_world());
323 Group::unref(comm->group_);
325 if(comm->refcount_==0){
327 comm->cleanup_attributes();
332 static int compare_ints (const void *a, const void *b)
334 const int *da = static_cast<const int *>(a);
335 const int *db = static_cast<const int *>(b);
337 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
340 void Comm::init_smp(){
343 if (this == MPI_COMM_UNINITIALIZED)
344 smpi_process_comm_world()->init_smp();
346 int comm_size = this->size();
348 // If we are in replay - perform an ugly hack
349 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
350 bool replaying = false; //cache data to set it back again after
351 if(smpi_process_get_replaying()){
353 smpi_process_set_replaying(false);
356 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
357 smpi_switch_data_segment(smpi_process_index());
359 //identify neighbours in comm
360 //get the indexes of all processes sharing the same simix host
361 xbt_swag_t process_list = SIMIX_host_self()->extension<simgrid::simix::Host>()->process_list;
362 int intra_comm_size = 0;
363 int min_index = INT_MAX;//the minimum index will be the leader
364 smx_actor_t actor = nullptr;
365 xbt_swag_foreach(actor, process_list) {
366 int index = actor->pid -1;
368 if(this->group()->rank(index)!=MPI_UNDEFINED){
370 //the process is in the comm
371 if(index < min_index)
375 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
376 MPI_Group group_intra = new Group(intra_comm_size);
379 xbt_swag_foreach(actor, process_list) {
380 int index = actor->pid -1;
381 if(this->group()->rank(index)!=MPI_UNDEFINED){
382 group_intra->set_mapping(index, i);
387 MPI_Comm comm_intra = new Comm(group_intra, nullptr);
390 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
391 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
392 for(i=0; i<comm_size; i++){
396 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
398 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
399 smpi_switch_data_segment(smpi_process_index());
402 if(leaders_map_==nullptr){
403 leaders_map_= leaders_map;
405 xbt_free(leaders_map);
408 int leader_group_size = 0;
409 for(i=0; i<comm_size; i++){
411 for(j=0;j<leader_group_size; j++){
412 if(leaders_map_[i]==leader_list[j]){
417 leader_list[leader_group_size]=leaders_map_[i];
421 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
423 MPI_Group leaders_group = new Group(leader_group_size);
425 MPI_Comm leader_comm = MPI_COMM_NULL;
426 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
427 //create leader_communicator
428 for (i=0; i< leader_group_size;i++)
429 leaders_group->set_mapping(leader_list[i], i);
430 leader_comm = new Comm(leaders_group, nullptr);
431 this->set_leaders_comm(leader_comm);
432 this->set_intra_comm(comm_intra);
434 //create intracommunicator
436 for (i=0; i< leader_group_size;i++)
437 leaders_group->set_mapping(leader_list[i], i);
439 if(this->get_leaders_comm()==MPI_COMM_NULL){
440 leader_comm = new Comm(leaders_group, nullptr);
441 this->set_leaders_comm(leader_comm);
443 leader_comm=this->get_leaders_comm();
444 Group::unref(leaders_group);
446 smpi_process_set_comm_intra(comm_intra);
451 // Are the nodes uniform ? = same number of process/node
452 int my_local_size=comm_intra->size();
453 if(comm_intra->rank()==0) {
454 int* non_uniform_map = xbt_new0(int,leader_group_size);
455 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
456 non_uniform_map, 1, MPI_INT, leader_comm);
457 for(i=0; i < leader_group_size; i++) {
458 if(non_uniform_map[0] != non_uniform_map[i]) {
463 if(is_uniform==0 && this->is_uniform()!=0){
464 non_uniform_map_= non_uniform_map;
466 xbt_free(non_uniform_map);
468 is_uniform_=is_uniform;
470 smpi_coll_tuned_bcast_mpich(&(is_uniform_),1, MPI_INT, 0, comm_intra );
472 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
473 smpi_switch_data_segment(smpi_process_index());
475 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
477 int prev=this->group()->rank(comm_intra->group()->index(0));
478 for (i=1; i<my_local_size; i++){
479 int that=this->group()->rank(comm_intra->group()->index(i));
488 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
490 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
492 is_blocked_=global_blocked;
495 is_blocked_=global_blocked;
497 xbt_free(leader_list);
500 smpi_process_set_replaying(true);
503 int Comm::attr_delete(int keyval){
504 smpi_comm_key_elem elem =
505 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
508 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
509 void* value = nullptr;
511 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
512 int ret = elem->delete_fn(this, keyval, value, &flag);
517 if(attributes_==nullptr)
520 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
524 int Comm::attr_get(int keyval, void* attr_value, int* flag){
525 smpi_comm_key_elem elem =
526 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
529 if(attributes_==nullptr){
534 *static_cast<void**>(attr_value) =
535 xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
544 int Comm::attr_put(int keyval, void* attr_value){
545 if(smpi_comm_keyvals==nullptr)
546 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
547 smpi_comm_key_elem elem =
548 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
552 void* value = nullptr;
553 this->attr_get(keyval, &value, &flag);
554 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
555 int ret = elem->delete_fn(this, keyval, value, &flag);
559 if(attributes_==nullptr)
560 attributes_ = xbt_dict_new_homogeneous(nullptr);
562 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
566 MPI_Comm Comm::f2c(int id) {
568 return MPI_COMM_SELF;
570 return MPI_COMM_WORLD;
571 } else if(Comm::f2c_lookup_ != nullptr && id >= 0) {
573 MPI_Comm tmp = static_cast<MPI_Comm>(xbt_dict_get_or_null(Comm::f2c_lookup_,get_key_id(key, id)));
574 return tmp != nullptr ? tmp : MPI_COMM_NULL ;
576 return MPI_COMM_NULL;
580 void Comm::free_f(int id) {
582 xbt_dict_remove(Comm::f2c_lookup_, id==0? get_key(key, id) : get_key_id(key, id));
586 if(Comm::f2c_lookup_==nullptr){
587 Comm::f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
590 xbt_dict_set(Comm::f2c_lookup_, this==MPI_COMM_WORLD? get_key(key, Comm::f2c_id_) : get_key_id(key,Comm::f2c_id_), this, nullptr);
592 return Comm::f2c_id_-1;
599 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
601 if(smpi_comm_keyvals==nullptr)
602 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
604 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
606 value->copy_fn=copy_fn;
607 value->delete_fn=delete_fn;
609 *keyval = comm_keyval_id;
610 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
615 int smpi_comm_keyval_free(int* keyval){
616 smpi_comm_key_elem elem =
617 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
620 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));