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 int *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 count = SIMIX_process_count();
34 group = xbt_new(s_smpi_mpi_group_t, 1);
36 group->rank_to_index_map = xbt_new(int, size);
37 group->index_to_rank_map = xbt_new(int, count);
39 for (i = 0; i < size; i++) {
40 group->rank_to_index_map[i] = MPI_UNDEFINED;
42 for (i = 0; i < count; i++) {
43 group->index_to_rank_map[i] = MPI_UNDEFINED;
49 MPI_Group smpi_group_copy(MPI_Group origin)
51 MPI_Group group=origin;
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 count = smpi_process_count();
59 group = xbt_new(s_smpi_mpi_group_t, 1);
60 group->size = origin->size;
61 group->rank_to_index_map = xbt_new(int, group->size);
62 group->index_to_rank_map = xbt_new(int, count);
64 for (i = 0; i < group->size; i++) {
65 group->rank_to_index_map[i] = origin->rank_to_index_map[i];
67 for (i = 0; i < count; i++) {
68 group->index_to_rank_map[i] = origin->index_to_rank_map[i];
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)
87 if (rank < group->size && index < SIMIX_process_count()) {
88 group->rank_to_index_map[rank] = index;
89 if(index!=MPI_UNDEFINED)group->index_to_rank_map[index] = rank;
93 int smpi_group_index(MPI_Group group, int rank)
95 int index = MPI_UNDEFINED;
97 if (0 <= rank && rank < group->size) {
98 index = group->rank_to_index_map[rank];
103 int smpi_group_rank(MPI_Group group, int index)
105 int rank = MPI_UNDEFINED;
106 rank = group->index_to_rank_map[index];
110 int smpi_group_use(MPI_Group group)
113 return group->refcount;
116 int smpi_group_unuse(MPI_Group group)
119 if (group->refcount <= 0) {
120 xbt_free(group->rank_to_index_map);
121 xbt_free(group->index_to_rank_map);
125 return group->refcount;
129 int smpi_group_size(MPI_Group group)
134 int smpi_group_compare(MPI_Group group1, MPI_Group group2)
137 int i, index, rank, size;
140 if (smpi_group_size(group1) != smpi_group_size(group2)) {
141 result = MPI_UNEQUAL;
143 size = smpi_group_size(group2);
144 for (i = 0; i < size; i++) {
145 index = smpi_group_index(group1, i);
146 rank = smpi_group_rank(group2, index);
147 if (rank == MPI_UNDEFINED) {
148 result = MPI_UNEQUAL;
152 result = MPI_SIMILAR;