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"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
21 xbt_dict_t smpi_comm_keyvals = nullptr;
22 int comm_keyval_id = 0;//avoid collisions
25 Comm mpi_MPI_COMM_UNINITIALIZED;
26 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
28 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
29 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
31 static int smpi_compare_rankmap(const void *a, const void *b)
33 const int* x = static_cast<const int*>(a);
34 const int* y = static_cast<const int*>(b);
56 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
59 topoType_ = MPI_INVALID_TOPO;
60 intra_comm_ = MPI_COMM_NULL;
61 leaders_comm_ = MPI_COMM_NULL;
63 non_uniform_map_ = nullptr;
64 leaders_map_ = nullptr;
69 void Comm::destroy(Comm* comm)
71 if (comm == MPI_COMM_UNINITIALIZED){
72 Comm::destroy(smpi_process_comm_world());
75 delete comm->topo_; // there's no use count on topos
79 int Comm::dup(MPI_Comm* newcomm){
80 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
81 smpi_switch_data_segment(smpi_process_index());
83 MPI_Group cp = new Group(this->group());
84 (*newcomm) = new Comm(cp, this->topo());
85 int ret = MPI_SUCCESS;
87 if(attributes_ !=nullptr){
88 (*newcomm)->attributes_ = xbt_dict_new_homogeneous(nullptr);
89 xbt_dict_cursor_t cursor = nullptr;
94 xbt_dict_foreach (attributes_, cursor, key, value_in) {
95 smpi_comm_key_elem elem =
96 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
97 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
98 ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
99 if (ret != MPI_SUCCESS) {
100 Comm::destroy(*newcomm);
101 *newcomm = MPI_COMM_NULL;
102 xbt_dict_cursor_free(&cursor);
106 xbt_dict_set_ext((*newcomm)->attributes_, key, sizeof(int), value_out, nullptr);
113 MPI_Group Comm::group()
115 if (this == MPI_COMM_UNINITIALIZED)
116 return smpi_process_comm_world()->group();
120 MPI_Topology Comm::topo() {
126 if (this == MPI_COMM_UNINITIALIZED)
127 return smpi_process_comm_world()->size();
128 return group_->size();
133 if (this == MPI_COMM_UNINITIALIZED)
134 return smpi_process_comm_world()->rank();
135 return group_->rank(smpi_process_index());
138 void Comm::get_name (char* name, int* len)
140 if (this == MPI_COMM_UNINITIALIZED){
141 smpi_process_comm_world()->get_name(name, len);
144 if(this == MPI_COMM_WORLD) {
145 strncpy(name, "WORLD",5);
148 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
152 void Comm::set_leaders_comm(MPI_Comm leaders){
153 if (this == MPI_COMM_UNINITIALIZED){
154 smpi_process_comm_world()->set_leaders_comm(leaders);
157 leaders_comm_=leaders;
160 void Comm::set_intra_comm(MPI_Comm leaders){
164 int* Comm::get_non_uniform_map(){
165 if (this == MPI_COMM_UNINITIALIZED)
166 return smpi_process_comm_world()->get_non_uniform_map();
167 return non_uniform_map_;
170 int* Comm::get_leaders_map(){
171 if (this == MPI_COMM_UNINITIALIZED)
172 return smpi_process_comm_world()->get_leaders_map();
176 MPI_Comm Comm::get_leaders_comm(){
177 if (this == MPI_COMM_UNINITIALIZED)
178 return smpi_process_comm_world()->get_leaders_comm();
179 return leaders_comm_;
182 MPI_Comm Comm::get_intra_comm(){
183 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
184 return smpi_process_get_comm_intra();
185 else return intra_comm_;
188 int Comm::is_uniform(){
189 if (this == MPI_COMM_UNINITIALIZED)
190 return smpi_process_comm_world()->is_uniform();
194 int Comm::is_blocked(){
195 if (this == MPI_COMM_UNINITIALIZED)
196 return smpi_process_comm_world()->is_blocked();
200 MPI_Comm Comm::split(int color, int key)
202 if (this == MPI_COMM_UNINITIALIZED)
203 return smpi_process_comm_world()->split(color, key);
204 int system_tag = 123;
207 MPI_Group group_root = nullptr;
208 MPI_Group group_out = nullptr;
209 MPI_Group group = this->group();
210 int rank = this->rank();
211 int size = this->size();
212 /* Gather all colors and keys on rank 0 */
213 int* sendbuf = xbt_new(int, 2);
217 recvbuf = xbt_new(int, 2 * size);
221 Coll_gather_default::gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
223 /* Do the actual job */
225 MPI_Group* group_snd = xbt_new(MPI_Group, size);
226 int* rankmap = xbt_new(int, 2 * size);
227 for (int i = 0; i < size; i++) {
228 if (recvbuf[2 * i] != MPI_UNDEFINED) {
230 for (int j = i + 1; j < size; j++) {
231 if(recvbuf[2 * i] == recvbuf[2 * j]) {
232 recvbuf[2 * j] = MPI_UNDEFINED;
233 rankmap[2 * count] = j;
234 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
238 /* Add self in the group */
239 recvbuf[2 * i] = MPI_UNDEFINED;
240 rankmap[2 * count] = i;
241 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
243 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
244 group_out = new Group(count);
246 group_root = group_out; /* Save root's group */
248 for (int j = 0; j < count; j++) {
249 int index = group->index(rankmap[2 * j]);
250 group_out->set_mapping(index, j);
252 MPI_Request* requests = xbt_new(MPI_Request, count);
254 for (int j = 0; j < count; j++) {
255 if(rankmap[2 * j] != 0) {
256 group_snd[reqs]=new Group(group_out);
257 requests[reqs] = Request::isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
262 if(group_out != MPI_COMM_WORLD->group() && group_out != MPI_GROUP_EMPTY)
263 Group::unref(group_out);
265 Request::waitall(reqs, requests, MPI_STATUS_IGNORE);
272 group_out = group_root; /* exit with root's group */
274 if(color != MPI_UNDEFINED) {
275 Request::recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
276 } /* otherwise, exit with group_out == nullptr */
278 return group_out!=nullptr ? new Comm(group_out, nullptr) : MPI_COMM_NULL;
282 if (this == MPI_COMM_UNINITIALIZED){
283 smpi_process_comm_world()->ref();
290 void Comm::cleanup_attributes(){
291 if(attributes_ !=nullptr){
292 xbt_dict_cursor_t cursor = nullptr;
296 xbt_dict_foreach (attributes_, cursor, key, value) {
297 smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
298 if (elem != nullptr && elem->delete_fn != nullptr)
299 elem->delete_fn(this, atoi(key), value, &flag);
301 xbt_dict_free(&attributes_);
305 void Comm::cleanup_smp(){
306 if (intra_comm_ != MPI_COMM_NULL)
307 Comm::unref(intra_comm_);
308 if (leaders_comm_ != MPI_COMM_NULL)
309 Comm::unref(leaders_comm_);
310 if (non_uniform_map_ != nullptr)
311 xbt_free(non_uniform_map_);
312 if (leaders_map_ != nullptr)
313 xbt_free(leaders_map_);
316 void Comm::unref(Comm* comm){
317 if (comm == MPI_COMM_UNINITIALIZED){
318 Comm::unref(smpi_process_comm_world());
322 Group::unref(comm->group_);
324 if(comm->refcount_==0){
326 comm->cleanup_attributes();
331 static int compare_ints (const void *a, const void *b)
333 const int *da = static_cast<const int *>(a);
334 const int *db = static_cast<const int *>(b);
336 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
339 void Comm::init_smp(){
342 if (this == MPI_COMM_UNINITIALIZED)
343 smpi_process_comm_world()->init_smp();
345 int comm_size = this->size();
347 // If we are in replay - perform an ugly hack
348 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
349 bool replaying = false; //cache data to set it back again after
350 if(smpi_process_get_replaying()){
352 smpi_process_set_replaying(false);
355 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
356 smpi_switch_data_segment(smpi_process_index());
358 //identify neighbours in comm
359 //get the indexes of all processes sharing the same simix host
360 xbt_swag_t process_list = SIMIX_host_self()->extension<simgrid::simix::Host>()->process_list;
361 int intra_comm_size = 0;
362 int min_index = INT_MAX;//the minimum index will be the leader
363 smx_actor_t actor = nullptr;
364 xbt_swag_foreach(actor, process_list) {
365 int index = actor->pid -1;
367 if(this->group()->rank(index)!=MPI_UNDEFINED){
369 //the process is in the comm
370 if(index < min_index)
374 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
375 MPI_Group group_intra = new Group(intra_comm_size);
378 xbt_swag_foreach(actor, process_list) {
379 int index = actor->pid -1;
380 if(this->group()->rank(index)!=MPI_UNDEFINED){
381 group_intra->set_mapping(index, i);
386 MPI_Comm comm_intra = new Comm(group_intra, nullptr);
389 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
390 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
391 for(i=0; i<comm_size; i++){
395 Coll_allgather_mpich::allgather(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
397 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
398 smpi_switch_data_segment(smpi_process_index());
401 if(leaders_map_==nullptr){
402 leaders_map_= leaders_map;
404 xbt_free(leaders_map);
407 int leader_group_size = 0;
408 for(i=0; i<comm_size; i++){
410 for(j=0;j<leader_group_size; j++){
411 if(leaders_map_[i]==leader_list[j]){
416 leader_list[leader_group_size]=leaders_map_[i];
420 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
422 MPI_Group leaders_group = new Group(leader_group_size);
424 MPI_Comm leader_comm = MPI_COMM_NULL;
425 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
426 //create leader_communicator
427 for (i=0; i< leader_group_size;i++)
428 leaders_group->set_mapping(leader_list[i], i);
429 leader_comm = new Comm(leaders_group, nullptr);
430 this->set_leaders_comm(leader_comm);
431 this->set_intra_comm(comm_intra);
433 //create intracommunicator
435 for (i=0; i< leader_group_size;i++)
436 leaders_group->set_mapping(leader_list[i], i);
438 if(this->get_leaders_comm()==MPI_COMM_NULL){
439 leader_comm = new Comm(leaders_group, nullptr);
440 this->set_leaders_comm(leader_comm);
442 leader_comm=this->get_leaders_comm();
443 Group::unref(leaders_group);
445 smpi_process_set_comm_intra(comm_intra);
450 // Are the nodes uniform ? = same number of process/node
451 int my_local_size=comm_intra->size();
452 if(comm_intra->rank()==0) {
453 int* non_uniform_map = xbt_new0(int,leader_group_size);
454 Coll_allgather_mpich::allgather(&my_local_size, 1, MPI_INT,
455 non_uniform_map, 1, MPI_INT, leader_comm);
456 for(i=0; i < leader_group_size; i++) {
457 if(non_uniform_map[0] != non_uniform_map[i]) {
462 if(is_uniform==0 && this->is_uniform()!=0){
463 non_uniform_map_= non_uniform_map;
465 xbt_free(non_uniform_map);
467 is_uniform_=is_uniform;
469 Coll_bcast_mpich::bcast(&(is_uniform_),1, MPI_INT, 0, comm_intra );
471 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
472 smpi_switch_data_segment(smpi_process_index());
474 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
476 int prev=this->group()->rank(comm_intra->group()->index(0));
477 for (i=1; i<my_local_size; i++){
478 int that=this->group()->rank(comm_intra->group()->index(i));
487 Coll_allreduce_default::allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
489 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
491 is_blocked_=global_blocked;
494 is_blocked_=global_blocked;
496 xbt_free(leader_list);
499 smpi_process_set_replaying(true);
502 int Comm::attr_delete(int keyval){
503 smpi_comm_key_elem elem =
504 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
507 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
508 void* value = nullptr;
510 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
511 int ret = elem->delete_fn(this, keyval, value, &flag);
516 if(attributes_==nullptr)
519 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
523 int Comm::attr_get(int keyval, void* attr_value, int* flag){
524 smpi_comm_key_elem elem =
525 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
528 if(attributes_==nullptr){
533 *static_cast<void**>(attr_value) =
534 xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
543 int Comm::attr_put(int keyval, void* attr_value){
544 if(smpi_comm_keyvals==nullptr)
545 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
546 smpi_comm_key_elem elem =
547 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
551 void* value = nullptr;
552 this->attr_get(keyval, &value, &flag);
553 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
554 int ret = elem->delete_fn(this, keyval, value, &flag);
558 if(attributes_==nullptr)
559 attributes_ = xbt_dict_new_homogeneous(nullptr);
561 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
565 MPI_Comm Comm::f2c(int id) {
567 return MPI_COMM_SELF;
569 return MPI_COMM_WORLD;
570 } else if(Comm::f2c_lookup_ != nullptr && id >= 0) {
572 MPI_Comm tmp = static_cast<MPI_Comm>(xbt_dict_get_or_null(Comm::f2c_lookup_,get_key_id(key, id)));
573 return tmp != nullptr ? tmp : MPI_COMM_NULL ;
575 return MPI_COMM_NULL;
579 void Comm::free_f(int id) {
581 xbt_dict_remove(Comm::f2c_lookup_, id==0? get_key(key, id) : get_key_id(key, id));
585 if(Comm::f2c_lookup_==nullptr){
586 Comm::f2c_lookup_=xbt_dict_new_homogeneous(nullptr);
589 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);
591 return Comm::f2c_id_-1;
598 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
600 if(smpi_comm_keyvals==nullptr)
601 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
603 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
605 value->copy_fn=copy_fn;
606 value->delete_fn=delete_fn;
608 *keyval = comm_keyval_id;
609 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
614 int smpi_comm_keyval_free(int* keyval){
615 smpi_comm_key_elem elem =
616 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
619 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));