Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] index_to_rank_map must be a xbt_dict, not a xbt_dynar
[simgrid.git] / src / smpi / smpi_group.c
1 /* Copyright (c) 2010, 2013-2014. 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   xbt_dict_t 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;
32
33   group = xbt_new(s_smpi_mpi_group_t, 1);
34   group->size = size;
35   group->rank_to_index_map = xbt_new(int, size);
36   group->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
37   group->refcount = 1;
38   for (i = 0; i < size; i++) {
39     group->rank_to_index_map[i] = MPI_UNDEFINED;
40   }
41
42   return group;
43 }
44
45 MPI_Group smpi_group_copy(MPI_Group origin)
46 {
47   MPI_Group group=origin;
48   char *key;
49   char *ptr_rank;
50   xbt_dict_cursor_t cursor = NULL;
51   
52   int i;
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       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);
62       group->refcount = 1;
63       for (i = 0; i < group->size; i++) {
64         group->rank_to_index_map[i] = origin->rank_to_index_map[i];
65       }
66
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);
69       }
70     }
71
72   return group;
73 }
74
75
76 void smpi_group_destroy(MPI_Group group)
77 {
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);
83 }
84
85 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
86 {
87   char * key;
88   int * val_rank;
89
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));
94       *val_rank = rank; 
95       asprintf(&key, "%d", index);
96       xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL);
97       free(key);
98     }
99   }
100 }
101
102 int smpi_group_index(MPI_Group group, int rank)
103 {
104   int index = MPI_UNDEFINED;
105
106   if (0 <= rank && rank < group->size) {
107     index = group->rank_to_index_map[rank];
108   }
109   return index;
110 }
111
112 int smpi_group_rank(MPI_Group group, int index)
113 {
114   int * ptr_rank;
115   char * key;
116   asprintf(&key, "%d", index);
117   ptr_rank = xbt_dict_get_or_null(group->index_to_rank_map, key);
118   if (!ptr_rank)
119     return MPI_UNDEFINED;
120   return *ptr_rank;
121 }
122
123 int smpi_group_use(MPI_Group group)
124 {
125   group->refcount++;
126   return group->refcount;
127 }
128
129 int smpi_group_unuse(MPI_Group group)
130 {
131   group->refcount--;
132   if (group->refcount <= 0) {
133     xbt_free(group->rank_to_index_map);
134     xbt_dict_free(&group->index_to_rank_map);
135     xbt_free(group);
136     return 0;
137   }
138   return group->refcount;
139
140 }
141
142 int smpi_group_size(MPI_Group group)
143 {
144   return group->size;
145 }
146
147 int smpi_group_compare(MPI_Group group1, MPI_Group group2)
148 {
149   int result;
150   int i, index, rank, size;
151
152   result = MPI_IDENT;
153   if (smpi_group_size(group1) != smpi_group_size(group2)) {
154     result = MPI_UNEQUAL;
155   } else {
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;
162         break;
163       }
164       if (rank != i) {
165         result = MPI_SIMILAR;
166       }
167     }
168   }
169   return result;
170 }
171
172 int smpi_group_incl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup)
173 {
174   int i=0, index=0;
175   if (n == 0) {
176     *newgroup = MPI_GROUP_EMPTY;
177   } else if (n == smpi_group_size(group)) {
178     *newgroup = 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);
184   } else {
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);
189     }
190   }
191   return MPI_SUCCESS;
192 }