Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_comm.c
index c189c4d..d462877 100644 (file)
@@ -1,8 +1,8 @@
-/* Copyright (c) 2010. The SimGrid Team.
+/* Copyright (c) 2010-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* 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. */
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <stdlib.h>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
                                 "Logging specific to SMPI (comm)");
 
+
+
+/* Support for cartesian topology was added, but there are 2 other types of
+ * topology, graph et dist graph. In order to support them, we have to add a
+ * field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
+
 typedef struct s_smpi_mpi_communicator {
   MPI_Group group;
+  MPIR_Topo_type topoType; 
+  MPI_Topology topo; // to be replaced by an union
+  int refcount;
 } s_smpi_mpi_communicator_t;
 
 static int smpi_compare_rankmap(const void *a, const void *b)
@@ -36,39 +45,60 @@ static int smpi_compare_rankmap(const void *a, const void *b)
   return 1;
 }
 
-MPI_Comm smpi_comm_new(MPI_Group group)
+MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
 {
   MPI_Comm comm;
 
   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
   comm->group = group;
   smpi_group_use(comm->group);
+  comm->refcount=1;
+  comm->topo = topo;
   return comm;
 }
 
 void smpi_comm_destroy(MPI_Comm comm)
 {
-  smpi_group_destroy(comm->group);
-  xbt_free(comm);
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+  smpi_group_unuse(comm->group);
+  smpi_topo_destroy(comm->topo); // there's no use count on topos
+  smpi_comm_unuse(comm);
 }
 
 MPI_Group smpi_comm_group(MPI_Comm comm)
 {
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+
   return comm->group;
 }
 
+MPI_Topology smpi_comm_topo(MPI_Comm comm) {
+  if (comm != MPI_COMM_NULL)
+    return comm->topo;
+  return NULL;
+}
+
 int smpi_comm_size(MPI_Comm comm)
 {
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+
   return smpi_group_size(smpi_comm_group(comm));
 }
 
 int smpi_comm_rank(MPI_Comm comm)
 {
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
   return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
 }
 
 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
 {
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
   if(comm == MPI_COMM_WORLD) {
     strcpy(name, "WORLD");
     *len = 5;
@@ -79,7 +109,9 @@ void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
 
 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
 {
-  int system_tag = 666;
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+  int system_tag = 123;
   int index, rank, size, i, j, count, reqs;
   int* sendbuf;
   int* recvbuf;
@@ -129,6 +161,7 @@ MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
         group_root = group_out; /* Save root's group */
       }
       for(j = 0; j < count; j++) {
+        //increment refcounter in order to avoid freeing the group too quick before copy
         index = smpi_group_index(group, rankmap[2 * j]);
         smpi_group_set_mapping(group_out, index, j);
       }
@@ -149,7 +182,25 @@ MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
   } else {
     if(color != MPI_UNDEFINED) {
       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
+      if(group_out){
+        group_out=smpi_group_copy(group_out);
+      }
     } /* otherwise, exit with group_out == NULL */
   }
-  return group_out ? smpi_comm_new(group_out) : MPI_COMM_NULL;
+  return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
+}
+
+void smpi_comm_use(MPI_Comm comm){
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+  comm->refcount++;
 }
+
+void smpi_comm_unuse(MPI_Comm comm){
+  if (comm == MPI_COMM_UNINITIALIZED)
+    comm = smpi_process_comm_world();
+  comm->refcount--;
+  if(comm->refcount==0)
+    xbt_free(comm);
+}
+