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. */
11 #include "smpi_mpi_dt_private.h"
13 #include "src/simix/smx_private.h"
14 #include "colls/colls.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
19 xbt_dict_t smpi_comm_keyvals = NULL;
20 int comm_keyval_id = 0;//avoid collisions
22 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
23 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
25 typedef struct s_smpi_mpi_communicator {
27 MPIR_Topo_type topoType;
28 MPI_Topology topo; // to be replaced by an union
30 MPI_Comm leaders_comm;//inter-node communicator
31 MPI_Comm intra_comm;//intra-node communicator . For MPI_COMM_WORLD this can't be used, as var is global.
32 //use an intracomm stored in the process data instead
33 int* leaders_map; //who is the leader of each process
35 int* non_uniform_map; //set if smp nodes have a different number of processes allocated
36 int is_blocked;// are ranks allocated on the same smp node contiguous ?
37 xbt_dict_t attributes;
38 } s_smpi_mpi_communicator_t;
40 static int smpi_compare_rankmap(const void *a, const void *b)
42 const int* x = (const int*)a;
43 const int* y = (const int*)b;
60 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
64 comm = xbt_new(s_smpi_mpi_communicator_t, 1);
67 comm->topoType = MPI_INVALID_TOPO;
69 comm->intra_comm = MPI_COMM_NULL;
70 comm->leaders_comm = MPI_COMM_NULL;
72 comm->non_uniform_map = NULL;
73 comm->leaders_map = NULL;
75 comm->attributes=NULL;
79 void smpi_comm_destroy(MPI_Comm comm)
81 if (comm == MPI_COMM_UNINITIALIZED)
82 comm = smpi_process_comm_world();
83 smpi_topo_destroy(comm->topo); // there's no use count on topos
84 smpi_comm_unuse(comm);
87 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
88 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
89 smpi_switch_data_segment(smpi_process_index());
91 MPI_Group cp=smpi_group_copy(smpi_comm_group(comm));
92 (*newcomm) = smpi_comm_new(cp, smpi_comm_topo(comm));
93 int ret = MPI_SUCCESS;
94 //todo: faire en sorte que ça fonctionne avec un communicator dupliqué (refaire un init_smp ?)
96 /* MPI_Comm tmp=smpi_comm_get_intra_comm(comm);
97 if( tmp != MPI_COMM_NULL)
98 smpi_comm_set_intra_comm((*newcomm), smpi_comm_dup(tmp));
99 tmp=smpi_comm_get_leaders_comm(comm);
100 if( tmp != MPI_COMM_NULL)
101 smpi_comm_set_leaders_comm((*newcomm), smpi_comm_dup(tmp));
102 if(comm->non_uniform_map !=NULL){
103 (*newcomm)->non_uniform_map=
104 xbt_malloc(smpi_comm_size(comm->leaders_comm)*sizeof(int));
105 memcpy((*newcomm)->non_uniform_map,
106 comm->non_uniform_map,smpi_comm_size(comm->leaders_comm)*sizeof(int) );
108 if(comm->leaders_map !=NULL){
109 (*newcomm)->leaders_map=xbt_malloc(smpi_comm_size(comm)*sizeof(int));
110 memcpy((*newcomm)->leaders_map,
111 comm->leaders_map,smpi_comm_size(comm)*sizeof(int) );
113 if(comm->attributes !=NULL){
114 (*newcomm)->attributes=xbt_dict_new();
115 xbt_dict_cursor_t cursor = NULL;
120 xbt_dict_foreach(comm->attributes, cursor, key, value_in){
121 smpi_comm_key_elem elem =
122 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)key, sizeof(int)));
123 if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
124 ret = elem->copy_fn(comm, *key, NULL, value_in, &value_out, &flag );
125 if(ret!=MPI_SUCCESS){
126 smpi_comm_destroy(*newcomm);
127 *newcomm=MPI_COMM_NULL;
131 xbt_dict_set_ext((*newcomm)->attributes, (const char*)key, sizeof(int),value_out, NULL);
138 MPI_Group smpi_comm_group(MPI_Comm comm)
140 if (comm == MPI_COMM_UNINITIALIZED)
141 comm = smpi_process_comm_world();
145 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
146 if (comm != MPI_COMM_NULL)
151 int smpi_comm_size(MPI_Comm comm)
153 if (comm == MPI_COMM_UNINITIALIZED)
154 comm = smpi_process_comm_world();
155 return smpi_group_size(smpi_comm_group(comm));
158 int smpi_comm_rank(MPI_Comm comm)
160 if (comm == MPI_COMM_UNINITIALIZED)
161 comm = smpi_process_comm_world();
162 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
165 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
167 if (comm == MPI_COMM_UNINITIALIZED)
168 comm = smpi_process_comm_world();
169 if(comm == MPI_COMM_WORLD) {
170 strcpy(name, "WORLD");
173 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
177 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
178 if (comm == MPI_COMM_UNINITIALIZED)
179 comm = smpi_process_comm_world();
180 comm->leaders_comm=leaders;
183 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
184 comm->intra_comm=leaders;
187 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
188 if (comm == MPI_COMM_UNINITIALIZED)
189 comm = smpi_process_comm_world();
190 return comm->non_uniform_map;
193 int* smpi_comm_get_leaders_map(MPI_Comm comm){
194 if (comm == MPI_COMM_UNINITIALIZED)
195 comm = smpi_process_comm_world();
196 return comm->leaders_map;
199 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
200 if (comm == MPI_COMM_UNINITIALIZED)
201 comm = smpi_process_comm_world();
202 return comm->leaders_comm;
205 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
206 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
207 return smpi_process_get_comm_intra();
208 else return comm->intra_comm;
211 int smpi_comm_is_uniform(MPI_Comm comm){
212 if (comm == MPI_COMM_UNINITIALIZED)
213 comm = smpi_process_comm_world();
214 return comm->is_uniform;
217 int smpi_comm_is_blocked(MPI_Comm comm){
218 if (comm == MPI_COMM_UNINITIALIZED)
219 comm = smpi_process_comm_world();
220 return comm->is_blocked;
223 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
225 if (comm == MPI_COMM_UNINITIALIZED)
226 comm = smpi_process_comm_world();
227 int system_tag = 123;
228 int index, rank, size, i, j, count, reqs;
232 MPI_Group group, group_root, group_out;
233 MPI_Group* group_snd;
234 MPI_Request* requests;
236 group_root = group_out = NULL;
237 group = smpi_comm_group(comm);
238 rank = smpi_comm_rank(comm);
239 size = smpi_comm_size(comm);
240 /* Gather all colors and keys on rank 0 */
241 sendbuf = xbt_new(int, 2);
245 recvbuf = xbt_new(int, 2 * size);
249 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
251 /* Do the actual job */
253 group_snd = xbt_new(MPI_Group, size);
254 rankmap = xbt_new(int, 2 * size);
255 for(i = 0; i < size; i++) {
256 if(recvbuf[2 * i] == MPI_UNDEFINED) {
260 for(j = i + 1; j < size; j++) {
261 if(recvbuf[2 * i] == recvbuf[2 * j]) {
262 recvbuf[2 * j] = MPI_UNDEFINED;
263 rankmap[2 * count] = j;
264 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
268 /* Add self in the group */
269 recvbuf[2 * i] = MPI_UNDEFINED;
270 rankmap[2 * count] = i;
271 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
273 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
274 group_out = smpi_group_new(count);
276 group_root = group_out; /* Save root's group */
278 for(j = 0; j < count; j++) {
279 index = smpi_group_index(group, rankmap[2 * j]);
280 smpi_group_set_mapping(group_out, index, j);
282 requests = xbt_new(MPI_Request, count);
284 for(j = 0; j < count; j++) {
285 if(rankmap[2 * j] != 0) {
286 group_snd[reqs]=smpi_group_copy(group_out);
287 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
292 smpi_group_destroy(group_out);
294 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
300 group_out = group_root; /* exit with root's group */
302 if(color != MPI_UNDEFINED) {
303 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
304 } /* otherwise, exit with group_out == NULL */
306 return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
309 void smpi_comm_use(MPI_Comm comm){
310 if (comm == MPI_COMM_UNINITIALIZED)
311 comm = smpi_process_comm_world();
312 smpi_group_use(comm->group);
316 void smpi_comm_cleanup_attributes(MPI_Comm comm){
317 if(comm->attributes !=NULL){
318 xbt_dict_cursor_t cursor = NULL;
322 xbt_dict_foreach(comm->attributes, cursor, key, value){
323 smpi_comm_key_elem elem =
324 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key));
325 if(elem && elem->delete_fn)
326 elem->delete_fn(comm, *key, value, &flag);
331 void smpi_comm_cleanup_smp(MPI_Comm comm){
332 if(comm->intra_comm != MPI_COMM_NULL)
333 smpi_comm_unuse(comm->intra_comm);
334 if(comm->leaders_comm != MPI_COMM_NULL)
335 smpi_comm_unuse(comm->leaders_comm);
336 if(comm->non_uniform_map !=NULL)
337 xbt_free(comm->non_uniform_map);
338 if(comm->leaders_map !=NULL)
339 xbt_free(comm->leaders_map);
342 void smpi_comm_unuse(MPI_Comm comm){
343 if (comm == MPI_COMM_UNINITIALIZED)
344 comm = smpi_process_comm_world();
346 smpi_group_unuse(comm->group);
348 if(comm->refcount==0){
349 smpi_comm_cleanup_smp(comm);
350 smpi_comm_cleanup_attributes(comm);
355 static int compare_ints (const void *a, const void *b)
357 const int *da = (const int *) a;
358 const int *db = (const int *) b;
360 return (*da > *db) - (*da < *db);
363 void smpi_comm_init_smp(MPI_Comm comm){
366 if (comm == MPI_COMM_UNINITIALIZED)
367 comm = smpi_process_comm_world();
369 int comm_size =smpi_comm_size(comm);
371 // If we are in replay - perform an ugly hack
372 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
373 int replaying = 0; //cache data to set it back again after
374 if(smpi_process_get_replaying()){
376 smpi_process_set_replaying(0);
379 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
380 smpi_switch_data_segment(smpi_process_index());
382 //identify neighbours in comm
383 //get the indexes of all processes sharing the same simix host
384 xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
385 int intra_comm_size = 0;
386 //only one process/node, disable SMP support and return
387 // if(intra_comm_size==1){
388 // smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
389 // //smpi_comm_set_leaders_comm(comm, comm);
390 // smpi_process_set_comm_intra(MPI_COMM_SELF);
394 int min_index=INT_MAX;//the minimum index will be the leader
395 smx_process_t process = NULL;
396 xbt_swag_foreach(process, process_list) {
398 int index = SIMIX_process_get_PID(process) -1;
400 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
402 //the process is in the comm
403 if(index < min_index)
408 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
409 MPI_Group group_intra = smpi_group_new(intra_comm_size);
412 xbt_swag_foreach(process, process_list) {
414 int index = SIMIX_process_get_PID(process) -1;
415 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
416 smpi_group_set_mapping(group_intra, index, i);
421 MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
422 //MPI_Comm shmem_comm = smpi_process_comm_intra();
423 //int intra_rank = smpi_comm_rank(shmem_comm);
425 //if(smpi_process_index()==min_index)
428 int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
429 int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
430 for(i=0; i<comm_size; i++){
434 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
436 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
437 smpi_switch_data_segment(smpi_process_index());
440 if(!comm->leaders_map){
441 comm->leaders_map= leaders_map;
443 xbt_free(leaders_map);
446 int leader_group_size = 0;
447 for(i=0; i<comm_size; i++){
449 for(j=0;j<leader_group_size; j++){
450 if(comm->leaders_map[i]==leader_list[j]){
455 leader_list[leader_group_size]=comm->leaders_map[i];
459 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
461 MPI_Group leaders_group = smpi_group_new(leader_group_size);
463 MPI_Comm leader_comm = MPI_COMM_NULL;
464 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
465 //create leader_communicator
466 for (i=0; i< leader_group_size;i++)
467 smpi_group_set_mapping(leaders_group, leader_list[i], i);
468 leader_comm = smpi_comm_new(leaders_group, NULL);
469 smpi_comm_set_leaders_comm(comm, leader_comm);
470 smpi_comm_set_intra_comm(comm, comm_intra);
472 //create intracommunicator
473 // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
475 for (i=0; i< leader_group_size;i++)
476 smpi_group_set_mapping(leaders_group, leader_list[i], i);
478 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL){
479 leader_comm = smpi_comm_new(leaders_group, NULL);
480 smpi_comm_set_leaders_comm(comm, leader_comm);
482 leader_comm=smpi_comm_get_leaders_comm(comm);
483 smpi_group_unuse(leaders_group);
485 smpi_process_set_comm_intra(comm_intra);
490 // Are the nodes uniform ? = same number of process/node
491 int my_local_size=smpi_comm_size(comm_intra);
492 if(smpi_comm_rank(comm_intra)==0) {
493 int* non_uniform_map = xbt_new0(int,leader_group_size);
494 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
495 non_uniform_map, 1, MPI_INT, leader_comm);
496 for(i=0; i < leader_group_size; i++) {
497 if(non_uniform_map[0] != non_uniform_map[i]) {
502 if(!is_uniform && smpi_comm_is_uniform(comm)){
503 comm->non_uniform_map= non_uniform_map;
505 xbt_free(non_uniform_map);
507 comm->is_uniform=is_uniform;
509 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
511 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
512 smpi_switch_data_segment(smpi_process_index());
514 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
516 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
517 for (i=1; i<my_local_size; i++){
518 int that=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
527 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
529 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
530 if(smpi_comm_rank(comm)==0){
531 comm->is_blocked=global_blocked;
534 comm->is_blocked=global_blocked;
536 xbt_free(leader_list);
539 smpi_process_set_replaying(1);
542 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
543 smpi_comm_key_elem elem =
544 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
547 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
550 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
551 int ret = elem->delete_fn(comm, keyval, value, &flag);
552 if(ret!=MPI_SUCCESS) return ret;
555 if(comm->attributes==NULL)
558 xbt_dict_remove_ext(comm->attributes, (const char*)&keyval, sizeof(int));
562 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
563 smpi_comm_key_elem elem =
564 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
568 if(comm->attributes==NULL){
573 *(void**)attr_value = xbt_dict_get_ext(comm->attributes, (const char*)&keyval, sizeof(int));
582 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
583 if(!smpi_comm_keyvals)
584 smpi_comm_keyvals = xbt_dict_new();
585 smpi_comm_key_elem elem =
586 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
591 smpi_comm_attr_get(comm, keyval, &value, &flag);
592 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
593 int ret = elem->delete_fn(comm, keyval, value, &flag);
594 if(ret!=MPI_SUCCESS) return ret;
596 if(comm->attributes==NULL)
597 comm->attributes=xbt_dict_new();
599 xbt_dict_set_ext(comm->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
603 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
605 if(!smpi_comm_keyvals)
606 smpi_comm_keyvals = xbt_dict_new();
608 smpi_comm_key_elem value = (smpi_comm_key_elem) xbt_new0(s_smpi_mpi_comm_key_elem_t,1);
610 value->copy_fn=copy_fn;
611 value->delete_fn=delete_fn;
613 *keyval = comm_keyval_id;
614 xbt_dict_set_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int),(void*)value, NULL);
619 int smpi_comm_keyval_free(int* keyval){
620 smpi_comm_key_elem elem =
621 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int)));
625 xbt_dict_remove_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int));