X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/6760cb07d6b57be16928d95339d71e57c4e24f36..bf1edfe6e3a70b02a0032a3fceb7863f0117d069:/src/smpi/smpi_group.c diff --git a/src/smpi/smpi_group.c b/src/smpi/smpi_group.c index a61e374e57..a88bccad3f 100644 --- a/src/smpi/smpi_group.c +++ b/src/smpi/smpi_group.c @@ -11,20 +11,22 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, typedef struct s_smpi_mpi_group { int size; - int* rank_to_index_map; - int* index_to_rank_map; + int *rank_to_index_map; + int *index_to_rank_map; int refcount; } s_smpi_mpi_group_t; static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = { - 0, /* size */ - NULL, /* rank_to_index_map */ - NULL, /* index_to_rank_map */ - 1, /* refcount: start > 0 so that this group never gets freed */ + 0, /* size */ + NULL, /* rank_to_index_map */ + NULL, /* index_to_rank_map */ + 1, /* refcount: start > 0 so that this group never gets freed */ }; + MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY; -MPI_Group smpi_group_new(int size) { +MPI_Group smpi_group_new(int size) +{ MPI_Group group; int i, count; @@ -33,80 +35,113 @@ MPI_Group smpi_group_new(int size) { group->size = size; group->rank_to_index_map = xbt_new(int, size); group->index_to_rank_map = xbt_new(int, count); - group->refcount = 0; - for(i = 0; i < size; i++) { + group->refcount = 1; + for (i = 0; i < size; i++) { group->rank_to_index_map[i] = MPI_UNDEFINED; } - for(i = 0; i < count; i++) { + for (i = 0; i < count; i++) { group->index_to_rank_map[i] = MPI_UNDEFINED; } + return group; } -void smpi_group_destroy(MPI_Group group) { - if(smpi_group_unuse(group) <= 0) { - xbt_free(group->rank_to_index_map); - xbt_free(group->index_to_rank_map); - xbt_free(group); +MPI_Group smpi_group_copy(MPI_Group origin) +{ + MPI_Group group; + int i, count; + + count = smpi_process_count(); + group = xbt_new(s_smpi_mpi_group_t, 1); + group->size = origin->size; + group->rank_to_index_map = xbt_new(int, group->size); + group->index_to_rank_map = xbt_new(int, count); + group->refcount = 1; + for (i = 0; i < group->size; i++) { + group->rank_to_index_map[i] = origin->rank_to_index_map[i]; } + for (i = 0; i < count; i++) { + group->index_to_rank_map[i] = origin->index_to_rank_map[i]; + } + + return group; +} + + +void smpi_group_destroy(MPI_Group group) +{ + XBT_VERB("trying to free group %p, refcount = %d", group, group->refcount); + smpi_group_unuse(group); } -void smpi_group_set_mapping(MPI_Group group, int index, int rank) { - if(rank < group->size && index < smpi_process_count()) { +void smpi_group_set_mapping(MPI_Group group, int index, int rank) +{ + if (rank < group->size && index < smpi_process_count()) { group->rank_to_index_map[rank] = index; group->index_to_rank_map[index] = rank; } } -int smpi_group_index(MPI_Group group, int rank) { +int smpi_group_index(MPI_Group group, int rank) +{ int index = MPI_UNDEFINED; - if(rank < group->size) { + if (0 <= rank && rank < group->size) { index = group->rank_to_index_map[rank]; } return index; } -int smpi_group_rank(MPI_Group group, int index) { +int smpi_group_rank(MPI_Group group, int index) +{ int rank = MPI_UNDEFINED; - if(index < smpi_process_count()) { + if (index < smpi_process_count()) { rank = group->index_to_rank_map[index]; } return rank; } -int smpi_group_use(MPI_Group group) { +int smpi_group_use(MPI_Group group) +{ group->refcount++; return group->refcount; } -int smpi_group_unuse(MPI_Group group) { - group->refcount--; +int smpi_group_unuse(MPI_Group group) +{ + if (group->refcount-- <= 0) { + XBT_VERB("freeing group %p", group); + xbt_free(group->rank_to_index_map); + xbt_free(group->index_to_rank_map); + xbt_free(group); + } return group->refcount; } -int smpi_group_size(MPI_Group group) { +int smpi_group_size(MPI_Group group) +{ return group->size; } -int smpi_group_compare(MPI_Group group1, MPI_Group group2) { +int smpi_group_compare(MPI_Group group1, MPI_Group group2) +{ int result; int i, index, rank, size; result = MPI_IDENT; - if(smpi_group_size(group1) != smpi_group_size(group2)) { + if (smpi_group_size(group1) != smpi_group_size(group2)) { result = MPI_UNEQUAL; } else { size = smpi_group_size(group2); - for(i = 0; i < size; i++) { + for (i = 0; i < size; i++) { index = smpi_group_index(group1, i); rank = smpi_group_rank(group2, index); - if(rank == MPI_UNDEFINED) { + if (rank == MPI_UNDEFINED) { result = MPI_UNEQUAL; break; } - if(rank != i) { + if (rank != i) { result = MPI_SIMILAR; } }