Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
db48e0fc149a5ab5829cf95d55f82c905c8d5a35
[simgrid.git] / src / smpi / smpi_comm.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 <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 typedef struct s_smpi_mpi_communicator {
16   MPI_Group group;
17   int refcount;
18 } s_smpi_mpi_communicator_t;
19
20 static int smpi_compare_rankmap(const void *a, const void *b)
21 {
22   const int* x = (const int*)a;
23   const int* y = (const int*)b;
24
25   if (x[1] < y[1]) {
26     return -1;
27   }
28   if (x[1] == y[1]) {
29     if (x[0] < y[0]) {
30       return -1;
31     }
32     if (x[0] == y[0]) {
33       return 0;
34     }
35     return 1;
36   }
37   return 1;
38 }
39
40 MPI_Comm smpi_comm_new(MPI_Group group)
41 {
42   MPI_Comm comm;
43
44   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
45   comm->group = group;
46   smpi_group_use(comm->group);
47   smpi_comm_use(comm);
48   return comm;
49 }
50
51 void smpi_comm_destroy(MPI_Comm comm)
52 {
53   smpi_group_unuse(comm->group);
54   smpi_comm_unuse(comm);
55 }
56
57 MPI_Group smpi_comm_group(MPI_Comm comm)
58 {
59   return comm->group;
60 }
61
62 int smpi_comm_size(MPI_Comm comm)
63 {
64   return smpi_group_size(smpi_comm_group(comm));
65 }
66
67 int smpi_comm_rank(MPI_Comm comm)
68 {
69   return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
70 }
71
72 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
73 {
74   if(comm == MPI_COMM_WORLD) {
75     strcpy(name, "WORLD");
76     *len = 5;
77   } else {
78     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
79   }
80 }
81
82 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
83 {
84   int system_tag = 123;
85   int index, rank, size, i, j, count, reqs;
86   int* sendbuf;
87   int* recvbuf;
88   int* rankmap;
89   MPI_Group group, group_root, group_out;
90   MPI_Request* requests;
91
92   group_root = group_out = NULL;
93   group = smpi_comm_group(comm);
94   rank = smpi_comm_rank(comm);
95   size = smpi_comm_size(comm);
96   /* Gather all colors and keys on rank 0 */
97   sendbuf = xbt_new(int, 2);
98   sendbuf[0] = color;
99   sendbuf[1] = key;
100   if(rank == 0) {
101     recvbuf = xbt_new(int, 2 * size);
102   } else {
103     recvbuf = NULL;
104   }
105   smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
106   xbt_free(sendbuf);
107   /* Do the actual job */
108   if(rank == 0) {
109     rankmap = xbt_new(int, 2 * size);
110     for(i = 0; i < size; i++) {
111       if(recvbuf[2 * i] == MPI_UNDEFINED) {
112         continue;
113       }
114       count = 0;
115       for(j = i + 1; j < size; j++)  {
116         if(recvbuf[2 * i] == recvbuf[2 * j]) {
117           recvbuf[2 * j] = MPI_UNDEFINED;
118           rankmap[2 * count] = j;
119           rankmap[2 * count + 1] = recvbuf[2 * j + 1];
120           count++;
121         }
122       }
123       /* Add self in the group */
124       recvbuf[2 * i] = MPI_UNDEFINED;
125       rankmap[2 * count] = i;
126       rankmap[2 * count + 1] = recvbuf[2 * i + 1];
127       count++;
128       qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
129       group_out = smpi_group_new(count);
130       if(i == 0) {
131         group_root = group_out; /* Save root's group */
132       }
133       for(j = 0; j < count; j++) {
134         index = smpi_group_index(group, rankmap[2 * j]);
135         smpi_group_set_mapping(group_out, index, j);
136       }
137       requests = xbt_new(MPI_Request, count);
138       reqs = 0;
139       for(j = 0; j < count; j++) {
140         if(rankmap[2 * j] != 0) {
141           requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
142           reqs++;
143         }
144       }
145       smpi_mpi_startall(reqs, requests);
146       smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
147       xbt_free(requests);
148     }
149     xbt_free(recvbuf);
150     group_out = group_root; /* exit with root's group */
151   } else {
152     if(color != MPI_UNDEFINED) {
153       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
154     } /* otherwise, exit with group_out == NULL */
155   }
156   if(group_out)smpi_group_use(group_out);
157   return group_out ? smpi_comm_new(group_out) : MPI_COMM_NULL;
158 }
159
160 void smpi_comm_use(MPI_Comm comm){
161   comm->refcount++;
162   smpi_group_use(comm->group);
163 }
164
165 void smpi_comm_unuse(MPI_Comm comm){
166   comm->refcount--;
167   smpi_group_unuse(comm->group);
168   if(comm->refcount==0)
169     xbt_free(comm);
170 }