1 /* Copyright (c) 2010-2014. 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 "simix/smx_private.h"
14 #include "colls/colls.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
18 "Logging specific to SMPI (comm)");
20 //number of bytes to hold an int converted in char*
21 #define INTSIZEDCHAR (sizeof(int)*CHAR_BIT-1)/3 + 3
22 xbt_dict_t smpi_comm_keyvals = NULL;
23 int comm_keyval_id=MPI_TAG_UB+1;//avoid collisions
25 /* Support for cartesian topology was added, but there are 2 other types of
26 * topology, graph et dist graph. In order to support them, we have to add a
27 * 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 = (const int*)a;
47 const int* y = (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);
70 smpi_group_use(comm->group);
74 comm->intra_comm = MPI_COMM_NULL;
75 comm->leaders_comm = MPI_COMM_NULL;
77 comm->non_uniform_map = NULL;
78 comm->leaders_map = NULL;
80 comm->attributes=NULL;
84 void smpi_comm_destroy(MPI_Comm comm)
86 if (comm == MPI_COMM_UNINITIALIZED)
87 comm = smpi_process_comm_world();
88 smpi_group_unuse(comm->group);
89 smpi_topo_destroy(comm->topo); // there's no use count on topos
90 smpi_comm_unuse(comm);
93 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
94 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
95 smpi_switch_data_segment(smpi_process_index());
97 (*newcomm) = smpi_comm_new(smpi_comm_group(comm), smpi_comm_topo(comm));
98 int ret = MPI_SUCCESS;
99 //todo: faire en sorte que ça fonctionne avec un communicator dupliqué (refaire un init_smp ?)
101 /* MPI_Comm tmp=smpi_comm_get_intra_comm(comm);
102 if( tmp != MPI_COMM_NULL)
103 smpi_comm_set_intra_comm((*newcomm), smpi_comm_dup(tmp));
104 tmp=smpi_comm_get_leaders_comm(comm);
105 if( tmp != MPI_COMM_NULL)
106 smpi_comm_set_leaders_comm((*newcomm), smpi_comm_dup(tmp));
107 if(comm->non_uniform_map !=NULL){
108 (*newcomm)->non_uniform_map=
109 xbt_malloc(smpi_comm_size(comm->leaders_comm)*sizeof(int));
110 memcpy((*newcomm)->non_uniform_map,
111 comm->non_uniform_map,smpi_comm_size(comm->leaders_comm)*sizeof(int) );
113 if(comm->leaders_map !=NULL){
114 (*newcomm)->leaders_map=xbt_malloc(smpi_comm_size(comm)*sizeof(int));
115 memcpy((*newcomm)->leaders_map,
116 comm->leaders_map,smpi_comm_size(comm)*sizeof(int) );
118 if(comm->attributes !=NULL){
119 (*newcomm)->attributes=xbt_dict_new();
120 xbt_dict_cursor_t cursor = NULL;
125 xbt_dict_foreach(comm->attributes, cursor, key, value_in){
126 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key);
127 if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
128 ret = elem->copy_fn(comm, atoi((const char*)key), NULL, value_in, &value_out, &flag );
129 if(ret!=MPI_SUCCESS){
130 smpi_comm_destroy(*newcomm);
131 *newcomm=MPI_COMM_NULL;
135 xbt_dict_set((*newcomm)->attributes, (const char*)key,value_out, NULL);
143 MPI_Group smpi_comm_group(MPI_Comm comm)
145 if (comm == MPI_COMM_UNINITIALIZED)
146 comm = smpi_process_comm_world();
151 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
152 if (comm != MPI_COMM_NULL)
157 int smpi_comm_size(MPI_Comm comm)
159 if (comm == MPI_COMM_UNINITIALIZED)
160 comm = smpi_process_comm_world();
162 return smpi_group_size(smpi_comm_group(comm));
165 int smpi_comm_rank(MPI_Comm comm)
167 if (comm == MPI_COMM_UNINITIALIZED)
168 comm = smpi_process_comm_world();
169 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
172 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
174 if (comm == MPI_COMM_UNINITIALIZED)
175 comm = smpi_process_comm_world();
176 if(comm == MPI_COMM_WORLD) {
177 strcpy(name, "WORLD");
180 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
184 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
185 if (comm == MPI_COMM_UNINITIALIZED)
186 comm = smpi_process_comm_world();
187 comm->leaders_comm=leaders;
190 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
191 comm->intra_comm=leaders;
194 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
195 if (comm == MPI_COMM_UNINITIALIZED)
196 comm = smpi_process_comm_world();
197 return comm->non_uniform_map;
200 int* smpi_comm_get_leaders_map(MPI_Comm comm){
201 if (comm == MPI_COMM_UNINITIALIZED)
202 comm = smpi_process_comm_world();
203 return comm->leaders_map;
206 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
207 if (comm == MPI_COMM_UNINITIALIZED)
208 comm = smpi_process_comm_world();
209 return comm->leaders_comm;
212 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
213 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
214 return smpi_process_get_comm_intra();
215 else return comm->intra_comm;
218 int smpi_comm_is_uniform(MPI_Comm comm){
219 if (comm == MPI_COMM_UNINITIALIZED)
220 comm = smpi_process_comm_world();
221 return comm->is_uniform;
224 int smpi_comm_is_blocked(MPI_Comm comm){
225 if (comm == MPI_COMM_UNINITIALIZED)
226 comm = smpi_process_comm_world();
227 return comm->is_blocked;
230 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
232 if (comm == MPI_COMM_UNINITIALIZED)
233 comm = smpi_process_comm_world();
234 int system_tag = 123;
235 int index, rank, size, i, j, count, reqs;
239 MPI_Group group, group_root, group_out;
240 MPI_Request* requests;
242 group_root = group_out = NULL;
243 group = smpi_comm_group(comm);
244 rank = smpi_comm_rank(comm);
245 size = smpi_comm_size(comm);
246 /* Gather all colors and keys on rank 0 */
247 sendbuf = xbt_new(int, 2);
251 recvbuf = xbt_new(int, 2 * size);
255 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
257 /* Do the actual job */
259 rankmap = xbt_new(int, 2 * size);
260 for(i = 0; i < size; i++) {
261 if(recvbuf[2 * i] == MPI_UNDEFINED) {
265 for(j = i + 1; j < size; j++) {
266 if(recvbuf[2 * i] == recvbuf[2 * j]) {
267 recvbuf[2 * j] = MPI_UNDEFINED;
268 rankmap[2 * count] = j;
269 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
273 /* Add self in the group */
274 recvbuf[2 * i] = MPI_UNDEFINED;
275 rankmap[2 * count] = i;
276 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
278 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
279 group_out = smpi_group_new(count);
281 group_root = group_out; /* Save root's group */
283 for(j = 0; j < count; j++) {
284 //increment refcounter in order to avoid freeing the group too quick before copy
285 index = smpi_group_index(group, rankmap[2 * j]);
286 smpi_group_set_mapping(group_out, index, j);
288 requests = xbt_new(MPI_Request, count);
290 for(j = 0; j < count; j++) {
291 if(rankmap[2 * j] != 0) {
292 requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
296 smpi_mpi_startall(reqs, requests);
297 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
301 group_out = group_root; /* exit with root's group */
303 if(color != MPI_UNDEFINED) {
304 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
306 group_out=smpi_group_copy(group_out);
308 } /* otherwise, exit with group_out == NULL */
310 return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
313 void smpi_comm_use(MPI_Comm comm){
314 if (comm == MPI_COMM_UNINITIALIZED)
315 comm = smpi_process_comm_world();
319 void smpi_comm_unuse(MPI_Comm comm){
320 if (comm == MPI_COMM_UNINITIALIZED)
321 comm = smpi_process_comm_world();
323 if(comm->refcount==0){
324 if(comm->intra_comm != MPI_COMM_NULL)
325 smpi_comm_unuse(comm->intra_comm);
326 if(comm->leaders_comm != MPI_COMM_NULL)
327 smpi_comm_unuse(comm->leaders_comm);
328 if(comm->non_uniform_map !=NULL)
329 xbt_free(comm->non_uniform_map);
330 if(comm->leaders_map !=NULL)
331 xbt_free(comm->leaders_map);
332 if(comm->attributes !=NULL){
333 xbt_dict_cursor_t cursor = NULL;
337 xbt_dict_foreach(comm->attributes, cursor, key, value){
338 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key);
339 if(elem && elem->delete_fn)
340 elem->delete_fn(comm, atoi((const char*)key), value, &flag);
348 compare_ints (const void *a, const void *b)
350 const int *da = (const int *) a;
351 const int *db = (const int *) b;
353 return (*da > *db) - (*da < *db);
356 void smpi_comm_init_smp(MPI_Comm comm){
359 if (comm == MPI_COMM_UNINITIALIZED)
360 comm = smpi_process_comm_world();
362 int comm_size =smpi_comm_size(comm);
364 // If we are in replay - perform an ugly hack
365 // say to SimGrid that we are not in replay for a while, because we need
366 // the buffers to be copied for the following calls
367 int replaying = 0; //cache data to set it back again after
368 if(smpi_process_get_replaying()){
370 smpi_process_set_replaying(0);
373 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
374 smpi_switch_data_segment(smpi_process_index());
376 //identify neighbours in comm
377 //get the indexes of all processes sharing the same simix host
378 xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
379 int intra_comm_size = 0;
380 //only one process/node, disable SMP support and return
381 // if(intra_comm_size==1){
382 // smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
383 // //smpi_comm_set_leaders_comm(comm, comm);
384 // smpi_process_set_comm_intra(MPI_COMM_SELF);
390 int min_index=INT_MAX;//the minimum index will be the leader
391 msg_process_t process = NULL;
392 xbt_swag_foreach(process, process_list) {
394 int index = SIMIX_process_get_PID(process) -1;
396 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
398 //the process is in the comm
399 if(index < min_index)
404 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
405 MPI_Group group_intra = smpi_group_new(intra_comm_size);
408 xbt_swag_foreach(process, process_list) {
410 int index = SIMIX_process_get_PID(process) -1;
411 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
412 smpi_group_set_mapping(group_intra, index, i);
418 MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
419 //MPI_Comm shmem_comm = smpi_process_comm_intra();
420 //int intra_rank = smpi_comm_rank(shmem_comm);
423 //if(smpi_process_index()==min_index)
426 int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
427 int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
428 for(i=0; i<comm_size; i++){
432 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
434 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
435 smpi_switch_data_segment(smpi_process_index());
438 if(!comm->leaders_map){
439 comm->leaders_map= leaders_map;
441 xbt_free(leaders_map);
444 int leader_group_size = 0;
445 for(i=0; i<comm_size; i++){
447 for(j=0;j<leader_group_size; j++){
448 if(comm->leaders_map[i]==leader_list[j]){
453 leader_list[leader_group_size]=comm->leaders_map[i];
457 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
459 MPI_Group leaders_group = smpi_group_new(leader_group_size);
462 MPI_Comm leader_comm = MPI_COMM_NULL;
463 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
464 //create leader_communicator
465 for (i=0; i< leader_group_size;i++)
466 smpi_group_set_mapping(leaders_group, leader_list[i], i);
467 leader_comm = smpi_comm_new(leaders_group, NULL);
468 smpi_comm_set_leaders_comm(comm, leader_comm);
469 smpi_comm_set_intra_comm(comm, comm_intra);
471 //create intracommunicator
472 // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
474 for (i=0; i< leader_group_size;i++)
475 smpi_group_set_mapping(leaders_group, leader_list[i], i);
477 leader_comm = smpi_comm_new(leaders_group, NULL);
478 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL)
479 smpi_comm_set_leaders_comm(comm, leader_comm);
480 smpi_process_set_comm_intra(comm_intra);
485 // Are the nodes uniform ? = same number of process/node
486 int my_local_size=smpi_comm_size(comm_intra);
487 if(smpi_comm_rank(comm_intra)==0) {
488 int* non_uniform_map = xbt_malloc0(sizeof(int)*leader_group_size);
489 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
490 non_uniform_map, 1, MPI_INT, leader_comm);
491 for(i=0; i < leader_group_size; i++) {
492 if(non_uniform_map[0] != non_uniform_map[i]) {
497 if(!is_uniform && smpi_comm_is_uniform(comm)){
498 comm->non_uniform_map= non_uniform_map;
500 xbt_free(non_uniform_map);
502 comm->is_uniform=is_uniform;
504 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
506 if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
507 smpi_switch_data_segment(smpi_process_index());
509 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
511 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
512 for (i=1; i<my_local_size; i++){
513 int this=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
522 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1,
523 MPI_INT, MPI_LAND, comm);
525 if(MPI_COMM_WORLD==SMPI_UNINITIALIZED || comm==MPI_COMM_WORLD){
526 if(smpi_comm_rank(comm)==0){
527 comm->is_blocked=global_blocked;
530 comm->is_blocked=global_blocked;
532 xbt_free(leader_list);
535 smpi_process_set_replaying(1);
538 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
539 char* tmpkey=xbt_malloc(INTSIZEDCHAR);
540 sprintf(tmpkey, "%d", keyval);
541 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
544 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
547 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
548 int ret = elem->delete_fn(comm, keyval, value, &flag);
549 if(ret!=MPI_SUCCESS) return ret;
552 if(comm->attributes==NULL)
555 xbt_dict_remove(comm->attributes, (const char*)tmpkey);
560 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
561 char* tmpkey=xbt_malloc(INTSIZEDCHAR);
562 sprintf(tmpkey, "%d", keyval);
563 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
567 if(comm->attributes==NULL){
572 *(void**)attr_value = xbt_dict_get(comm->attributes, (const char*)tmpkey);
583 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
584 if(!smpi_comm_keyvals)
585 smpi_comm_keyvals = xbt_dict_new();
586 char* tmpkey=xbt_malloc(INTSIZEDCHAR);
587 sprintf(tmpkey, "%d", keyval);
588 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
593 smpi_comm_attr_get(comm, keyval, &value, &flag);
594 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
595 int ret = elem->delete_fn(comm, keyval, value, &flag);
596 if(ret!=MPI_SUCCESS) return ret;
598 if(comm->attributes==NULL)
599 comm->attributes=xbt_dict_new();
601 xbt_dict_set(comm->attributes, (const char*)tmpkey, attr_value, NULL);
606 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval, void* extra_state){
608 if(!smpi_comm_keyvals)
609 smpi_comm_keyvals = xbt_dict_new();
611 smpi_comm_key_elem value = (smpi_comm_key_elem) xbt_new0(s_smpi_mpi_comm_key_elem_t,1);
613 value->copy_fn=copy_fn;
614 value->delete_fn=delete_fn;
616 *keyval = comm_keyval_id;
617 char* tmpkey=xbt_malloc(INTSIZEDCHAR);
618 sprintf(tmpkey, "%d", *keyval);
619 xbt_dict_set(smpi_comm_keyvals,(const char*)tmpkey,(void*)value, NULL);
625 int smpi_comm_keyval_free(int* keyval){
626 smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)keyval);
629 char* tmpkey=xbt_malloc(INTSIZEDCHAR);
630 sprintf(tmpkey, "%d", *keyval);
631 xbt_dict_remove(smpi_comm_keyvals, (const char*)tmpkey);