Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
make sure refcount of a comm is really 1 when initialized
[simgrid.git] / src / smpi / smpi_comm.c
index 70267cd79e369362031e529396ea770242efc626..8e2b9564c77ae080ec1066296dc0836651e08f73 100644 (file)
@@ -14,6 +14,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
 
 typedef struct s_smpi_mpi_communicator {
   MPI_Group group;
+  int refcount;
 } s_smpi_mpi_communicator_t;
 
 static int smpi_compare_rankmap(const void *a, const void *b)
@@ -43,13 +44,14 @@ MPI_Comm smpi_comm_new(MPI_Group group)
   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
   comm->group = group;
   smpi_group_use(comm->group);
+  comm->refcount=1;
   return comm;
 }
 
 void smpi_comm_destroy(MPI_Comm comm)
 {
-  smpi_group_destroy(comm->group);
-  xbt_free(comm);
+  smpi_group_unuse(comm->group);
+  smpi_comm_unuse(comm);
 }
 
 MPI_Group smpi_comm_group(MPI_Comm comm)
@@ -67,9 +69,19 @@ int smpi_comm_rank(MPI_Comm comm)
   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_WORLD) {
+    strcpy(name, "WORLD");
+    *len = 5;
+  } else {
+    *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
+  }
+}
+
 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
 {
-  int system_tag = 666;
+  int system_tag = 123;
   int index, rank, size, i, j, count, reqs;
   int* sendbuf;
   int* recvbuf;
@@ -141,5 +153,18 @@ MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
     } /* otherwise, exit with group_out == NULL */
   }
+  if(group_out)smpi_group_use(group_out);
   return group_out ? smpi_comm_new(group_out) : MPI_COMM_NULL;
 }
+
+void smpi_comm_use(MPI_Comm comm){
+  comm->refcount++;
+  smpi_group_use(comm->group);
+}
+
+void smpi_comm_unuse(MPI_Comm comm){
+  comm->refcount--;
+  smpi_group_unuse(comm->group);
+  if(comm->refcount==0)
+    xbt_free(comm);
+}