1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
10 #include "smpi_mpi_dt_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
13 "Logging specific to SMPI (comm)");
15 typedef struct s_smpi_mpi_communicator {
17 } s_smpi_mpi_communicator_t;
19 static int smpi_compare_rankmap(const void *a, const void *b)
21 const int* x = (const int*)a;
22 const int* y = (const int*)b;
39 MPI_Comm smpi_comm_new(MPI_Group group)
43 comm = xbt_new(s_smpi_mpi_communicator_t, 1);
45 smpi_group_use(comm->group);
49 void smpi_comm_destroy(MPI_Comm comm)
51 smpi_group_destroy(comm->group);
55 MPI_Group smpi_comm_group(MPI_Comm comm)
60 int smpi_comm_size(MPI_Comm comm)
62 return smpi_group_size(smpi_comm_group(comm));
65 int smpi_comm_rank(MPI_Comm comm)
67 return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
70 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
72 if(comm == MPI_COMM_WORLD) {
73 strcpy(name, "WORLD");
76 *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
80 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
83 int index, rank, size, i, j, count, reqs;
87 MPI_Group group, group_root, group_out;
88 MPI_Request* requests;
90 group_root = group_out = NULL;
91 group = smpi_comm_group(comm);
92 rank = smpi_comm_rank(comm);
93 size = smpi_comm_size(comm);
94 /* Gather all colors and keys on rank 0 */
95 sendbuf = xbt_new(int, 2);
99 recvbuf = xbt_new(int, 2 * size);
103 smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
105 /* Do the actual job */
107 rankmap = xbt_new(int, 2 * size);
108 for(i = 0; i < size; i++) {
109 if(recvbuf[2 * i] == MPI_UNDEFINED) {
113 for(j = i + 1; j < size; j++) {
114 if(recvbuf[2 * i] == recvbuf[2 * j]) {
115 recvbuf[2 * j] = MPI_UNDEFINED;
116 rankmap[2 * count] = j;
117 rankmap[2 * count + 1] = recvbuf[2 * j + 1];
121 /* Add self in the group */
122 recvbuf[2 * i] = MPI_UNDEFINED;
123 rankmap[2 * count] = i;
124 rankmap[2 * count + 1] = recvbuf[2 * i + 1];
126 qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
127 group_out = smpi_group_new(count);
129 group_root = group_out; /* Save root's group */
131 for(j = 0; j < count; j++) {
132 index = smpi_group_index(group, rankmap[2 * j]);
133 smpi_group_set_mapping(group_out, index, j);
135 requests = xbt_new(MPI_Request, count);
137 for(j = 0; j < count; j++) {
138 if(rankmap[2 * j] != 0) {
139 requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
143 smpi_mpi_startall(reqs, requests);
144 smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
148 group_out = group_root; /* exit with root's group */
150 if(color != MPI_UNDEFINED) {
151 smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
152 } /* otherwise, exit with group_out == NULL */
154 return group_out ? smpi_comm_new(group_out) : MPI_COMM_NULL;