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
26 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
27 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
29 static int smpi_compare_rankmap(const void *a, const void *b)
31 const int* x = static_cast<const int*>(a);
32 const int* y = static_cast<const int*>(b);
52 Comm::Comm(MPI_Group group, MPI_Topology topo)
56 m_topoType = MPI_INVALID_TOPO;
58 m_intra_comm = MPI_COMM_NULL;
59 m_leaders_comm = MPI_COMM_NULL;
61 m_non_uniform_map = nullptr;
62 m_leaders_map = nullptr;
69 if (this == MPI_COMM_UNINITIALIZED)
70 return smpi_process_comm_world()->destroy();
71 smpi_topo_destroy(m_topo); // there's no use count on topos
75 int Comm::dup(MPI_Comm* newcomm){
76 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
77 smpi_switch_data_segment(smpi_process_index());
79 MPI_Group cp = new simgrid::SMPI::Group(this->group());
80 (*newcomm) = new simgrid::SMPI::Comm(cp, this->topo());
81 int ret = MPI_SUCCESS;
83 if(m_attributes !=nullptr){
84 (*newcomm)->m_attributes = xbt_dict_new_homogeneous(nullptr);
85 xbt_dict_cursor_t cursor = nullptr;
90 xbt_dict_foreach (m_attributes, cursor, key, value_in) {
91 smpi_comm_key_elem elem =
92 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
93 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
94 ret = elem->copy_fn(this, atoi(key), nullptr, value_in, &value_out, &flag);
95 if (ret != MPI_SUCCESS) {
96 (*newcomm)->destroy();
97 *newcomm = MPI_COMM_NULL;
98 xbt_dict_cursor_free(&cursor);
102 xbt_dict_set_ext((*newcomm)->m_attributes, key, sizeof(int), value_out, nullptr);
109 MPI_Group Comm::group()
111 if (this == MPI_COMM_UNINITIALIZED)
112 return smpi_process_comm_world()->group();
116 MPI_Topology Comm::topo() {
122 if (this == MPI_COMM_UNINITIALIZED)
123 return smpi_process_comm_world()->size();
124 return m_group->size();
129 if (this == MPI_COMM_UNINITIALIZED)
130 return smpi_process_comm_world()->rank();
131 return m_group->rank(smpi_process_index());
134 void Comm::get_name (char* name, int* len)
136 if (this == MPI_COMM_UNINITIALIZED)
137 return smpi_process_comm_world()->get_name(name, len);
138 if(this == MPI_COMM_WORLD) {
139 strncpy(name, "WORLD",5);
142 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", this);
146 void Comm::set_leaders_comm(MPI_Comm leaders){
147 if (this == MPI_COMM_UNINITIALIZED)
148 return smpi_process_comm_world()->set_leaders_comm(leaders);
149 m_leaders_comm=leaders;
152 void Comm::set_intra_comm(MPI_Comm leaders){
153 m_intra_comm=leaders;
156 int* Comm::get_non_uniform_map(){
157 if (this == MPI_COMM_UNINITIALIZED)
158 return smpi_process_comm_world()->get_non_uniform_map();
159 return m_non_uniform_map;
162 int* Comm::get_leaders_map(){
163 if (this == MPI_COMM_UNINITIALIZED)
164 return smpi_process_comm_world()->get_leaders_map();
165 return m_leaders_map;
168 MPI_Comm Comm::get_leaders_comm(){
169 if (this == MPI_COMM_UNINITIALIZED)
170 return smpi_process_comm_world()->get_leaders_comm();
171 return m_leaders_comm;
174 MPI_Comm Comm::get_intra_comm(){
175 if (this == MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD)
176 return smpi_process_get_comm_intra();
177 else return m_intra_comm;
180 int Comm::is_uniform(){
181 if (this == MPI_COMM_UNINITIALIZED)
182 return smpi_process_comm_world()->is_uniform();
186 int Comm::is_blocked(){
187 if (this == MPI_COMM_UNINITIALIZED)
188 return smpi_process_comm_world()->is_blocked();
192 MPI_Comm Comm::split(int color, int key)
194 if (this == MPI_COMM_UNINITIALIZED)
195 return smpi_process_comm_world()->split(color, key);
196 int system_tag = 123;
199 MPI_Group group_root = nullptr;
200 MPI_Group group_out = nullptr;
201 MPI_Group group = this->group();
202 int rank = this->rank();
203 int size = this->size();
204 /* Gather all colors and keys on rank 0 */
205 int* sendbuf = xbt_new(int, 2);
209 recvbuf = xbt_new(int, 2 * size);
213 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, this);
215 /* Do the actual job */
217 MPI_Group* group_snd = xbt_new(MPI_Group, size);
218 int* rankmap = xbt_new(int, 2 * size);
219 for (int i = 0; i < size; i++) {
220 if (recvbuf[2 * i] != MPI_UNDEFINED) {
222 for (int j = i + 1; j < size; j++) {
223 if(recvbuf[2 * i] == recvbuf[2 * j]) {
224 recvbuf[2 * j] = MPI_UNDEFINED;
225 rankmap[2 * count] = j;
226 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
230 /* Add self in the group */
231 recvbuf[2 * i] = MPI_UNDEFINED;
232 rankmap[2 * count] = i;
233 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
235 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
236 group_out = new simgrid::SMPI::Group(count);
238 group_root = group_out; /* Save root's group */
240 for (int j = 0; j < count; j++) {
241 int index = group->index(rankmap[2 * j]);
242 group_out->set_mapping(index, j);
244 MPI_Request* requests = xbt_new(MPI_Request, count);
246 for (int j = 0; j < count; j++) {
247 if(rankmap[2 * j] != 0) {
248 group_snd[reqs]=new simgrid::SMPI::Group(group_out);
249 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, this);
254 group_out->destroy();
256 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
263 group_out = group_root; /* exit with root's group */
265 if(color != MPI_UNDEFINED) {
266 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, this, MPI_STATUS_IGNORE);
267 } /* otherwise, exit with group_out == nullptr */
269 return group_out!=nullptr ? new simgrid::SMPI::Comm(group_out, nullptr) : MPI_COMM_NULL;
273 if (this == MPI_COMM_UNINITIALIZED)
274 return smpi_process_comm_world()->use();
279 void Comm::cleanup_attributes(){
280 if(m_attributes !=nullptr){
281 xbt_dict_cursor_t cursor = nullptr;
285 xbt_dict_foreach (m_attributes, cursor, key, value) {
286 smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
287 if (elem != nullptr && elem->delete_fn != nullptr)
288 elem->delete_fn(this, atoi(key), value, &flag);
290 xbt_dict_free(&m_attributes);
294 void Comm::cleanup_smp(){
295 if (m_intra_comm != MPI_COMM_NULL)
296 m_intra_comm->unuse();
297 if (m_leaders_comm != MPI_COMM_NULL)
298 m_leaders_comm->unuse();
299 if (m_non_uniform_map != nullptr)
300 xbt_free(m_non_uniform_map);
301 if (m_leaders_map != nullptr)
302 xbt_free(m_leaders_map);
306 if (this == MPI_COMM_UNINITIALIZED)
307 return smpi_process_comm_world()->unuse();
313 this->cleanup_attributes();
318 static int compare_ints (const void *a, const void *b)
320 const int *da = static_cast<const int *>(a);
321 const int *db = static_cast<const int *>(b);
323 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
326 void Comm::init_smp(){
329 if (this == MPI_COMM_UNINITIALIZED)
330 return smpi_process_comm_world()->init_smp();
332 int comm_size = this->size();
334 // If we are in replay - perform an ugly hack
335 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
336 bool replaying = false; //cache data to set it back again after
337 if(smpi_process_get_replaying()){
339 smpi_process_set_replaying(false);
342 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
343 smpi_switch_data_segment(smpi_process_index());
345 //identify neighbours in comm
346 //get the indexes of all processes sharing the same simix host
347 xbt_swag_t process_list = SIMIX_host_self()->processes();
348 int intra_comm_size = 0;
350 int min_index=INT_MAX;//the minimum index will be the leader
351 smx_actor_t process = nullptr;
352 xbt_swag_foreach(process, process_list) {
353 int index = process->pid -1;
355 if(this->group()->rank(index)!=MPI_UNDEFINED){
357 //the process is in the comm
358 if(index < min_index)
363 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
364 MPI_Group group_intra = new simgrid::SMPI::Group(intra_comm_size);
367 xbt_swag_foreach(process, process_list) {
368 int index = process->pid -1;
369 if(this->group()->rank(index)!=MPI_UNDEFINED){
370 group_intra->set_mapping(index, i);
375 MPI_Comm comm_intra = new simgrid::SMPI::Comm(group_intra, nullptr);
378 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
379 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
380 for(i=0; i<comm_size; i++){
384 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, this);
386 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
387 smpi_switch_data_segment(smpi_process_index());
390 if(m_leaders_map==nullptr){
391 m_leaders_map= leaders_map;
393 xbt_free(leaders_map);
396 int leader_group_size = 0;
397 for(i=0; i<comm_size; i++){
399 for(j=0;j<leader_group_size; j++){
400 if(m_leaders_map[i]==leader_list[j]){
405 leader_list[leader_group_size]=m_leaders_map[i];
409 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
411 MPI_Group leaders_group = new simgrid::SMPI::Group(leader_group_size);
413 MPI_Comm leader_comm = MPI_COMM_NULL;
414 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && this!=MPI_COMM_WORLD){
415 //create leader_communicator
416 for (i=0; i< leader_group_size;i++)
417 leaders_group->set_mapping(leader_list[i], i);
418 leader_comm = new simgrid::SMPI::Comm(leaders_group, nullptr);
419 this->set_leaders_comm(leader_comm);
420 this->set_intra_comm(comm_intra);
422 //create intracommunicator
424 for (i=0; i< leader_group_size;i++)
425 leaders_group->set_mapping(leader_list[i], i);
427 if(this->get_leaders_comm()==MPI_COMM_NULL){
428 leader_comm = new simgrid::SMPI::Comm(leaders_group, nullptr);
429 this->set_leaders_comm(leader_comm);
431 leader_comm=this->get_leaders_comm();
432 leaders_group->unuse();
434 smpi_process_set_comm_intra(comm_intra);
439 // Are the nodes uniform ? = same number of process/node
440 int my_local_size=comm_intra->size();
441 if(comm_intra->rank()==0) {
442 int* non_uniform_map = xbt_new0(int,leader_group_size);
443 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
444 non_uniform_map, 1, MPI_INT, leader_comm);
445 for(i=0; i < leader_group_size; i++) {
446 if(non_uniform_map[0] != non_uniform_map[i]) {
451 if(is_uniform==0 && this->is_uniform()!=0){
452 m_non_uniform_map= non_uniform_map;
454 xbt_free(non_uniform_map);
456 m_is_uniform=is_uniform;
458 smpi_coll_tuned_bcast_mpich(&(m_is_uniform),1, MPI_INT, 0, comm_intra );
460 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
461 smpi_switch_data_segment(smpi_process_index());
463 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
465 int prev=this->group()->rank(comm_intra->group()->index(0));
466 for (i=1; i<my_local_size; i++){
467 int that=this->group()->rank(comm_intra->group()->index(i));
476 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, this);
478 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || this==MPI_COMM_WORLD){
480 m_is_blocked=global_blocked;
483 m_is_blocked=global_blocked;
485 xbt_free(leader_list);
488 smpi_process_set_replaying(true);
491 int Comm::attr_delete(int keyval){
492 smpi_comm_key_elem elem =
493 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
496 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
497 void* value = nullptr;
499 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
500 int ret = elem->delete_fn(this, keyval, value, &flag);
505 if(m_attributes==nullptr)
508 xbt_dict_remove_ext(m_attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
512 int Comm::attr_get(int keyval, void* attr_value, int* flag){
513 smpi_comm_key_elem elem =
514 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
517 if(m_attributes==nullptr){
522 *static_cast<void**>(attr_value) =
523 xbt_dict_get_ext(m_attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
532 int Comm::attr_put(int keyval, void* attr_value){
533 if(smpi_comm_keyvals==nullptr)
534 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
535 smpi_comm_key_elem elem =
536 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
540 void* value = nullptr;
541 this->attr_get(keyval, &value, &flag);
542 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
543 int ret = elem->delete_fn(this, keyval, value, &flag);
547 if(m_attributes==nullptr)
548 m_attributes = xbt_dict_new_homogeneous(nullptr);
550 xbt_dict_set_ext(m_attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
557 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
559 if(smpi_comm_keyvals==nullptr)
560 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
562 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
564 value->copy_fn=copy_fn;
565 value->delete_fn=delete_fn;
567 *keyval = comm_keyval_id;
568 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
573 int smpi_comm_keyval_free(int* keyval){
574 smpi_comm_key_elem elem =
575 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
578 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));