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 = nullptr;
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 = static_cast<const int*>(a);
43 const int* y = static_cast<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 = nullptr;
73 comm->leaders_map = nullptr;
75 comm->attributes=nullptr;
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;
95 if(comm->attributes !=nullptr){
96 (*newcomm)->attributes=xbt_dict_new();
97 xbt_dict_cursor_t cursor = nullptr;
102 xbt_dict_foreach(comm->attributes, cursor, key, value_in){
103 smpi_comm_key_elem elem =
104 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(key), sizeof(int)));
105 if(elem!=nullptr && elem->copy_fn!=MPI_NULL_COPY_FN){
106 ret = elem->copy_fn(comm, *key, nullptr, value_in, &value_out, &flag );
107 if(ret!=MPI_SUCCESS){
108 smpi_comm_destroy(*newcomm);
109 *newcomm=MPI_COMM_NULL;
113 xbt_dict_set_ext((*newcomm)->attributes, reinterpret_cast<const char*>(key), sizeof(int),value_out, nullptr);
120 MPI_Group smpi_comm_group(MPI_Comm comm)
122 if (comm == MPI_COMM_UNINITIALIZED)
123 comm = smpi_process_comm_world();
127 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
128 if (comm != MPI_COMM_NULL)
133 int smpi_comm_size(MPI_Comm comm)
135 if (comm == MPI_COMM_UNINITIALIZED)
136 comm = smpi_process_comm_world();
137 return smpi_group_size(smpi_comm_group(comm));
140 int smpi_comm_rank(MPI_Comm comm)
142 if (comm == MPI_COMM_UNINITIALIZED)
143 comm = smpi_process_comm_world();
144 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
147 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
149 if (comm == MPI_COMM_UNINITIALIZED)
150 comm = smpi_process_comm_world();
151 if(comm == MPI_COMM_WORLD) {
152 strncpy(name, "WORLD",5);
155 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
159 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
160 if (comm == MPI_COMM_UNINITIALIZED)
161 comm = smpi_process_comm_world();
162 comm->leaders_comm=leaders;
165 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
166 comm->intra_comm=leaders;
169 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
170 if (comm == MPI_COMM_UNINITIALIZED)
171 comm = smpi_process_comm_world();
172 return comm->non_uniform_map;
175 int* smpi_comm_get_leaders_map(MPI_Comm comm){
176 if (comm == MPI_COMM_UNINITIALIZED)
177 comm = smpi_process_comm_world();
178 return comm->leaders_map;
181 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
182 if (comm == MPI_COMM_UNINITIALIZED)
183 comm = smpi_process_comm_world();
184 return comm->leaders_comm;
187 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
188 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
189 return smpi_process_get_comm_intra();
190 else return comm->intra_comm;
193 int smpi_comm_is_uniform(MPI_Comm comm){
194 if (comm == MPI_COMM_UNINITIALIZED)
195 comm = smpi_process_comm_world();
196 return comm->is_uniform;
199 int smpi_comm_is_blocked(MPI_Comm comm){
200 if (comm == MPI_COMM_UNINITIALIZED)
201 comm = smpi_process_comm_world();
202 return comm->is_blocked;
205 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
207 if (comm == MPI_COMM_UNINITIALIZED)
208 comm = smpi_process_comm_world();
209 int system_tag = 123;
210 int index, rank, size, i, j, count, reqs;
214 MPI_Group group, group_root, group_out;
215 MPI_Group* group_snd;
216 MPI_Request* requests;
218 group_root = nullptr;
220 group = smpi_comm_group(comm);
221 rank = smpi_comm_rank(comm);
222 size = smpi_comm_size(comm);
223 /* Gather all colors and keys on rank 0 */
224 sendbuf = xbt_new(int, 2);
228 recvbuf = xbt_new(int, 2 * size);
232 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
234 /* Do the actual job */
236 group_snd = xbt_new(MPI_Group, size);
237 rankmap = xbt_new(int, 2 * size);
238 for(i = 0; i < size; i++) {
239 if(recvbuf[2 * i] != MPI_UNDEFINED) {
242 for(j = i + 1; j < size; j++) {
243 if(recvbuf[2 * i] == recvbuf[2 * j]) {
244 recvbuf[2 * j] = MPI_UNDEFINED;
245 rankmap[2 * count] = j;
246 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
250 /* Add self in the group */
251 recvbuf[2 * i] = MPI_UNDEFINED;
252 rankmap[2 * count] = i;
253 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
255 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
256 group_out = smpi_group_new(count);
258 group_root = group_out; /* Save root's group */
260 for(j = 0; j < count; j++) {
261 index = smpi_group_index(group, rankmap[2 * j]);
262 smpi_group_set_mapping(group_out, index, j);
264 requests = xbt_new(MPI_Request, count);
266 for(j = 0; j < count; j++) {
267 if(rankmap[2 * j] != 0) {
268 group_snd[reqs]=smpi_group_copy(group_out);
269 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
274 smpi_group_destroy(group_out);
276 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
283 group_out = group_root; /* exit with root's group */
285 if(color != MPI_UNDEFINED) {
286 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
287 } /* otherwise, exit with group_out == nullptr */
289 return group_out!=nullptr ? smpi_comm_new(group_out, nullptr) : MPI_COMM_NULL;
292 void smpi_comm_use(MPI_Comm comm){
293 if (comm == MPI_COMM_UNINITIALIZED)
294 comm = smpi_process_comm_world();
295 smpi_group_use(comm->group);
299 void smpi_comm_cleanup_attributes(MPI_Comm comm){
300 if(comm->attributes !=nullptr){
301 xbt_dict_cursor_t cursor = nullptr;
305 xbt_dict_foreach(comm->attributes, cursor, key, value){
306 smpi_comm_key_elem elem =
307 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, reinterpret_cast<const char*>(key)));
308 if(elem!=nullptr && elem->delete_fn!=nullptr)
309 elem->delete_fn(comm, *key, value, &flag);
311 xbt_dict_free(&comm->attributes);
315 void smpi_comm_cleanup_smp(MPI_Comm comm){
316 if(comm->intra_comm != MPI_COMM_NULL)
317 smpi_comm_unuse(comm->intra_comm);
318 if(comm->leaders_comm != MPI_COMM_NULL)
319 smpi_comm_unuse(comm->leaders_comm);
320 if(comm->non_uniform_map !=nullptr)
321 xbt_free(comm->non_uniform_map);
322 if(comm->leaders_map !=nullptr)
323 xbt_free(comm->leaders_map);
326 void smpi_comm_unuse(MPI_Comm comm){
327 if (comm == MPI_COMM_UNINITIALIZED)
328 comm = smpi_process_comm_world();
330 smpi_group_unuse(comm->group);
332 if(comm->refcount==0){
333 smpi_comm_cleanup_smp(comm);
334 smpi_comm_cleanup_attributes(comm);
339 static int compare_ints (const void *a, const void *b)
341 const int *da = static_cast<const int *>(a);
342 const int *db = static_cast<const int *>(b);
344 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
347 void smpi_comm_init_smp(MPI_Comm comm){
350 if (comm == MPI_COMM_UNINITIALIZED)
351 comm = smpi_process_comm_world();
353 int comm_size =smpi_comm_size(comm);
355 // If we are in replay - perform an ugly hack
356 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
357 bool replaying = false; //cache data to set it back again after
358 if(smpi_process_get_replaying()){
360 smpi_process_set_replaying(false);
363 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
364 smpi_switch_data_segment(smpi_process_index());
366 //identify neighbours in comm
367 //get the indexes of all processes sharing the same simix host
368 xbt_swag_t process_list = SIMIX_host_self()->processes();
369 int intra_comm_size = 0;
371 int min_index=INT_MAX;//the minimum index will be the leader
372 smx_process_t process = nullptr;
373 xbt_swag_foreach(process, process_list) {
374 int index = SIMIX_process_get_PID(process) -1;
376 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
378 //the process is in the comm
379 if(index < min_index)
384 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
385 MPI_Group group_intra = smpi_group_new(intra_comm_size);
388 xbt_swag_foreach(process, process_list) {
389 int index = SIMIX_process_get_PID(process) -1;
390 if(smpi_group_rank(smpi_comm_group(comm), index)!=MPI_UNDEFINED){
391 smpi_group_set_mapping(group_intra, index, i);
396 MPI_Comm comm_intra = smpi_comm_new(group_intra, nullptr);
399 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
400 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
401 for(i=0; i<comm_size; i++){
405 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
407 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
408 smpi_switch_data_segment(smpi_process_index());
411 if(comm->leaders_map==nullptr){
412 comm->leaders_map= leaders_map;
414 xbt_free(leaders_map);
417 int leader_group_size = 0;
418 for(i=0; i<comm_size; i++){
420 for(j=0;j<leader_group_size; j++){
421 if(comm->leaders_map[i]==leader_list[j]){
426 leader_list[leader_group_size]=comm->leaders_map[i];
430 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
432 MPI_Group leaders_group = smpi_group_new(leader_group_size);
434 MPI_Comm leader_comm = MPI_COMM_NULL;
435 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
436 //create leader_communicator
437 for (i=0; i< leader_group_size;i++)
438 smpi_group_set_mapping(leaders_group, leader_list[i], i);
439 leader_comm = smpi_comm_new(leaders_group, nullptr);
440 smpi_comm_set_leaders_comm(comm, leader_comm);
441 smpi_comm_set_intra_comm(comm, comm_intra);
443 //create intracommunicator
445 for (i=0; i< leader_group_size;i++)
446 smpi_group_set_mapping(leaders_group, leader_list[i], i);
448 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL){
449 leader_comm = smpi_comm_new(leaders_group, nullptr);
450 smpi_comm_set_leaders_comm(comm, leader_comm);
452 leader_comm=smpi_comm_get_leaders_comm(comm);
453 smpi_group_unuse(leaders_group);
455 smpi_process_set_comm_intra(comm_intra);
460 // Are the nodes uniform ? = same number of process/node
461 int my_local_size=smpi_comm_size(comm_intra);
462 if(smpi_comm_rank(comm_intra)==0) {
463 int* non_uniform_map = xbt_new0(int,leader_group_size);
464 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
465 non_uniform_map, 1, MPI_INT, leader_comm);
466 for(i=0; i < leader_group_size; i++) {
467 if(non_uniform_map[0] != non_uniform_map[i]) {
472 if(is_uniform==0 && smpi_comm_is_uniform(comm)!=0){
473 comm->non_uniform_map= non_uniform_map;
475 xbt_free(non_uniform_map);
477 comm->is_uniform=is_uniform;
479 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
481 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
482 smpi_switch_data_segment(smpi_process_index());
484 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
486 int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
487 for (i=1; i<my_local_size; i++){
488 int that=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
497 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
499 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
500 if(smpi_comm_rank(comm)==0){
501 comm->is_blocked=global_blocked;
504 comm->is_blocked=global_blocked;
506 xbt_free(leader_list);
509 smpi_process_set_replaying(true);
512 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
513 smpi_comm_key_elem elem =
514 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
517 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
520 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
521 int ret = elem->delete_fn(comm, keyval, value, &flag);
526 if(comm->attributes==nullptr)
529 xbt_dict_remove_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
533 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
534 smpi_comm_key_elem elem =
535 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
539 if(comm->attributes==nullptr){
544 *static_cast<void**>(attr_value) = xbt_dict_get_ext(comm->attributes,
545 reinterpret_cast<const char*>(&keyval), sizeof(int));
554 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
555 if(smpi_comm_keyvals==nullptr)
556 smpi_comm_keyvals = xbt_dict_new();
557 smpi_comm_key_elem elem =
558 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
563 smpi_comm_attr_get(comm, keyval, &value, &flag);
564 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
565 int ret = elem->delete_fn(comm, keyval, value, &flag);
569 if(comm->attributes==nullptr)
570 comm->attributes=xbt_dict_new();
572 xbt_dict_set_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
576 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
578 if(smpi_comm_keyvals==nullptr)
579 smpi_comm_keyvals = xbt_dict_new();
581 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
583 value->copy_fn=copy_fn;
584 value->delete_fn=delete_fn;
586 *keyval = comm_keyval_id;
587 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
592 int smpi_comm_keyval_free(int* keyval){
593 smpi_comm_key_elem elem =
594 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
597 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));