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 "smpi_mpi_dt_private.h"
18 #include "src/simix/smx_private.h"
19 #include "colls/colls.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
23 xbt_dict_t smpi_comm_keyvals = nullptr;
24 int comm_keyval_id = 0;//avoid collisions
27 simgrid::smpi::Comm mpi_MPI_COMM_UNINITIALIZED;
28 MPI_Comm MPI_COMM_UNINITIALIZED=&mpi_MPI_COMM_UNINITIALIZED;
30 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
31 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
33 static int smpi_compare_rankmap(const void *a, const void *b)
35 const int* x = static_cast<const int*>(a);
36 const int* y = static_cast<const int*>(b);
58 Comm::Comm(MPI_Group group, MPI_Topology topo) : group_(group), topo_(topo)
61 topoType_ = MPI_INVALID_TOPO;
62 intra_comm_ = MPI_COMM_NULL;
63 leaders_comm_ = MPI_COMM_NULL;
65 non_uniform_map_ = nullptr;
66 leaders_map_ = nullptr;
73 if (this == MPI_COMM_UNINITIALIZED){
74 smpi_process_comm_world()->destroy();
77 delete topo_; // there's no use count on topos
81 int Comm::dup(MPI_Comm* newcomm){
82 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
83 smpi_switch_data_segment(smpi_process_index());
85 MPI_Group cp = new simgrid::smpi::Group(this->group());
86 (*newcomm) = new simgrid::smpi::Comm(cp, this->topo());
87 int ret = MPI_SUCCESS;
89 if(attributes_ !=nullptr){
90 (*newcomm)->attributes_ = xbt_dict_new_homogeneous(nullptr);
91 xbt_dict_cursor_t cursor = nullptr;
96 xbt_dict_foreach (attributes_, cursor, key, value_in) {
97 smpi_comm_key_elem elem =
98 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
99 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
100 ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
101 if (ret != MPI_SUCCESS) {
102 (*newcomm)->destroy();
103 *newcomm = MPI_COMM_NULL;
104 xbt_dict_cursor_free(&cursor);
108 xbt_dict_set_ext((*newcomm)->attributes_, key, sizeof(int), value_out, nullptr);
115 MPI_Group Comm::group()
117 if (this == MPI_COMM_UNINITIALIZED)
118 return smpi_process_comm_world()->group();
122 MPI_Topology Comm::topo() {
128 if (this == MPI_COMM_UNINITIALIZED)
129 return smpi_process_comm_world()->size();
130 return group_->size();
135 if (this == MPI_COMM_UNINITIALIZED)
136 return smpi_process_comm_world()->rank();
137 return group_->rank(smpi_process_index());
140 void Comm::get_name (char* name, int* len)
142 if (this == MPI_COMM_UNINITIALIZED){
143 smpi_process_comm_world()->get_name(name, len);
146 if(this == MPI_COMM_WORLD) {
147 strncpy(name, "WORLD",5);
150 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
154 void Comm::set_leaders_comm(MPI_Comm leaders){
155 if (this == MPI_COMM_UNINITIALIZED){
156 smpi_process_comm_world()->set_leaders_comm(leaders);
159 leaders_comm_=leaders;
162 void Comm::set_intra_comm(MPI_Comm leaders){
166 int* Comm::get_non_uniform_map(){
167 if (this == MPI_COMM_UNINITIALIZED)
168 return smpi_process_comm_world()->get_non_uniform_map();
169 return non_uniform_map_;
172 int* Comm::get_leaders_map(){
173 if (this == MPI_COMM_UNINITIALIZED)
174 return smpi_process_comm_world()->get_leaders_map();
178 MPI_Comm Comm::get_leaders_comm(){
179 if (this == MPI_COMM_UNINITIALIZED)
180 return smpi_process_comm_world()->get_leaders_comm();
181 return leaders_comm_;
184 MPI_Comm Comm::get_intra_comm(){
185 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
186 return smpi_process_get_comm_intra();
187 else return intra_comm_;
190 int Comm::is_uniform(){
191 if (this == MPI_COMM_UNINITIALIZED)
192 return smpi_process_comm_world()->is_uniform();
196 int Comm::is_blocked(){
197 if (this == MPI_COMM_UNINITIALIZED)
198 return smpi_process_comm_world()->is_blocked();
202 MPI_Comm Comm::split(int color, int key)
204 if (this == MPI_COMM_UNINITIALIZED)
205 return smpi_process_comm_world()->split(color, key);
206 int system_tag = 123;
209 MPI_Group group_root = nullptr;
210 MPI_Group group_out = nullptr;
211 MPI_Group group = this->group();
212 int rank = this->rank();
213 int size = this->size();
214 /* Gather all colors and keys on rank 0 */
215 int* sendbuf = xbt_new(int, 2);
219 recvbuf = xbt_new(int, 2 * size);
223 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
225 /* Do the actual job */
227 MPI_Group* group_snd = xbt_new(MPI_Group, size);
228 int* rankmap = xbt_new(int, 2 * size);
229 for (int i = 0; i < size; i++) {
230 if (recvbuf[2 * i] != MPI_UNDEFINED) {
232 for (int j = i + 1; j < size; j++) {
233 if(recvbuf[2 * i] == recvbuf[2 * j]) {
234 recvbuf[2 * j] = MPI_UNDEFINED;
235 rankmap[2 * count] = j;
236 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
240 /* Add self in the group */
241 recvbuf[2 * i] = MPI_UNDEFINED;
242 rankmap[2 * count] = i;
243 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
245 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
246 group_out = new simgrid::smpi::Group(count);
248 group_root = group_out; /* Save root's group */
250 for (int j = 0; j < count; j++) {
251 int index = group->index(rankmap[2 * j]);
252 group_out->set_mapping(index, j);
254 MPI_Request* requests = xbt_new(MPI_Request, count);
256 for (int j = 0; j < count; j++) {
257 if(rankmap[2 * j] != 0) {
258 group_snd[reqs]=new simgrid::smpi::Group(group_out);
259 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
264 group_out->destroy();
266 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
273 group_out = group_root; /* exit with root's group */
275 if(color != MPI_UNDEFINED) {
276 smpi_mpi_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 simgrid::smpi::Comm(group_out, nullptr) : MPI_COMM_NULL;
283 if (this == MPI_COMM_UNINITIALIZED){
284 smpi_process_comm_world()->use();
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 intra_comm_->unuse();
309 if (leaders_comm_ != MPI_COMM_NULL)
310 leaders_comm_->unuse();
311 if (non_uniform_map_ != nullptr)
312 xbt_free(non_uniform_map_);
313 if (leaders_map_ != nullptr)
314 xbt_free(leaders_map_);
318 if (this == MPI_COMM_UNINITIALIZED){
319 smpi_process_comm_world()->unuse();
327 this->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()->processes();
362 int intra_comm_size = 0;
364 int min_index=INT_MAX;//the minimum index will be the leader
365 smx_actor_t process = nullptr;
366 xbt_swag_foreach(process, process_list) {
367 int index = process->pid -1;
369 if(this->group()->rank(index)!=MPI_UNDEFINED){
371 //the process is in the comm
372 if(index < min_index)
377 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
378 MPI_Group group_intra = new simgrid::smpi::Group(intra_comm_size);
381 xbt_swag_foreach(process, process_list) {
382 int index = process->pid -1;
383 if(this->group()->rank(index)!=MPI_UNDEFINED){
384 group_intra->set_mapping(index, i);
389 MPI_Comm comm_intra = new simgrid::smpi::Comm(group_intra, nullptr);
392 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
393 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
394 for(i=0; i<comm_size; i++){
398 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
400 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
401 smpi_switch_data_segment(smpi_process_index());
404 if(leaders_map_==nullptr){
405 leaders_map_= leaders_map;
407 xbt_free(leaders_map);
410 int leader_group_size = 0;
411 for(i=0; i<comm_size; i++){
413 for(j=0;j<leader_group_size; j++){
414 if(leaders_map_[i]==leader_list[j]){
419 leader_list[leader_group_size]=leaders_map_[i];
423 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
425 MPI_Group leaders_group = new simgrid::smpi::Group(leader_group_size);
427 MPI_Comm leader_comm = MPI_COMM_NULL;
428 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
429 //create leader_communicator
430 for (i=0; i< leader_group_size;i++)
431 leaders_group->set_mapping(leader_list[i], i);
432 leader_comm = new simgrid::smpi::Comm(leaders_group, nullptr);
433 this->set_leaders_comm(leader_comm);
434 this->set_intra_comm(comm_intra);
436 //create intracommunicator
438 for (i=0; i< leader_group_size;i++)
439 leaders_group->set_mapping(leader_list[i], i);
441 if(this->get_leaders_comm()==MPI_COMM_NULL){
442 leader_comm = new simgrid::smpi::Comm(leaders_group, nullptr);
443 this->set_leaders_comm(leader_comm);
445 leader_comm=this->get_leaders_comm();
446 leaders_group->unuse();
448 smpi_process_set_comm_intra(comm_intra);
453 // Are the nodes uniform ? = same number of process/node
454 int my_local_size=comm_intra->size();
455 if(comm_intra->rank()==0) {
456 int* non_uniform_map = xbt_new0(int,leader_group_size);
457 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
458 non_uniform_map, 1, MPI_INT, leader_comm);
459 for(i=0; i < leader_group_size; i++) {
460 if(non_uniform_map[0] != non_uniform_map[i]) {
465 if(is_uniform==0 && this->is_uniform()!=0){
466 non_uniform_map_= non_uniform_map;
468 xbt_free(non_uniform_map);
470 is_uniform_=is_uniform;
472 smpi_coll_tuned_bcast_mpich(&(is_uniform_),1, MPI_INT, 0, comm_intra );
474 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
475 smpi_switch_data_segment(smpi_process_index());
477 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
479 int prev=this->group()->rank(comm_intra->group()->index(0));
480 for (i=1; i<my_local_size; i++){
481 int that=this->group()->rank(comm_intra->group()->index(i));
490 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
492 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
494 is_blocked_=global_blocked;
497 is_blocked_=global_blocked;
499 xbt_free(leader_list);
502 smpi_process_set_replaying(true);
505 int Comm::attr_delete(int keyval){
506 smpi_comm_key_elem elem =
507 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
510 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
511 void* value = nullptr;
513 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
514 int ret = elem->delete_fn(this, keyval, value, &flag);
519 if(attributes_==nullptr)
522 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
526 int Comm::attr_get(int keyval, void* attr_value, int* flag){
527 smpi_comm_key_elem elem =
528 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
531 if(attributes_==nullptr){
536 *static_cast<void**>(attr_value) =
537 xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
546 int Comm::attr_put(int keyval, void* attr_value){
547 if(smpi_comm_keyvals==nullptr)
548 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
549 smpi_comm_key_elem elem =
550 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
554 void* value = nullptr;
555 this->attr_get(keyval, &value, &flag);
556 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
557 int ret = elem->delete_fn(this, keyval, value, &flag);
561 if(attributes_==nullptr)
562 attributes_ = xbt_dict_new_homogeneous(nullptr);
564 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
571 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
573 if(smpi_comm_keyvals==nullptr)
574 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
576 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
578 value->copy_fn=copy_fn;
579 value->delete_fn=delete_fn;
581 *keyval = comm_keyval_id;
582 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
587 int smpi_comm_keyval_free(int* keyval){
588 smpi_comm_key_elem elem =
589 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
592 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));