Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Copy the group when doing a comm_split, to avoid to have shared objects with referenc...
authorAugustin Degomme <degomme@idpann.imag.fr>
Tue, 9 Jul 2013 12:59:39 +0000 (14:59 +0200)
committerAugustin Degomme <degomme@idpann.imag.fr>
Thu, 11 Jul 2013 19:26:34 +0000 (21:26 +0200)
src/smpi/private.h
src/smpi/smpi_comm.c
src/smpi/smpi_group.c
src/smpi/smpi_pmpi.c

index 52b9ed8..651841c 100644 (file)
@@ -157,6 +157,7 @@ void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
                    MPI_Datatype * datatype);
 
 MPI_Group smpi_group_new(int size);
+MPI_Group smpi_group_copy(MPI_Group origin);
 void smpi_group_destroy(MPI_Group group);
 void smpi_group_set_mapping(MPI_Group group, int index, int rank);
 int smpi_group_index(MPI_Group group, int rank);
index 8e2b956..2f6bf15 100644 (file)
@@ -131,6 +131,8 @@ 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
+        smpi_group_use(group_out);
         index = smpi_group_index(group, rankmap[2 * j]);
         smpi_group_set_mapping(group_out, index, j);
       }
@@ -148,23 +150,25 @@ MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
     }
     xbt_free(recvbuf);
     group_out = group_root; /* exit with root's group */
+    if(group_out)smpi_group_unuse(group_out);
   } 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);
+        smpi_group_unuse(group_out);
+      }
     } /* 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);
 }
index 0222b88..a88bcca 100644 (file)
@@ -35,23 +35,43 @@ MPI_Group smpi_group_new(int size)
   group->size = size;
   group->rank_to_index_map = xbt_new(int, size);
   group->index_to_rank_map = xbt_new(int, count);
-  group->refcount = 0;
+  group->refcount = 1;
   for (i = 0; i < size; i++) {
     group->rank_to_index_map[i] = MPI_UNDEFINED;
   }
   for (i = 0; i < count; i++) {
     group->index_to_rank_map[i] = MPI_UNDEFINED;
   }
+
   return group;
 }
 
-void smpi_group_destroy(MPI_Group group)
+MPI_Group smpi_group_copy(MPI_Group origin)
 {
-  if (smpi_group_unuse(group) <= 0) {
-    xbt_free(group->rank_to_index_map);
-    xbt_free(group->index_to_rank_map);
-    xbt_free(group);
+  MPI_Group group;
+  int i, count;
+
+  count = smpi_process_count();
+  group = xbt_new(s_smpi_mpi_group_t, 1);
+  group->size = origin->size;
+  group->rank_to_index_map = xbt_new(int, group->size);
+  group->index_to_rank_map = xbt_new(int, count);
+  group->refcount = 1;
+  for (i = 0; i < group->size; i++) {
+    group->rank_to_index_map[i] = origin->rank_to_index_map[i];
+  }
+  for (i = 0; i < count; i++) {
+    group->index_to_rank_map[i] = origin->index_to_rank_map[i];
   }
+
+  return group;
+}
+
+
+void smpi_group_destroy(MPI_Group group)
+{
+  XBT_VERB("trying to free group %p, refcount = %d", group, group->refcount);
+  smpi_group_unuse(group);
 }
 
 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
@@ -90,7 +110,12 @@ int smpi_group_use(MPI_Group group)
 
 int smpi_group_unuse(MPI_Group group)
 {
-  group->refcount--;
+  if (group->refcount-- <= 0) {
+    XBT_VERB("freeing group %p", group);
+    xbt_free(group->rank_to_index_map);
+    xbt_free(group->index_to_rank_map);
+    xbt_free(group);
+  }
   return group->refcount;
 }
 
index df6074b..26e809b 100644 (file)
@@ -606,26 +606,25 @@ int PMPI_Group_range_incl(MPI_Group group, int n, int ranges[][3],
         }
       }
 
-        *newgroup = smpi_group_new(size);
-        j = 0;
-        for (i = 0; i < n; i++) {
-          for (rank = ranges[i][0];     /* First */
-               rank >= 0; /* Last */
-               ) {
-            index = smpi_group_index(group, rank);
-            smpi_group_set_mapping(*newgroup, index, j);
-            j++;
-            rank += ranges[i][2]; /* Stride */
-           if (ranges[i][0]<ranges[i][1]){
-             if(rank > ranges[i][1])
-               break;
-           }else{
-             if(rank < ranges[i][1])
-               break;
-           }
-          }
+      *newgroup = smpi_group_new(size);
+      j = 0;
+      for (i = 0; i < n; i++) {
+        for (rank = ranges[i][0];     /* First */
+             rank >= 0; /* Last */
+             ) {
+          index = smpi_group_index(group, rank);
+          smpi_group_set_mapping(*newgroup, index, j);
+          j++;
+          rank += ranges[i][2]; /* Stride */
+         if (ranges[i][0]<ranges[i][1]){
+           if(rank > ranges[i][1])
+             break;
+         }else{
+           if(rank < ranges[i][1])
+             break;
+         }
         }
-      //}
+      }
     }
     smpi_group_use(*newgroup);
     retval = MPI_SUCCESS;