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_isend_init(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
292 smpi_group_destroy(group_out);
294 smpi_mpi_startall(reqs, requests);
295 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);
305 } /* otherwise, exit with group_out == NULL */
307 return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
310 void smpi_comm_use(MPI_Comm comm){
311 if (comm == MPI_COMM_UNINITIALIZED)
312 comm = smpi_process_comm_world();
313 smpi_group_use(comm->group);
317 void smpi_comm_unuse(MPI_Comm comm){
318 if (comm == MPI_COMM_UNINITIALIZED)
319 comm = smpi_process_comm_world();
321 smpi_group_unuse(comm->group);
322 if(comm->refcount==0){
323 if(comm->intra_comm != MPI_COMM_NULL)
324 smpi_comm_unuse(comm->intra_comm);
325 if(comm->leaders_comm != MPI_COMM_NULL)
326 smpi_comm_unuse(comm->leaders_comm);
327 if(comm->non_uniform_map !=NULL)
328 xbt_free(comm->non_uniform_map);
329 if(comm->leaders_map !=NULL)
330 xbt_free(comm->leaders_map);
331 if(comm->attributes !=NULL){
332 xbt_dict_cursor_t cursor = NULL;
336 xbt_dict_foreach(comm->attributes, cursor, key, value){
337 smpi_comm_key_elem elem =
338 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key));
339 if(elem && elem->delete_fn)
340 elem->delete_fn(comm, *key, value, &flag);
347 static int compare_ints (const void *a, const void *b)
349 const int *da = (const int *) a;
350 const int *db = (const int *) b;
352 return (*da > *db) - (*da < *db);
355 void smpi_comm_init_smp(MPI_Comm comm){
358 if (comm == MPI_COMM_UNINITIALIZED)
359 comm = smpi_process_comm_world();
361 int comm_size =smpi_comm_size(comm);
363 // If we are in replay - perform an ugly hack
364 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
365 int replaying = 0; //cache data to set it back again after
366 if(smpi_process_get_replaying()){
368 smpi_process_set_replaying(0);
371 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
372 smpi_switch_data_segment(smpi_process_index());
374 //identify neighbours in comm
375 //get the indexes of all processes sharing the same simix host
376 xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
377 int intra_comm_size = 0;
378 //only one process/node, disable SMP support and return
379 // if(intra_comm_size==1){
380 // smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
381 // //smpi_comm_set_leaders_comm(comm, comm);
382 // smpi_process_set_comm_intra(MPI_COMM_SELF);
386 int min_index=INT_MAX;//the minimum index will be the leader
387 smx_process_t process = NULL;
388 xbt_swag_foreach(process, process_list) {
390 int index = SIMIX_process_get_PID(process) -1;
392 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
394 //the process is in the comm
395 if(index < min_index)
400 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
401 MPI_Group group_intra = smpi_group_new(intra_comm_size);
404 xbt_swag_foreach(process, process_list) {
406 int index = SIMIX_process_get_PID(process) -1;
407 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
408 smpi_group_set_mapping(group_intra, index, i);
413 MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
414 //MPI_Comm shmem_comm = smpi_process_comm_intra();
415 //int intra_rank = smpi_comm_rank(shmem_comm);
417 //if(smpi_process_index()==min_index)
420 int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
421 int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
422 for(i=0; i<comm_size; i++){
426 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
428 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
429 smpi_switch_data_segment(smpi_process_index());
432 if(!comm->leaders_map){
433 comm->leaders_map= leaders_map;
435 xbt_free(leaders_map);
438 int leader_group_size = 0;
439 for(i=0; i<comm_size; i++){
441 for(j=0;j<leader_group_size; j++){
442 if(comm->leaders_map[i]==leader_list[j]){
447 leader_list[leader_group_size]=comm->leaders_map[i];
451 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
453 MPI_Group leaders_group = smpi_group_new(leader_group_size);
455 MPI_Comm leader_comm = MPI_COMM_NULL;
456 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
457 //create leader_communicator
458 for (i=0; i< leader_group_size;i++)
459 smpi_group_set_mapping(leaders_group, leader_list[i], i);
460 leader_comm = smpi_comm_new(leaders_group, NULL);
461 smpi_comm_set_leaders_comm(comm, leader_comm);
462 smpi_comm_set_intra_comm(comm, comm_intra);
464 //create intracommunicator
465 // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
467 for (i=0; i< leader_group_size;i++)
468 smpi_group_set_mapping(leaders_group, leader_list[i], i);
470 leader_comm = smpi_comm_new(leaders_group, NULL);
471 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL)
472 smpi_comm_set_leaders_comm(comm, leader_comm);
473 smpi_process_set_comm_intra(comm_intra);
478 // Are the nodes uniform ? = same number of process/node
479 int my_local_size=smpi_comm_size(comm_intra);
480 if(smpi_comm_rank(comm_intra)==0) {
481 int* non_uniform_map = xbt_new0(int,leader_group_size);
482 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
483 non_uniform_map, 1, MPI_INT, leader_comm);
484 for(i=0; i < leader_group_size; i++) {
485 if(non_uniform_map[0] != non_uniform_map[i]) {
490 if(!is_uniform && smpi_comm_is_uniform(comm)){
491 comm->non_uniform_map= non_uniform_map;
493 xbt_free(non_uniform_map);
495 comm->is_uniform=is_uniform;
497 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
499 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
500 smpi_switch_data_segment(smpi_process_index());
502 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
504 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
505 for (i=1; i<my_local_size; i++){
506 int that=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
515 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
517 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
518 if(smpi_comm_rank(comm)==0){
519 comm->is_blocked=global_blocked;
522 comm->is_blocked=global_blocked;
524 xbt_free(leader_list);
527 smpi_process_set_replaying(1);
530 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
531 smpi_comm_key_elem elem =
532 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
535 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
538 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
539 int ret = elem->delete_fn(comm, keyval, value, &flag);
540 if(ret!=MPI_SUCCESS) return ret;
543 if(comm->attributes==NULL)
546 xbt_dict_remove_ext(comm->attributes, (const char*)&keyval, sizeof(int));
550 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
551 smpi_comm_key_elem elem =
552 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
556 if(comm->attributes==NULL){
561 *(void**)attr_value = xbt_dict_get_ext(comm->attributes, (const char*)&keyval, sizeof(int));
570 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
571 if(!smpi_comm_keyvals)
572 smpi_comm_keyvals = xbt_dict_new();
573 smpi_comm_key_elem elem =
574 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)&keyval, sizeof(int)));
579 smpi_comm_attr_get(comm, keyval, &value, &flag);
580 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
581 int ret = elem->delete_fn(comm, keyval, value, &flag);
582 if(ret!=MPI_SUCCESS) return ret;
584 if(comm->attributes==NULL)
585 comm->attributes=xbt_dict_new();
587 xbt_dict_set_ext(comm->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
591 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
593 if(!smpi_comm_keyvals)
594 smpi_comm_keyvals = xbt_dict_new();
596 smpi_comm_key_elem value = (smpi_comm_key_elem) xbt_new0(s_smpi_mpi_comm_key_elem_t,1);
598 value->copy_fn=copy_fn;
599 value->delete_fn=delete_fn;
601 *keyval = comm_keyval_id;
602 xbt_dict_set_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int),(void*)value, NULL);
607 int smpi_comm_keyval_free(int* keyval){
608 smpi_comm_key_elem elem =
609 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int)));
613 xbt_dict_remove_ext(smpi_comm_keyvals, (const char*)keyval, sizeof(int));