Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix comm determinism detection mechanisms
[simgrid.git] / src / smpi / smpi_comm.c
1 /* Copyright (c) 2010-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 <stdlib.h>
8
9 #include "private.h"
10 #include "smpi_mpi_dt_private.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
13                                 "Logging specific to SMPI (comm)");
14
15
16
17 /* Support for cartesian topology was added, but there are 2 other types of
18  * topology, graph et dist graph. In order to support them, we have to add a
19  * field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
20
21 typedef struct s_smpi_mpi_communicator {
22   MPI_Group group;
23   MPIR_Topo_type topoType; 
24   MPI_Topology topo; // to be replaced by an union
25   int refcount;
26 } s_smpi_mpi_communicator_t;
27
28 static int smpi_compare_rankmap(const void *a, const void *b)
29 {
30   const int* x = (const int*)a;
31   const int* y = (const int*)b;
32
33   if (x[1] < y[1]) {
34     return -1;
35   }
36   if (x[1] == y[1]) {
37     if (x[0] < y[0]) {
38       return -1;
39     }
40     if (x[0] == y[0]) {
41       return 0;
42     }
43     return 1;
44   }
45   return 1;
46 }
47
48 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
49 {
50   MPI_Comm comm;
51
52   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
53   comm->group = group;
54   smpi_group_use(comm->group);
55   comm->refcount=1;
56   comm->topo = topo;
57   return comm;
58 }
59
60 void smpi_comm_destroy(MPI_Comm comm)
61 {
62   if (comm == MPI_COMM_UNINITIALIZED)
63     comm = smpi_process_comm_world();
64   smpi_group_unuse(comm->group);
65   smpi_topo_destroy(comm->topo); // there's no use count on topos
66   smpi_comm_unuse(comm);
67 }
68
69 MPI_Group smpi_comm_group(MPI_Comm comm)
70 {
71   if (comm == MPI_COMM_UNINITIALIZED)
72     comm = smpi_process_comm_world();
73
74   return comm->group;
75 }
76
77 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
78   if (comm != MPI_COMM_NULL)
79     return comm->topo;
80   return NULL;
81 }
82
83 int smpi_comm_size(MPI_Comm comm)
84 {
85   if (comm == MPI_COMM_UNINITIALIZED)
86     comm = smpi_process_comm_world();
87
88   return smpi_group_size(smpi_comm_group(comm));
89 }
90
91 int smpi_comm_rank(MPI_Comm comm)
92 {
93   if (comm == MPI_COMM_UNINITIALIZED)
94     comm = smpi_process_comm_world();
95   return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
96 }
97
98 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
99 {
100   if (comm == MPI_COMM_UNINITIALIZED)
101     comm = smpi_process_comm_world();
102   if(comm == MPI_COMM_WORLD) {
103     strcpy(name, "WORLD");
104     *len = 5;
105   } else {
106     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
107   }
108 }
109
110 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
111 {
112   if (comm == MPI_COMM_UNINITIALIZED)
113     comm = smpi_process_comm_world();
114   int system_tag = 123;
115   int index, rank, size, i, j, count, reqs;
116   int* sendbuf;
117   int* recvbuf;
118   int* rankmap;
119   MPI_Group group, group_root, group_out;
120   MPI_Request* requests;
121
122   group_root = group_out = NULL;
123   group = smpi_comm_group(comm);
124   rank = smpi_comm_rank(comm);
125   size = smpi_comm_size(comm);
126   /* Gather all colors and keys on rank 0 */
127   sendbuf = xbt_new(int, 2);
128   sendbuf[0] = color;
129   sendbuf[1] = key;
130   if(rank == 0) {
131     recvbuf = xbt_new(int, 2 * size);
132   } else {
133     recvbuf = NULL;
134   }
135   smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
136   xbt_free(sendbuf);
137   /* Do the actual job */
138   if(rank == 0) {
139     rankmap = xbt_new(int, 2 * size);
140     for(i = 0; i < size; i++) {
141       if(recvbuf[2 * i] == MPI_UNDEFINED) {
142         continue;
143       }
144       count = 0;
145       for(j = i + 1; j < size; j++)  {
146         if(recvbuf[2 * i] == recvbuf[2 * j]) {
147           recvbuf[2 * j] = MPI_UNDEFINED;
148           rankmap[2 * count] = j;
149           rankmap[2 * count + 1] = recvbuf[2 * j + 1];
150           count++;
151         }
152       }
153       /* Add self in the group */
154       recvbuf[2 * i] = MPI_UNDEFINED;
155       rankmap[2 * count] = i;
156       rankmap[2 * count + 1] = recvbuf[2 * i + 1];
157       count++;
158       qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
159       group_out = smpi_group_new(count);
160       if(i == 0) {
161         group_root = group_out; /* Save root's group */
162       }
163       for(j = 0; j < count; j++) {
164         //increment refcounter in order to avoid freeing the group too quick before copy
165         index = smpi_group_index(group, rankmap[2 * j]);
166         smpi_group_set_mapping(group_out, index, j);
167       }
168       requests = xbt_new(MPI_Request, count);
169       reqs = 0;
170       for(j = 0; j < count; j++) {
171         if(rankmap[2 * j] != 0) {
172           requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
173           reqs++;
174         }
175       }
176       smpi_mpi_startall(reqs, requests);
177       smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
178       xbt_free(requests);
179     }
180     xbt_free(recvbuf);
181     group_out = group_root; /* exit with root's group */
182   } else {
183     if(color != MPI_UNDEFINED) {
184       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
185       if(group_out){
186         group_out=smpi_group_copy(group_out);
187       }
188     } /* otherwise, exit with group_out == NULL */
189   }
190   return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
191 }
192
193 void smpi_comm_use(MPI_Comm comm){
194   if (comm == MPI_COMM_UNINITIALIZED)
195     comm = smpi_process_comm_world();
196   comm->refcount++;
197 }
198
199 void smpi_comm_unuse(MPI_Comm comm){
200   if (comm == MPI_COMM_UNINITIALIZED)
201     comm = smpi_process_comm_world();
202   comm->refcount--;
203   if(comm->refcount==0)
204     xbt_free(comm);
205 }
206