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);
66 smpi_group_use(comm->group);
68 comm->topoType = MPI_INVALID_TOPO;
70 comm->intra_comm = MPI_COMM_NULL;
71 comm->leaders_comm = MPI_COMM_NULL;
73 comm->non_uniform_map = NULL;
74 comm->leaders_map = NULL;
76 comm->attributes=NULL;
80 void smpi_comm_destroy(MPI_Comm comm)
82 if (comm == MPI_COMM_UNINITIALIZED)
83 comm = smpi_process_comm_world();
84 smpi_group_unuse(comm->group);
85 smpi_topo_destroy(comm->topo); // there's no use count on topos
86 smpi_comm_unuse(comm);
89 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
90 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
91 smpi_switch_data_segment(smpi_process_index());
93 (*newcomm) = smpi_comm_new(smpi_comm_group(comm), smpi_comm_topo(comm));
94 int ret = MPI_SUCCESS;
95 //todo: faire en sorte que ça fonctionne avec un communicator dupliqué (refaire un init_smp ?)
97 /* MPI_Comm tmp=smpi_comm_get_intra_comm(comm);
98 if( tmp != MPI_COMM_NULL)
99 smpi_comm_set_intra_comm((*newcomm), smpi_comm_dup(tmp));
100 tmp=smpi_comm_get_leaders_comm(comm);
101 if( tmp != MPI_COMM_NULL)
102 smpi_comm_set_leaders_comm((*newcomm), smpi_comm_dup(tmp));
103 if(comm->non_uniform_map !=NULL){
104 (*newcomm)->non_uniform_map=
105 xbt_malloc(smpi_comm_size(comm->leaders_comm)*sizeof(int));
106 memcpy((*newcomm)->non_uniform_map,
107 comm->non_uniform_map,smpi_comm_size(comm->leaders_comm)*sizeof(int) );
109 if(comm->leaders_map !=NULL){
110 (*newcomm)->leaders_map=xbt_malloc(smpi_comm_size(comm)*sizeof(int));
111 memcpy((*newcomm)->leaders_map,
112 comm->leaders_map,smpi_comm_size(comm)*sizeof(int) );
114 if(comm->attributes !=NULL){
115 (*newcomm)->attributes=xbt_dict_new();
116 xbt_dict_cursor_t cursor = NULL;
121 xbt_dict_foreach(comm->attributes, cursor, key, value_in){
122 smpi_comm_key_elem elem =
123 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)key, sizeof(int)));
124 if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
125 ret = elem->copy_fn(comm, *key, NULL, value_in, &value_out, &flag );
126 if(ret!=MPI_SUCCESS){
127 smpi_comm_destroy(*newcomm);
128 *newcomm=MPI_COMM_NULL;
132 xbt_dict_set_ext((*newcomm)->attributes, (const char*)key, sizeof(int),value_out, NULL);
139 MPI_Group smpi_comm_group(MPI_Comm comm)
141 if (comm == MPI_COMM_UNINITIALIZED)
142 comm = smpi_process_comm_world();
146 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
147 if (comm != MPI_COMM_NULL)
152 int smpi_comm_size(MPI_Comm comm)
154 if (comm == MPI_COMM_UNINITIALIZED)
155 comm = smpi_process_comm_world();
156 return smpi_group_size(smpi_comm_group(comm));
159 int smpi_comm_rank(MPI_Comm comm)
161 if (comm == MPI_COMM_UNINITIALIZED)
162 comm = smpi_process_comm_world();
163 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
166 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
168 if (comm == MPI_COMM_UNINITIALIZED)
169 comm = smpi_process_comm_world();
170 if(comm == MPI_COMM_WORLD) {
171 strcpy(name, "WORLD");
174 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
178 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
179 if (comm == MPI_COMM_UNINITIALIZED)
180 comm = smpi_process_comm_world();
181 comm->leaders_comm=leaders;
184 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
185 comm->intra_comm=leaders;
188 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
189 if (comm == MPI_COMM_UNINITIALIZED)
190 comm = smpi_process_comm_world();
191 return comm->non_uniform_map;
194 int* smpi_comm_get_leaders_map(MPI_Comm comm){
195 if (comm == MPI_COMM_UNINITIALIZED)
196 comm = smpi_process_comm_world();
197 return comm->leaders_map;
200 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
201 if (comm == MPI_COMM_UNINITIALIZED)
202 comm = smpi_process_comm_world();
203 return comm->leaders_comm;
206 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
207 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
208 return smpi_process_get_comm_intra();
209 else return comm->intra_comm;
212 int smpi_comm_is_uniform(MPI_Comm comm){
213 if (comm == MPI_COMM_UNINITIALIZED)
214 comm = smpi_process_comm_world();
215 return comm->is_uniform;
218 int smpi_comm_is_blocked(MPI_Comm comm){
219 if (comm == MPI_COMM_UNINITIALIZED)
220 comm = smpi_process_comm_world();
221 return comm->is_blocked;
224 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
226 if (comm == MPI_COMM_UNINITIALIZED)
227 comm = smpi_process_comm_world();
228 int system_tag = 123;
229 int index, rank, size, i, j, count, reqs;
233 MPI_Group group, group_root, group_out;
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 rankmap = xbt_new(int, 2 * size);
254 for(i = 0; i < size; i++) {
255 if(recvbuf[2 * i] == MPI_UNDEFINED) {
259 for(j = i + 1; j < size; j++) {
260 if(recvbuf[2 * i] == recvbuf[2 * j]) {
261 recvbuf[2 * j] = MPI_UNDEFINED;
262 rankmap[2 * count] = j;
263 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
267 /* Add self in the group */
268 recvbuf[2 * i] = MPI_UNDEFINED;
269 rankmap[2 * count] = i;
270 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
272 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
273 group_out = smpi_group_new(count);
275 group_root = group_out; /* Save root's group */
277 for(j = 0; j < count; j++) {
278 //increment refcounter in order to avoid freeing the group too quick before copy
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 requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
290 smpi_mpi_startall(reqs, requests);
291 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
295 group_out = group_root; /* exit with root's group */
297 if(color != MPI_UNDEFINED) {
298 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
300 group_out=smpi_group_copy(group_out);
302 } /* otherwise, exit with group_out == NULL */
304 return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
307 void smpi_comm_use(MPI_Comm comm){
308 if (comm == MPI_COMM_UNINITIALIZED)
309 comm = smpi_process_comm_world();
313 void smpi_comm_unuse(MPI_Comm comm){
314 if (comm == MPI_COMM_UNINITIALIZED)
315 comm = smpi_process_comm_world();
317 if(comm->refcount==0){
318 if(comm->intra_comm != MPI_COMM_NULL)
319 smpi_comm_unuse(comm->intra_comm);
320 if(comm->leaders_comm != MPI_COMM_NULL)
321 smpi_comm_unuse(comm->leaders_comm);
322 if(comm->non_uniform_map !=NULL)
323 xbt_free(comm->non_uniform_map);
324 if(comm->leaders_map !=NULL)
325 xbt_free(comm->leaders_map);
326 if(comm->attributes !=NULL){
327 xbt_dict_cursor_t cursor = NULL;
331 xbt_dict_foreach(comm->attributes, cursor, key, value){
332 smpi_comm_key_elem elem =
333 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key));
334 if(elem && elem->delete_fn)
335 elem->delete_fn(comm, *key, value, &flag);
342 static int compare_ints (const void *a, const void *b)
344 const int *da = (const int *) a;
345 const int *db = (const int *) b;
347 return (*da > *db) - (*da < *db);
350 void smpi_comm_init_smp(MPI_Comm comm){
353 if (comm == MPI_COMM_UNINITIALIZED)
354 comm = smpi_process_comm_world();
356 int comm_size =smpi_comm_size(comm);
358 // If we are in replay - perform an ugly hack
359 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
360 int replaying = 0; //cache data to set it back again after
361 if(smpi_process_get_replaying()){
363 smpi_process_set_replaying(0);
366 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
367 smpi_switch_data_segment(smpi_process_index());
369 //identify neighbours in comm
370 //get the indexes of all processes sharing the same simix host
371 xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
372 int intra_comm_size = 0;
373 //only one process/node, disable SMP support and return
374 // if(intra_comm_size==1){
375 // smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
376 // //smpi_comm_set_leaders_comm(comm, comm);
377 // smpi_process_set_comm_intra(MPI_COMM_SELF);
381 int min_index=INT_MAX;//the minimum index will be the leader
382 smx_process_t process = NULL;
383 xbt_swag_foreach(process, process_list) {
385 int index = SIMIX_process_get_PID(process) -1;
387 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
389 //the process is in the comm
390 if(index < min_index)
395 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
396 MPI_Group group_intra = smpi_group_new(intra_comm_size);
399 xbt_swag_foreach(process, process_list) {
401 int index = SIMIX_process_get_PID(process) -1;
402 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
403 smpi_group_set_mapping(group_intra, index, i);
408 MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
409 //MPI_Comm shmem_comm = smpi_process_comm_intra();
410 //int intra_rank = smpi_comm_rank(shmem_comm);
412 //if(smpi_process_index()==min_index)
415 int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
416 int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
417 for(i=0; i<comm_size; i++){
421 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
423 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
424 smpi_switch_data_segment(smpi_process_index());
427 if(!comm->leaders_map){
428 comm->leaders_map= leaders_map;
430 xbt_free(leaders_map);
433 int leader_group_size = 0;
434 for(i=0; i<comm_size; i++){
436 for(j=0;j<leader_group_size; j++){
437 if(comm->leaders_map[i]==leader_list[j]){
442 leader_list[leader_group_size]=comm->leaders_map[i];
446 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
448 MPI_Group leaders_group = smpi_group_new(leader_group_size);
450 MPI_Comm leader_comm = MPI_COMM_NULL;
451 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
452 //create leader_communicator
453 for (i=0; i< leader_group_size;i++)
454 smpi_group_set_mapping(leaders_group, leader_list[i], i);
455 leader_comm = smpi_comm_new(leaders_group, NULL);
456 smpi_comm_set_leaders_comm(comm, leader_comm);
457 smpi_comm_set_intra_comm(comm, comm_intra);
459 //create intracommunicator
460 // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
462 for (i=0; i< leader_group_size;i++)
463 smpi_group_set_mapping(leaders_group, leader_list[i], i);
465 leader_comm = smpi_comm_new(leaders_group, NULL);
466 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL)
467 smpi_comm_set_leaders_comm(comm, leader_comm);
468 smpi_process_set_comm_intra(comm_intra);
473 // Are the nodes uniform ? = same number of process/node
474 int my_local_size=smpi_comm_size(comm_intra);
475 if(smpi_comm_rank(comm_intra)==0) {
476 int* non_uniform_map = xbt_new0(int,leader_group_size);
477 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
478 non_uniform_map, 1, MPI_INT, leader_comm);
479 for(i=0; i < leader_group_size; i++) {
480 if(non_uniform_map[0] != non_uniform_map[i]) {
485 if(!is_uniform && smpi_comm_is_uniform(comm)){
486 comm->non_uniform_map= non_uniform_map;
488 xbt_free(non_uniform_map);
490 comm->is_uniform=is_uniform;
492 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
494 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
495 smpi_switch_data_segment(smpi_process_index());
497 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
499 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
500 for (i=1; i<my_local_size; i++){
501 int that=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
510 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
512 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
513 if(smpi_comm_rank(comm)==0){
514 comm->is_blocked=global_blocked;
517 comm->is_blocked=global_blocked;
519 xbt_free(leader_list);
522 smpi_process_set_replaying(1);
525 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
526 smpi_comm_key_elem elem =
527 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
530 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
533 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
534 int ret = elem->delete_fn(comm, keyval, value, &flag);
535 if(ret!=MPI_SUCCESS) return ret;
538 if(comm->attributes==NULL)
541 xbt_dict_remove_ext(comm->attributes, (const char*)&keyval, sizeof(int));
545 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
546 smpi_comm_key_elem elem =
547 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
551 if(comm->attributes==NULL){
556 *(void**)attr_value = xbt_dict_get_ext(comm->attributes, (const char*)&keyval, sizeof(int));
565 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
566 if(!smpi_comm_keyvals)
567 smpi_comm_keyvals = xbt_dict_new();
568 smpi_comm_key_elem elem =
569 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
574 smpi_comm_attr_get(comm, keyval, &value, &flag);
575 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
576 int ret = elem->delete_fn(comm, keyval, value, &flag);
577 if(ret!=MPI_SUCCESS) return ret;
579 if(comm->attributes==NULL)
580 comm->attributes=xbt_dict_new();
582 xbt_dict_set_ext(comm->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
586 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
588 if(!smpi_comm_keyvals)
589 smpi_comm_keyvals = xbt_dict_new();
591 smpi_comm_key_elem value = (smpi_comm_key_elem) xbt_new0(s_smpi_mpi_comm_key_elem_t,1);
593 value->copy_fn=copy_fn;
594 value->delete_fn=delete_fn;
596 *keyval = comm_keyval_id;
597 xbt_dict_set_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int),(void*)value, NULL);
602 int smpi_comm_keyval_free(int* keyval){
603 smpi_comm_key_elem elem =
604 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int)));
608 xbt_dict_remove_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int));