Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Comm_split is back to life.
authorpini <pini@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 18 Nov 2010 14:55:29 +0000 (14:55 +0000)
committerpini <pini@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 18 Nov 2010 14:55:29 +0000 (14:55 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@8573 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/smpi/split.c
include/smpi/smpi.h
src/smpi/private.h
src/smpi/smpi_comm.c
src/smpi/smpi_mpi.c
src/smpi/smpi_mpi_dt.c
src/smpi/smpi_mpi_dt_private.h

index 984ee7e..a3bb58f 100644 (file)
 int main(int argc, char *argv[])
 {
   int worldrank;
 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);
   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;
 }
   MPI_Finalize();
   return 0;
 }
index 65fcb2f..32843b7 100644 (file)
@@ -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_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,
 
 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);
 
                               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);
 // smpi functions
 XBT_IMPORT_NO_EXPORT(int) smpi_simulated_main(int argc, char **argv);
 XBT_PUBLIC(MPI_Comm) smpi_process_comm_self(void);
index bccb451..cdbf7a9 100644 (file)
@@ -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_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);
 
 MPI_Request smpi_mpi_send_init(void *buf, int count, MPI_Datatype datatype,
                                int dst, int tag, MPI_Comm comm);
index 9416dd0..70267cd 100644 (file)
@@ -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. */
 
 /* 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 <stdlib.h>
+
 #include "private.h"
 #include "private.h"
+#include "smpi_mpi_dt_private.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
                                 "Logging specific to SMPI (comm)");
 
 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;
 
   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;
 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());
 }
 {
   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;
+}
index 39b2926..ff15e6a 100644 (file)
@@ -762,6 +762,24 @@ int MPI_Comm_free(MPI_Comm * comm)
   return retval;
 }
 
   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)
 {
 int MPI_Send_init(void *buf, int count, MPI_Datatype datatype, int dst,
                   int tag, MPI_Comm comm, MPI_Request * request)
 {
index 9adfdb3..cc85e55 100644 (file)
@@ -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);
 
 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)
 {
 
 size_t smpi_datatype_size(MPI_Datatype datatype)
 {
index 5034bb3..374f8f8 100644 (file)
@@ -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. */
 
 /* 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"
 
 
 #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)
  * 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