Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix unitialized
[simgrid.git] / src / smpi / smpi_group.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
10                                 "Logging specific to SMPI (group)");
11
12 typedef struct s_smpi_mpi_group {
13   int size;
14   int *rank_to_index_map;
15   int *index_to_rank_map;
16   int refcount;
17 } s_smpi_mpi_group_t;
18
19 static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = {
20   0,                            /* size */
21   NULL,                         /* rank_to_index_map */
22   NULL,                         /* index_to_rank_map */
23   1,                            /* refcount: start > 0 so that this group never gets freed */
24 };
25
26 MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
27
28 MPI_Group smpi_group_new(int size)
29 {
30   MPI_Group group;
31   int i, count;
32
33   count = smpi_process_count();
34   group = xbt_new(s_smpi_mpi_group_t, 1);
35   group->size = size;
36   group->rank_to_index_map = xbt_new(int, size);
37   group->index_to_rank_map = xbt_new(int, count);
38   group->refcount = 1;
39   for (i = 0; i < size; i++) {
40     group->rank_to_index_map[i] = MPI_UNDEFINED;
41   }
42   for (i = 0; i < count; i++) {
43     group->index_to_rank_map[i] = MPI_UNDEFINED;
44   }
45
46   return group;
47 }
48
49 MPI_Group smpi_group_copy(MPI_Group origin)
50 {
51   MPI_Group group=origin;
52   int i, count;
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)
57     {
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);
63       group->refcount = 1;
64       for (i = 0; i < group->size; i++) {
65         group->rank_to_index_map[i] = origin->rank_to_index_map[i];
66       }
67       for (i = 0; i < count; i++) {
68         group->index_to_rank_map[i] = origin->index_to_rank_map[i];
69       }
70     }
71
72   return group;
73 }
74
75
76 void smpi_group_destroy(MPI_Group group)
77 {
78   XBT_VERB("trying to free group %p, refcount = %d", group, group->refcount);
79   if(group!= smpi_comm_group(MPI_COMM_WORLD)
80           && group != MPI_GROUP_NULL
81           && group != smpi_comm_group(MPI_COMM_SELF)
82           && group != MPI_GROUP_EMPTY)
83   smpi_group_unuse(group);
84 }
85
86 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
87 {
88   if (rank < group->size && index < smpi_process_count()) {
89     group->rank_to_index_map[rank] = index;
90     if(index!=MPI_UNDEFINED)group->index_to_rank_map[index] = rank;
91   }
92 }
93
94 int smpi_group_index(MPI_Group group, int rank)
95 {
96   int index = MPI_UNDEFINED;
97
98   if (0 <= rank && rank < group->size) {
99     index = group->rank_to_index_map[rank];
100   }
101   return index;
102 }
103
104 int smpi_group_rank(MPI_Group group, int index)
105 {
106   int rank = MPI_UNDEFINED;
107
108   if (index < smpi_process_count()) {
109     rank = group->index_to_rank_map[index];
110   }
111   return rank;
112 }
113
114 int smpi_group_use(MPI_Group group)
115 {
116   group->refcount++;
117   return group->refcount;
118 }
119
120 int smpi_group_unuse(MPI_Group group)
121 {
122   group->refcount--;
123   if (group->refcount <= 0) {
124     XBT_VERB("freeing group %p", group);
125     xbt_free(group->rank_to_index_map);
126     xbt_free(group->index_to_rank_map);
127     xbt_free(group);
128     return 0;
129   }
130   return group->refcount;
131
132 }
133
134 int smpi_group_size(MPI_Group group)
135 {
136   return group->size;
137 }
138
139 int smpi_group_compare(MPI_Group group1, MPI_Group group2)
140 {
141   int result;
142   int i, index, rank, size;
143
144   result = MPI_IDENT;
145   if (smpi_group_size(group1) != smpi_group_size(group2)) {
146     result = MPI_UNEQUAL;
147   } else {
148     size = smpi_group_size(group2);
149     for (i = 0; i < size; i++) {
150       index = smpi_group_index(group1, i);
151       rank = smpi_group_rank(group2, index);
152       if (rank == MPI_UNDEFINED) {
153         result = MPI_UNEQUAL;
154         break;
155       }
156       if (rank != i) {
157         result = MPI_SIMILAR;
158       }
159     }
160   }
161   return result;
162 }