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 typedef struct s_smpi_mpi_communicator {
31 MPIR_Topo_type topoType;
32 MPI_Topology topo; // to be replaced by an union
34 MPI_Comm leaders_comm;//inter-node communicator
35 MPI_Comm intra_comm;//intra-node communicator . For MPI_COMM_WORLD this can't be used, as var is global.
36 //use an intracomm stored in the process data instead
37 int* leaders_map; //who is the leader of each process
39 int* non_uniform_map; //set if smp nodes have a different number of processes allocated
40 int is_blocked;// are ranks allocated on the same smp node contiguous ?
41 xbt_dict_t attributes;
42 } s_smpi_mpi_communicator_t;
44 static int smpi_compare_rankmap(const void *a, const void *b)
46 const int* x = static_cast<const int*>(a);
47 const int* y = static_cast<const int*>(b);
64 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
68 comm = xbt_new(s_smpi_mpi_communicator_t, 1);
71 comm->topoType = MPI_INVALID_TOPO;
73 comm->intra_comm = MPI_COMM_NULL;
74 comm->leaders_comm = MPI_COMM_NULL;
76 comm->non_uniform_map = nullptr;
77 comm->leaders_map = nullptr;
79 comm->attributes=nullptr;
83 void smpi_comm_destroy(MPI_Comm comm)
85 if (comm == MPI_COMM_UNINITIALIZED)
86 comm = smpi_process_comm_world();
87 smpi_topo_destroy(comm->topo); // there's no use count on topos
88 smpi_comm_unuse(comm);
91 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
92 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
93 smpi_switch_data_segment(smpi_process_index());
95 MPI_Group cp=smpi_group_copy(smpi_comm_group(comm));
96 (*newcomm) = smpi_comm_new(cp, smpi_comm_topo(comm));
97 int ret = MPI_SUCCESS;
99 if(comm->attributes !=nullptr){
100 (*newcomm)->attributes=xbt_dict_new();
101 xbt_dict_cursor_t cursor = nullptr;
106 xbt_dict_foreach(comm->attributes, cursor, key, value_in){
107 smpi_comm_key_elem elem =
108 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(key), sizeof(int)));
109 if(elem!=nullptr && elem->copy_fn!=MPI_NULL_COPY_FN){
110 ret = elem->copy_fn(comm, *key, nullptr, value_in, &value_out, &flag );
111 if(ret!=MPI_SUCCESS){
112 smpi_comm_destroy(*newcomm);
113 *newcomm=MPI_COMM_NULL;
117 xbt_dict_set_ext((*newcomm)->attributes, reinterpret_cast<const char*>(key), sizeof(int),value_out, nullptr);
124 MPI_Group smpi_comm_group(MPI_Comm comm)
126 if (comm == MPI_COMM_UNINITIALIZED)
127 comm = smpi_process_comm_world();
131 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
132 if (comm != MPI_COMM_NULL)
137 int smpi_comm_size(MPI_Comm comm)
139 if (comm == MPI_COMM_UNINITIALIZED)
140 comm = smpi_process_comm_world();
141 return smpi_group_size(smpi_comm_group(comm));
144 int smpi_comm_rank(MPI_Comm comm)
146 if (comm == MPI_COMM_UNINITIALIZED)
147 comm = smpi_process_comm_world();
148 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
151 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
153 if (comm == MPI_COMM_UNINITIALIZED)
154 comm = smpi_process_comm_world();
155 if(comm == MPI_COMM_WORLD) {
156 strncpy(name, "WORLD",5);
159 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
163 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
164 if (comm == MPI_COMM_UNINITIALIZED)
165 comm = smpi_process_comm_world();
166 comm->leaders_comm=leaders;
169 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
170 comm->intra_comm=leaders;
173 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
174 if (comm == MPI_COMM_UNINITIALIZED)
175 comm = smpi_process_comm_world();
176 return comm->non_uniform_map;
179 int* smpi_comm_get_leaders_map(MPI_Comm comm){
180 if (comm == MPI_COMM_UNINITIALIZED)
181 comm = smpi_process_comm_world();
182 return comm->leaders_map;
185 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
186 if (comm == MPI_COMM_UNINITIALIZED)
187 comm = smpi_process_comm_world();
188 return comm->leaders_comm;
191 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
192 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
193 return smpi_process_get_comm_intra();
194 else return comm->intra_comm;
197 int smpi_comm_is_uniform(MPI_Comm comm){
198 if (comm == MPI_COMM_UNINITIALIZED)
199 comm = smpi_process_comm_world();
200 return comm->is_uniform;
203 int smpi_comm_is_blocked(MPI_Comm comm){
204 if (comm == MPI_COMM_UNINITIALIZED)
205 comm = smpi_process_comm_world();
206 return comm->is_blocked;
209 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
211 if (comm == MPI_COMM_UNINITIALIZED)
212 comm = smpi_process_comm_world();
213 int system_tag = 123;
214 int index, rank, size, i, j, count, reqs;
218 MPI_Group group, group_root, group_out;
219 MPI_Group* group_snd;
220 MPI_Request* requests;
222 group_root = nullptr;
224 group = smpi_comm_group(comm);
225 rank = smpi_comm_rank(comm);
226 size = smpi_comm_size(comm);
227 /* Gather all colors and keys on rank 0 */
228 sendbuf = xbt_new(int, 2);
232 recvbuf = xbt_new(int, 2 * size);
236 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
238 /* Do the actual job */
240 group_snd = xbt_new(MPI_Group, size);
241 rankmap = xbt_new(int, 2 * size);
242 for(i = 0; i < size; i++) {
243 if(recvbuf[2 * i] != MPI_UNDEFINED) {
246 for(j = i + 1; j < size; j++) {
247 if(recvbuf[2 * i] == recvbuf[2 * j]) {
248 recvbuf[2 * j] = MPI_UNDEFINED;
249 rankmap[2 * count] = j;
250 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
254 /* Add self in the group */
255 recvbuf[2 * i] = MPI_UNDEFINED;
256 rankmap[2 * count] = i;
257 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
259 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
260 group_out = smpi_group_new(count);
262 group_root = group_out; /* Save root's group */
264 for(j = 0; j < count; j++) {
265 index = smpi_group_index(group, rankmap[2 * j]);
266 smpi_group_set_mapping(group_out, index, j);
268 requests = xbt_new(MPI_Request, count);
270 for(j = 0; j < count; j++) {
271 if(rankmap[2 * j] != 0) {
272 group_snd[reqs]=smpi_group_copy(group_out);
273 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
278 smpi_group_destroy(group_out);
280 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
287 group_out = group_root; /* exit with root's group */
289 if(color != MPI_UNDEFINED) {
290 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
291 } /* otherwise, exit with group_out == nullptr */
293 return group_out!=nullptr ? smpi_comm_new(group_out, nullptr) : MPI_COMM_NULL;
296 void smpi_comm_use(MPI_Comm comm){
297 if (comm == MPI_COMM_UNINITIALIZED)
298 comm = smpi_process_comm_world();
299 smpi_group_use(comm->group);
303 void smpi_comm_cleanup_attributes(MPI_Comm comm){
304 if(comm->attributes !=nullptr){
305 xbt_dict_cursor_t cursor = nullptr;
309 xbt_dict_foreach(comm->attributes, cursor, key, value){
310 smpi_comm_key_elem elem =
311 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, reinterpret_cast<const char*>(key)));
312 if(elem!=nullptr && elem->delete_fn!=nullptr)
313 elem->delete_fn(comm, *key, value, &flag);
315 xbt_dict_free(&comm->attributes);
319 void smpi_comm_cleanup_smp(MPI_Comm comm){
320 if(comm->intra_comm != MPI_COMM_NULL)
321 smpi_comm_unuse(comm->intra_comm);
322 if(comm->leaders_comm != MPI_COMM_NULL)
323 smpi_comm_unuse(comm->leaders_comm);
324 if(comm->non_uniform_map !=nullptr)
325 xbt_free(comm->non_uniform_map);
326 if(comm->leaders_map !=nullptr)
327 xbt_free(comm->leaders_map);
330 void smpi_comm_unuse(MPI_Comm comm){
331 if (comm == MPI_COMM_UNINITIALIZED)
332 comm = smpi_process_comm_world();
334 smpi_group_unuse(comm->group);
336 if(comm->refcount==0){
337 smpi_comm_cleanup_smp(comm);
338 smpi_comm_cleanup_attributes(comm);
343 static int compare_ints (const void *a, const void *b)
345 const int *da = static_cast<const int *>(a);
346 const int *db = static_cast<const int *>(b);
348 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
351 void smpi_comm_init_smp(MPI_Comm comm){
354 if (comm == MPI_COMM_UNINITIALIZED)
355 comm = smpi_process_comm_world();
357 int comm_size =smpi_comm_size(comm);
359 // If we are in replay - perform an ugly hack
360 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
361 bool replaying = false; //cache data to set it back again after
362 if(smpi_process_get_replaying()){
364 smpi_process_set_replaying(false);
367 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
368 smpi_switch_data_segment(smpi_process_index());
370 //identify neighbours in comm
371 //get the indexes of all processes sharing the same simix host
372 xbt_swag_t process_list = SIMIX_host_self()->processes();
373 int intra_comm_size = 0;
375 int min_index=INT_MAX;//the minimum index will be the leader
376 smx_process_t process = nullptr;
377 xbt_swag_foreach(process, process_list) {
378 int index = SIMIX_process_get_PID(process) -1;
380 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
382 //the process is in the comm
383 if(index < min_index)
388 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
389 MPI_Group group_intra = smpi_group_new(intra_comm_size);
392 xbt_swag_foreach(process, process_list) {
393 int index = SIMIX_process_get_PID(process) -1;
394 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
395 smpi_group_set_mapping(group_intra, index, i);
400 MPI_Comm comm_intra = smpi_comm_new(group_intra, nullptr);
403 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
404 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
405 for(i=0; i<comm_size; i++){
409 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
411 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
412 smpi_switch_data_segment(smpi_process_index());
415 if(comm->leaders_map==nullptr){
416 comm->leaders_map= leaders_map;
418 xbt_free(leaders_map);
421 int leader_group_size = 0;
422 for(i=0; i<comm_size; i++){
424 for(j=0;j<leader_group_size; j++){
425 if(comm->leaders_map[i]==leader_list[j]){
430 leader_list[leader_group_size]=comm->leaders_map[i];
434 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
436 MPI_Group leaders_group = smpi_group_new(leader_group_size);
438 MPI_Comm leader_comm = MPI_COMM_NULL;
439 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
440 //create leader_communicator
441 for (i=0; i< leader_group_size;i++)
442 smpi_group_set_mapping(leaders_group, leader_list[i], i);
443 leader_comm = smpi_comm_new(leaders_group, nullptr);
444 smpi_comm_set_leaders_comm(comm, leader_comm);
445 smpi_comm_set_intra_comm(comm, comm_intra);
447 //create intracommunicator
449 for (i=0; i< leader_group_size;i++)
450 smpi_group_set_mapping(leaders_group, leader_list[i], i);
452 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL){
453 leader_comm = smpi_comm_new(leaders_group, nullptr);
454 smpi_comm_set_leaders_comm(comm, leader_comm);
456 leader_comm=smpi_comm_get_leaders_comm(comm);
457 smpi_group_unuse(leaders_group);
459 smpi_process_set_comm_intra(comm_intra);
464 // Are the nodes uniform ? = same number of process/node
465 int my_local_size=smpi_comm_size(comm_intra);
466 if(smpi_comm_rank(comm_intra)==0) {
467 int* non_uniform_map = xbt_new0(int,leader_group_size);
468 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
469 non_uniform_map, 1, MPI_INT, leader_comm);
470 for(i=0; i < leader_group_size; i++) {
471 if(non_uniform_map[0] != non_uniform_map[i]) {
476 if(is_uniform==0 && smpi_comm_is_uniform(comm)!=0){
477 comm->non_uniform_map= non_uniform_map;
479 xbt_free(non_uniform_map);
481 comm->is_uniform=is_uniform;
483 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
485 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
486 smpi_switch_data_segment(smpi_process_index());
488 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
490 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
491 for (i=1; i<my_local_size; i++){
492 int that=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
501 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
503 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
504 if(smpi_comm_rank(comm)==0){
505 comm->is_blocked=global_blocked;
508 comm->is_blocked=global_blocked;
510 xbt_free(leader_list);
513 smpi_process_set_replaying(true);
516 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
517 smpi_comm_key_elem elem =
518 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
521 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
522 void* value = nullptr;
524 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
525 int ret = elem->delete_fn(comm, keyval, value, &flag);
530 if(comm->attributes==nullptr)
533 xbt_dict_remove_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
537 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
538 smpi_comm_key_elem elem =
539 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
542 if(comm->attributes==nullptr){
547 *static_cast<void**>(attr_value) = xbt_dict_get_ext(comm->attributes,
548 reinterpret_cast<const char*>(&keyval), sizeof(int));
557 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
558 if(smpi_comm_keyvals==nullptr)
559 smpi_comm_keyvals = xbt_dict_new();
560 smpi_comm_key_elem elem =
561 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
565 void* value = nullptr;
566 smpi_comm_attr_get(comm, keyval, &value, &flag);
567 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
568 int ret = elem->delete_fn(comm, keyval, value, &flag);
572 if(comm->attributes==nullptr)
573 comm->attributes=xbt_dict_new();
575 xbt_dict_set_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
579 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
581 if(smpi_comm_keyvals==nullptr)
582 smpi_comm_keyvals = xbt_dict_new();
584 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
586 value->copy_fn=copy_fn;
587 value->delete_fn=delete_fn;
589 *keyval = comm_keyval_id;
590 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
595 int smpi_comm_keyval_free(int* keyval){
596 smpi_comm_key_elem elem =
597 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
600 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));