Logo AND Algorithmique Numérique Distribuée

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