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. */
14 #include <simgrid/s4u/host.hpp>
15 #include <src/smpi/smpi_group.hpp>
18 #include "smpi_mpi_dt_private.h"
19 #include "src/simix/smx_private.h"
20 #include "colls/colls.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)");
24 xbt_dict_t smpi_comm_keyvals = nullptr;
25 int comm_keyval_id = 0;//avoid collisions
27 /* Support for cartesian topology was added, but there are 2 other types of topology, graph et dist graph. In order to
28 * support them, we have to add a field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
30 typedef struct s_smpi_mpi_communicator {
32 MPIR_Topo_type topoType;
33 MPI_Topology topo; // to be replaced by an union
35 MPI_Comm leaders_comm;//inter-node communicator
36 MPI_Comm intra_comm;//intra-node communicator . For MPI_COMM_WORLD this can't be used, as var is global.
37 //use an intracomm stored in the process data instead
38 int* leaders_map; //who is the leader of each process
40 int* non_uniform_map; //set if smp nodes have a different number of processes allocated
41 int is_blocked;// are ranks allocated on the same smp node contiguous ?
42 xbt_dict_t attributes;
43 } s_smpi_mpi_communicator_t;
45 static int smpi_compare_rankmap(const void *a, const void *b)
47 const int* x = static_cast<const int*>(a);
48 const int* y = static_cast<const int*>(b);
65 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
69 comm = xbt_new(s_smpi_mpi_communicator_t, 1);
72 comm->topoType = MPI_INVALID_TOPO;
74 comm->intra_comm = MPI_COMM_NULL;
75 comm->leaders_comm = MPI_COMM_NULL;
77 comm->non_uniform_map = nullptr;
78 comm->leaders_map = nullptr;
80 comm->attributes=nullptr;
84 void smpi_comm_destroy(MPI_Comm comm)
86 if (comm == MPI_COMM_UNINITIALIZED)
87 comm = smpi_process_comm_world();
88 smpi_topo_destroy(comm->topo); // there's no use count on topos
89 smpi_comm_unuse(comm);
92 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
93 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
94 smpi_switch_data_segment(smpi_process_index());
96 MPI_Group cp=new simgrid::SMPI::Group(smpi_comm_group(comm));
97 (*newcomm) = smpi_comm_new(cp, smpi_comm_topo(comm));
98 int ret = MPI_SUCCESS;
100 if(comm->attributes !=nullptr){
101 (*newcomm)->attributes = xbt_dict_new_homogeneous(nullptr);
102 xbt_dict_cursor_t cursor = nullptr;
107 xbt_dict_foreach (comm->attributes, cursor, key, value_in) {
108 smpi_comm_key_elem elem =
109 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, key, sizeof(int)));
110 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
111 ret = elem->copy_fn(comm, atoi(key), nullptr, value_in, &value_out, &flag);
112 if (ret != MPI_SUCCESS) {
113 smpi_comm_destroy(*newcomm);
114 *newcomm = MPI_COMM_NULL;
115 xbt_dict_cursor_free(&cursor);
119 xbt_dict_set_ext((*newcomm)->attributes, key, sizeof(int), value_out, nullptr);
126 MPI_Group smpi_comm_group(MPI_Comm comm)
128 if (comm == MPI_COMM_UNINITIALIZED)
129 comm = smpi_process_comm_world();
133 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
134 if (comm != MPI_COMM_NULL)
139 int smpi_comm_size(MPI_Comm comm)
141 if (comm == MPI_COMM_UNINITIALIZED)
142 comm = smpi_process_comm_world();
143 return smpi_comm_group(comm)->getsize();
146 int smpi_comm_rank(MPI_Comm comm)
148 if (comm == MPI_COMM_UNINITIALIZED)
149 comm = smpi_process_comm_world();
150 return smpi_comm_group(comm)->rank(smpi_process_index());
153 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
155 if (comm == MPI_COMM_UNINITIALIZED)
156 comm = smpi_process_comm_world();
157 if(comm == MPI_COMM_WORLD) {
158 strncpy(name, "WORLD",5);
161 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
165 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
166 if (comm == MPI_COMM_UNINITIALIZED)
167 comm = smpi_process_comm_world();
168 comm->leaders_comm=leaders;
171 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
172 comm->intra_comm=leaders;
175 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
176 if (comm == MPI_COMM_UNINITIALIZED)
177 comm = smpi_process_comm_world();
178 return comm->non_uniform_map;
181 int* smpi_comm_get_leaders_map(MPI_Comm comm){
182 if (comm == MPI_COMM_UNINITIALIZED)
183 comm = smpi_process_comm_world();
184 return comm->leaders_map;
187 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
188 if (comm == MPI_COMM_UNINITIALIZED)
189 comm = smpi_process_comm_world();
190 return comm->leaders_comm;
193 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
194 if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD)
195 return smpi_process_get_comm_intra();
196 else return comm->intra_comm;
199 int smpi_comm_is_uniform(MPI_Comm comm){
200 if (comm == MPI_COMM_UNINITIALIZED)
201 comm = smpi_process_comm_world();
202 return comm->is_uniform;
205 int smpi_comm_is_blocked(MPI_Comm comm){
206 if (comm == MPI_COMM_UNINITIALIZED)
207 comm = smpi_process_comm_world();
208 return comm->is_blocked;
211 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
213 if (comm == MPI_COMM_UNINITIALIZED)
214 comm = smpi_process_comm_world();
215 int system_tag = 123;
218 MPI_Group group_root = nullptr;
219 MPI_Group group_out = nullptr;
220 MPI_Group group = smpi_comm_group(comm);
221 int rank = smpi_comm_rank(comm);
222 int size = smpi_comm_size(comm);
223 /* Gather all colors and keys on rank 0 */
224 int* 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 MPI_Group* group_snd = xbt_new(MPI_Group, size);
237 int* rankmap = xbt_new(int, 2 * size);
238 for (int i = 0; i < size; i++) {
239 if (recvbuf[2 * i] != MPI_UNDEFINED) {
241 for (int j = i + 1; j < size; j++) {
242 if(recvbuf[2 * i] == recvbuf[2 * j]) {
243 recvbuf[2 * j] = MPI_UNDEFINED;
244 rankmap[2 * count] = j;
245 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
249 /* Add self in the group */
250 recvbuf[2 * i] = MPI_UNDEFINED;
251 rankmap[2 * count] = i;
252 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
254 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
255 group_out = new simgrid::SMPI::Group(count);
257 group_root = group_out; /* Save root's group */
259 for (int j = 0; j < count; j++) {
260 int index = group->index(rankmap[2 * j]);
261 group_out->set_mapping(index, j);
263 MPI_Request* requests = xbt_new(MPI_Request, count);
265 for (int j = 0; j < count; j++) {
266 if(rankmap[2 * j] != 0) {
267 group_snd[reqs]=new simgrid::SMPI::Group(group_out);
268 requests[reqs] = smpi_mpi_isend(&(group_snd[reqs]), 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
273 group_out->destroy();
275 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
282 group_out = group_root; /* exit with root's group */
284 if(color != MPI_UNDEFINED) {
285 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
286 } /* otherwise, exit with group_out == nullptr */
288 return group_out!=nullptr ? smpi_comm_new(group_out, nullptr) : MPI_COMM_NULL;
291 void smpi_comm_use(MPI_Comm comm){
292 if (comm == MPI_COMM_UNINITIALIZED)
293 comm = smpi_process_comm_world();
298 void smpi_comm_cleanup_attributes(MPI_Comm comm){
299 if(comm->attributes !=nullptr){
300 xbt_dict_cursor_t cursor = nullptr;
304 xbt_dict_foreach (comm->attributes, cursor, key, value) {
305 smpi_comm_key_elem elem = static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null(smpi_comm_keyvals, key));
306 if (elem != nullptr && elem->delete_fn != nullptr)
307 elem->delete_fn(comm, atoi(key), value, &flag);
309 xbt_dict_free(&comm->attributes);
313 void smpi_comm_cleanup_smp(MPI_Comm comm){
314 if (comm->intra_comm != MPI_COMM_NULL)
315 smpi_comm_unuse(comm->intra_comm);
316 if (comm->leaders_comm != MPI_COMM_NULL)
317 smpi_comm_unuse(comm->leaders_comm);
318 if (comm->non_uniform_map != nullptr)
319 xbt_free(comm->non_uniform_map);
320 if (comm->leaders_map != nullptr)
321 xbt_free(comm->leaders_map);
324 void smpi_comm_unuse(MPI_Comm comm){
325 if (comm == MPI_COMM_UNINITIALIZED)
326 comm = smpi_process_comm_world();
328 comm->group->unuse();
330 if(comm->refcount==0){
331 smpi_comm_cleanup_smp(comm);
332 smpi_comm_cleanup_attributes(comm);
337 static int compare_ints (const void *a, const void *b)
339 const int *da = static_cast<const int *>(a);
340 const int *db = static_cast<const int *>(b);
342 return static_cast<int>(*da > *db) - static_cast<int>(*da < *db);
345 void smpi_comm_init_smp(MPI_Comm comm){
348 if (comm == MPI_COMM_UNINITIALIZED)
349 comm = smpi_process_comm_world();
351 int comm_size =smpi_comm_size(comm);
353 // If we are in replay - perform an ugly hack
354 // tell SimGrid we are not in replay for a while, because we need the buffers to be copied for the following calls
355 bool replaying = false; //cache data to set it back again after
356 if(smpi_process_get_replaying()){
358 smpi_process_set_replaying(false);
361 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
362 smpi_switch_data_segment(smpi_process_index());
364 //identify neighbours in comm
365 //get the indexes of all processes sharing the same simix host
366 xbt_swag_t process_list = SIMIX_host_self()->processes();
367 int intra_comm_size = 0;
369 int min_index=INT_MAX;//the minimum index will be the leader
370 smx_actor_t process = nullptr;
371 xbt_swag_foreach(process, process_list) {
372 int index = process->pid -1;
374 if(smpi_comm_group(comm)->rank(index)!=MPI_UNDEFINED){
376 //the process is in the comm
377 if(index < min_index)
382 XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
383 MPI_Group group_intra = new simgrid::SMPI::Group(intra_comm_size);
386 xbt_swag_foreach(process, process_list) {
387 int index = process->pid -1;
388 if(smpi_comm_group(comm)->rank(index)!=MPI_UNDEFINED){
389 group_intra->set_mapping(index, i);
394 MPI_Comm comm_intra = smpi_comm_new(group_intra, nullptr);
397 int * leaders_map= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
398 int * leader_list= static_cast<int*>(xbt_malloc0(sizeof(int)*comm_size));
399 for(i=0; i<comm_size; i++){
403 smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
405 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
406 smpi_switch_data_segment(smpi_process_index());
409 if(comm->leaders_map==nullptr){
410 comm->leaders_map= leaders_map;
412 xbt_free(leaders_map);
415 int leader_group_size = 0;
416 for(i=0; i<comm_size; i++){
418 for(j=0;j<leader_group_size; j++){
419 if(comm->leaders_map[i]==leader_list[j]){
424 leader_list[leader_group_size]=comm->leaders_map[i];
428 qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
430 MPI_Group leaders_group = new simgrid::SMPI::Group(leader_group_size);
432 MPI_Comm leader_comm = MPI_COMM_NULL;
433 if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
434 //create leader_communicator
435 for (i=0; i< leader_group_size;i++)
436 leaders_group->set_mapping(leader_list[i], i);
437 leader_comm = smpi_comm_new(leaders_group, nullptr);
438 smpi_comm_set_leaders_comm(comm, leader_comm);
439 smpi_comm_set_intra_comm(comm, comm_intra);
441 //create intracommunicator
443 for (i=0; i< leader_group_size;i++)
444 leaders_group->set_mapping(leader_list[i], i);
446 if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL){
447 leader_comm = smpi_comm_new(leaders_group, nullptr);
448 smpi_comm_set_leaders_comm(comm, leader_comm);
450 leader_comm=smpi_comm_get_leaders_comm(comm);
451 leaders_group->unuse();
453 smpi_process_set_comm_intra(comm_intra);
458 // Are the nodes uniform ? = same number of process/node
459 int my_local_size=smpi_comm_size(comm_intra);
460 if(smpi_comm_rank(comm_intra)==0) {
461 int* non_uniform_map = xbt_new0(int,leader_group_size);
462 smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
463 non_uniform_map, 1, MPI_INT, leader_comm);
464 for(i=0; i < leader_group_size; i++) {
465 if(non_uniform_map[0] != non_uniform_map[i]) {
470 if(is_uniform==0 && smpi_comm_is_uniform(comm)!=0){
471 comm->non_uniform_map= non_uniform_map;
473 xbt_free(non_uniform_map);
475 comm->is_uniform=is_uniform;
477 smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
479 if(smpi_privatize_global_variables){ //we need to switch as the called function may silently touch global variables
480 smpi_switch_data_segment(smpi_process_index());
482 // Are the ranks blocked ? = allocated contiguously on the SMP nodes
484 int prev=smpi_comm_group(comm)->rank(smpi_comm_group(comm_intra)->index(0));
485 for (i=1; i<my_local_size; i++){
486 int that=smpi_comm_group(comm)->rank(smpi_comm_group(comm_intra)->index(i));
495 smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1, MPI_INT, MPI_LAND, comm);
497 if(MPI_COMM_WORLD==MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD){
498 if(smpi_comm_rank(comm)==0){
499 comm->is_blocked=global_blocked;
502 comm->is_blocked=global_blocked;
504 xbt_free(leader_list);
507 smpi_process_set_replaying(true);
510 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
511 smpi_comm_key_elem elem =
512 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
515 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
516 void* value = nullptr;
518 if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
519 int ret = elem->delete_fn(comm, keyval, value, &flag);
524 if(comm->attributes==nullptr)
527 xbt_dict_remove_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
531 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
532 smpi_comm_key_elem elem =
533 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
536 if(comm->attributes==nullptr){
541 *static_cast<void**>(attr_value) =
542 xbt_dict_get_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int));
551 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
552 if(smpi_comm_keyvals==nullptr)
553 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
554 smpi_comm_key_elem elem =
555 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
559 void* value = nullptr;
560 smpi_comm_attr_get(comm, keyval, &value, &flag);
561 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
562 int ret = elem->delete_fn(comm, keyval, value, &flag);
566 if(comm->attributes==nullptr)
567 comm->attributes = xbt_dict_new_homogeneous(nullptr);
569 xbt_dict_set_ext(comm->attributes, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
573 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval,
575 if(smpi_comm_keyvals==nullptr)
576 smpi_comm_keyvals = xbt_dict_new_homogeneous(nullptr);
578 smpi_comm_key_elem value = static_cast<smpi_comm_key_elem>(xbt_new0(s_smpi_mpi_comm_key_elem_t,1));
580 value->copy_fn=copy_fn;
581 value->delete_fn=delete_fn;
583 *keyval = comm_keyval_id;
584 xbt_dict_set_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int),static_cast<void*>(value), nullptr);
589 int smpi_comm_keyval_free(int* keyval){
590 smpi_comm_key_elem elem =
591 static_cast<smpi_comm_key_elem>(xbt_dict_get_or_null_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
594 xbt_dict_remove_ext(smpi_comm_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));