From: pini Date: Thu, 18 Nov 2010 14:55:29 +0000 (+0000) Subject: MPI_Comm_split is back to life. X-Git-Tag: v3_5~254 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/57e7dcd49d4873f73dc0337e4f172bd169f77a44 MPI_Comm_split is back to life. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8573 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/examples/smpi/split.c b/examples/smpi/split.c index 984ee7ee00..a3bb58fd63 100644 --- a/examples/smpi/split.c +++ b/examples/smpi/split.c @@ -10,14 +10,14 @@ int main(int argc, char *argv[]) { int worldrank; -// int localrank; -// MPI_Comm localcomm; + int localrank; + MPI_Comm localcomm; + MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &worldrank); - printf("MPI_Comm_split is not implemented\n"); - //MPI_Comm_split(MPI_COMM_WORLD, worldrank % 2, worldrank, &localcomm); - //MPI_Comm_rank(localcomm, &localrank); - //printf("node with world rank %d has local rank %d\n", worldrank, localrank); + MPI_Comm_split(MPI_COMM_WORLD, worldrank % 2, worldrank, &localcomm); + MPI_Comm_rank(localcomm, &localrank); + printf("node with world rank %d has local rank %d\n", worldrank, localrank); MPI_Finalize(); return 0; } diff --git a/include/smpi/smpi.h b/include/smpi/smpi.h index 65fcb2fab5..32843b7520 100644 --- a/include/smpi/smpi.h +++ b/include/smpi/smpi.h @@ -205,6 +205,7 @@ XBT_PUBLIC(int) MPI_Comm_dup(MPI_Comm comm, MPI_Comm * newcomm); XBT_PUBLIC(int) MPI_Comm_create(MPI_Comm comm, MPI_Group group, MPI_Comm * newcomm); XBT_PUBLIC(int) MPI_Comm_free(MPI_Comm * comm); +XBT_PUBLIC(int) MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out); XBT_PUBLIC(int) MPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, @@ -299,11 +300,6 @@ XBT_PUBLIC(int) MPI_Alltoallv(void *sendbuf, int *sendcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm); -/* -TODO -XBT_PUBLIC(int) MPI_Comm_split(MPI_Comm comm, int color, int key, - MPI_Comm* comm_out); -*/ // smpi functions XBT_IMPORT_NO_EXPORT(int) smpi_simulated_main(int argc, char **argv); XBT_PUBLIC(MPI_Comm) smpi_process_comm_self(void); diff --git a/src/smpi/private.h b/src/smpi/private.h index bccb451346..cdbf7a9be4 100644 --- a/src/smpi/private.h +++ b/src/smpi/private.h @@ -85,6 +85,7 @@ void smpi_comm_destroy(MPI_Comm comm); MPI_Group smpi_comm_group(MPI_Comm comm); int smpi_comm_size(MPI_Comm comm); int smpi_comm_rank(MPI_Comm comm); +MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key); MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm); diff --git a/src/smpi/smpi_comm.c b/src/smpi/smpi_comm.c index 9416dd0ba1..70267cd79e 100644 --- a/src/smpi/smpi_comm.c +++ b/src/smpi/smpi_comm.c @@ -4,7 +4,10 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#include + #include "private.h" +#include "smpi_mpi_dt_private.h" XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi, "Logging specific to SMPI (comm)"); @@ -13,6 +16,26 @@ typedef struct s_smpi_mpi_communicator { MPI_Group group; } s_smpi_mpi_communicator_t; +static int smpi_compare_rankmap(const void *a, const void *b) +{ + const int* x = (const int*)a; + const int* y = (const int*)b; + + if (x[1] < y[1]) { + return -1; + } + if (x[1] == y[1]) { + if (x[0] < y[0]) { + return -1; + } + if (x[0] == y[0]) { + return 0; + } + return 1; + } + return 1; +} + MPI_Comm smpi_comm_new(MPI_Group group) { MPI_Comm comm; @@ -43,3 +66,80 @@ int smpi_comm_rank(MPI_Comm comm) { return smpi_group_rank(smpi_comm_group(comm), smpi_process_index()); } + +MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key) +{ + int system_tag = 666; + int index, rank, size, i, j, count, reqs; + int* sendbuf; + int* recvbuf; + int* rankmap; + MPI_Group group, group_root, group_out; + MPI_Request* requests; + + group_root = group_out = NULL; + group = smpi_comm_group(comm); + rank = smpi_comm_rank(comm); + size = smpi_comm_size(comm); + /* Gather all colors and keys on rank 0 */ + sendbuf = xbt_new(int, 2); + sendbuf[0] = color; + sendbuf[1] = key; + if(rank == 0) { + recvbuf = xbt_new(int, 2 * size); + } else { + recvbuf = NULL; + } + smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm); + xbt_free(sendbuf); + /* Do the actual job */ + if(rank == 0) { + rankmap = xbt_new(int, 2 * size); + for(i = 0; i < size; i++) { + if(recvbuf[2 * i] == MPI_UNDEFINED) { + continue; + } + count = 0; + for(j = i + 1; j < size; j++) { + if(recvbuf[2 * i] == recvbuf[2 * j]) { + recvbuf[2 * j] = MPI_UNDEFINED; + rankmap[2 * count] = j; + rankmap[2 * count + 1] = recvbuf[2 * j + 1]; + count++; + } + } + /* Add self in the group */ + recvbuf[2 * i] = MPI_UNDEFINED; + rankmap[2 * count] = i; + rankmap[2 * count + 1] = recvbuf[2 * i + 1]; + count++; + qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap); + group_out = smpi_group_new(count); + if(i == 0) { + group_root = group_out; /* Save root's group */ + } + for(j = 0; j < count; j++) { + index = smpi_group_index(group, rankmap[2 * j]); + smpi_group_set_mapping(group_out, index, j); + } + requests = xbt_new(MPI_Request, count); + reqs = 0; + for(j = 0; j < count; j++) { + if(rankmap[2 * j] != 0) { + requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm); + reqs++; + } + } + smpi_mpi_startall(reqs, requests); + smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE); + xbt_free(requests); + } + xbt_free(recvbuf); + group_out = group_root; /* exit with root's group */ + } else { + if(color != MPI_UNDEFINED) { + smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE); + } /* otherwise, exit with group_out == NULL */ + } + return group_out ? smpi_comm_new(group_out) : MPI_COMM_NULL; +} diff --git a/src/smpi/smpi_mpi.c b/src/smpi/smpi_mpi.c index 39b292669e..ff15e6a3f5 100644 --- a/src/smpi/smpi_mpi.c +++ b/src/smpi/smpi_mpi.c @@ -762,6 +762,24 @@ int MPI_Comm_free(MPI_Comm * comm) return retval; } +int MPI_Comm_split(MPI_Comm comm, int color, int key, MPI_Comm* comm_out) +{ + int retval; + + smpi_bench_end(-1, NULL); + if (comm_out == NULL) { + retval = MPI_ERR_ARG; + } else if (comm == MPI_COMM_NULL) { + retval = MPI_ERR_COMM; + } else { + printf("[%d] gives %d, %d\n", smpi_comm_rank(comm), color, key); + *comm_out = smpi_comm_split(comm, color, key); + retval = MPI_SUCCESS; + } + smpi_bench_begin(-1, NULL); + return retval; +} + int MPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst, int tag, MPI_Comm comm, MPI_Request * request) { diff --git a/src/smpi/smpi_mpi_dt.c b/src/smpi/smpi_mpi_dt.c index 9adfdb39e4..cc85e55b86 100644 --- a/src/smpi/smpi_mpi_dt.c +++ b/src/smpi/smpi_mpi_dt.c @@ -98,6 +98,9 @@ CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int); CREATE_MPI_DATATYPE(MPI_2INT, int_int); CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int); +// Internal use only +CREATE_MPI_DATATYPE(MPI_PTR, void*); + size_t smpi_datatype_size(MPI_Datatype datatype) { diff --git a/src/smpi/smpi_mpi_dt_private.h b/src/smpi/smpi_mpi_dt_private.h index 5034bb3cc5..374f8f8bc5 100644 --- a/src/smpi/smpi_mpi_dt_private.h +++ b/src/smpi/smpi_mpi_dt_private.h @@ -6,6 +6,8 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#ifndef SMPI_DT_PRIVATE_H +#define SMPI_DT_PRIVATE_H #include "private.h" @@ -27,3 +29,7 @@ * datatypes. The DT_FLAG_BASIC is held by all predefined contiguous datatypes. */ #define DT_FLAG_BASIC (DT_FLAG_PREDEFINED | DT_FLAG_CONTIGUOUS | DT_FLAG_NO_GAPS | DT_FLAG_DATA | DT_FLAG_COMMITED) + +extern MPI_Datatype MPI_PTR; + +#endif