1 /* Copyright (c) 2010, 2013-2014. 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. */
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
10 "Logging specific to SMPI (group)");
12 typedef struct s_smpi_mpi_group {
14 int *rank_to_index_map;
15 xbt_dict_t index_to_rank_map;
19 static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = {
21 NULL, /* rank_to_index_map */
22 NULL, /* index_to_rank_map */
23 1, /* refcount: start > 0 so that this group never gets freed */
26 MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
28 MPI_Group smpi_group_new(int size)
33 group = xbt_new(s_smpi_mpi_group_t, 1);
35 group->rank_to_index_map = xbt_new(int, size);
36 group->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
38 for (i = 0; i < size; i++) {
39 group->rank_to_index_map[i] = MPI_UNDEFINED;
45 MPI_Group smpi_group_copy(MPI_Group origin)
47 MPI_Group group=origin;
50 xbt_dict_cursor_t cursor = NULL;
53 if(origin!= smpi_comm_group(MPI_COMM_WORLD)
54 && origin != MPI_GROUP_NULL
55 && origin != smpi_comm_group(MPI_COMM_SELF)
56 && origin != MPI_GROUP_EMPTY)
58 group = xbt_new(s_smpi_mpi_group_t, 1);
59 group->size = origin->size;
60 group->rank_to_index_map = xbt_new(int, group->size);
61 group->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
63 for (i = 0; i < group->size; i++) {
64 group->rank_to_index_map[i] = origin->rank_to_index_map[i];
67 xbt_dict_foreach(origin->index_to_rank_map, cursor, key, ptr_rank) {
68 xbt_dict_set(group->index_to_rank_map, key, ptr_rank, NULL);
76 void smpi_group_destroy(MPI_Group group)
78 if(group!= smpi_comm_group(MPI_COMM_WORLD)
79 && group != MPI_GROUP_NULL
80 && group != smpi_comm_group(MPI_COMM_SELF)
81 && group != MPI_GROUP_EMPTY)
82 smpi_group_unuse(group);
85 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
90 if (rank < group->size) {
91 group->rank_to_index_map[rank] = index;
92 if (index!=MPI_UNDEFINED ) {
93 val_rank = (int *) malloc(sizeof(int));
95 asprintf(&key, "%d", index);
96 xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL);
102 int smpi_group_index(MPI_Group group, int rank)
104 int index = MPI_UNDEFINED;
106 if (0 <= rank && rank < group->size) {
107 index = group->rank_to_index_map[rank];
112 int smpi_group_rank(MPI_Group group, int index)
116 asprintf(&key, "%d", index);
117 ptr_rank = xbt_dict_get_or_null(group->index_to_rank_map, key);
119 return MPI_UNDEFINED;
123 int smpi_group_use(MPI_Group group)
126 return group->refcount;
129 int smpi_group_unuse(MPI_Group group)
132 if (group->refcount <= 0) {
133 xbt_free(group->rank_to_index_map);
134 xbt_dict_free(&group->index_to_rank_map);
138 return group->refcount;
142 int smpi_group_size(MPI_Group group)
147 int smpi_group_compare(MPI_Group group1, MPI_Group group2)
150 int i, index, rank, size;
153 if (smpi_group_size(group1) != smpi_group_size(group2)) {
154 result = MPI_UNEQUAL;
156 size = smpi_group_size(group2);
157 for (i = 0; i < size; i++) {
158 index = smpi_group_index(group1, i);
159 rank = smpi_group_rank(group2, index);
160 if (rank == MPI_UNDEFINED) {
161 result = MPI_UNEQUAL;
165 result = MPI_SIMILAR;
172 int smpi_group_incl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup)
176 *newgroup = MPI_GROUP_EMPTY;
177 } else if (n == smpi_group_size(group)) {
179 if(group!= smpi_comm_group(MPI_COMM_WORLD)
180 && group != MPI_GROUP_NULL
181 && group != smpi_comm_group(MPI_COMM_SELF)
182 && group != MPI_GROUP_EMPTY)
183 smpi_group_use(group);
185 *newgroup = smpi_group_new(n);
186 for (i = 0; i < n; i++) {
187 index = smpi_group_index(group, ranks[i]);
188 smpi_group_set_mapping(*newgroup, index, i);