Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[DOC] Fixed even more errors.
[simgrid.git] / src / smpi / smpi_group.c
index 0222b88..33f9902 100644 (file)
@@ -1,8 +1,8 @@
-/* Copyright (c) 2010. The SimGrid Team.
+/* Copyright (c) 2010, 2013-2015. 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 "private.h"
 
@@ -12,7 +12,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
 typedef struct s_smpi_mpi_group {
   int size;
   int *rank_to_index_map;
-  int *index_to_rank_map;
+  xbt_dict_t index_to_rank_map;
   int refcount;
 } s_smpi_mpi_group_t;
 
@@ -28,37 +28,78 @@ MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
 MPI_Group smpi_group_new(int size)
 {
   MPI_Group group;
-  int i, count;
+  int i;
 
-  count = smpi_process_count();
   group = xbt_new(s_smpi_mpi_group_t, 1);
   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->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
+  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;
 }
 
+MPI_Group smpi_group_copy(MPI_Group origin)
+{
+  MPI_Group group=origin;
+  char *key;
+  char *ptr_rank;
+  xbt_dict_cursor_t cursor = NULL;
+  
+  int i;
+  if(origin!= smpi_comm_group(MPI_COMM_WORLD)
+            && origin != MPI_GROUP_NULL
+            && origin != smpi_comm_group(MPI_COMM_SELF)
+            && origin != MPI_GROUP_EMPTY)
+    {
+      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_dict_new_homogeneous(xbt_free_f);
+      group->refcount = 1;
+      for (i = 0; i < group->size; i++) {
+        group->rank_to_index_map[i] = origin->rank_to_index_map[i];
+      }
+
+      xbt_dict_foreach(origin->index_to_rank_map, cursor, key, ptr_rank) {
+        xbt_dict_set(group->index_to_rank_map, key, ptr_rank, NULL);
+      }
+    }
+
+  return group;
+}
+
+
 void smpi_group_destroy(MPI_Group group)
 {
-  if (smpi_group_unuse(group) <= 0) {
-    xbt_free(group->rank_to_index_map);
-    xbt_free(group->index_to_rank_map);
-    xbt_free(group);
-  }
+  if(group!= smpi_comm_group(MPI_COMM_WORLD)
+          && group != MPI_GROUP_NULL
+          && group != smpi_comm_group(MPI_COMM_SELF)
+          && group != MPI_GROUP_EMPTY)
+  smpi_group_unuse(group);
 }
 
 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
 {
-  if (rank < group->size && index < smpi_process_count()) {
+  char * key;
+  int * val_rank;
+
+  if (rank < group->size) {
     group->rank_to_index_map[rank] = index;
-    group->index_to_rank_map[index] = rank;
+    if (index!=MPI_UNDEFINED ) {
+      val_rank = (int *) malloc(sizeof(int));
+      *val_rank = rank; 
+      int size = asprintf(&key, "%d", index);
+      if (size!=-1){
+        xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL);
+        free(key);
+      } else {
+        xbt_die("could not allocate memory for asprintf");
+      }
+    }
   }
 }
 
@@ -74,12 +115,17 @@ int smpi_group_index(MPI_Group group, int rank)
 
 int smpi_group_rank(MPI_Group group, int index)
 {
-  int rank = MPI_UNDEFINED;
-
-  if (index < smpi_process_count()) {
-    rank = group->index_to_rank_map[index];
-  }
-  return rank;
+  int * ptr_rank = NULL;
+  char * key;
+  int size = asprintf(&key, "%d", index);
+  if (size!=-1){
+    ptr_rank = xbt_dict_get_or_null(group->index_to_rank_map, key);
+    xbt_free(key);
+  }else
+    xbt_die("could not allocate memory for asprintf");
+  if (!ptr_rank)
+    return MPI_UNDEFINED;
+  return *ptr_rank;
 }
 
 int smpi_group_use(MPI_Group group)
@@ -91,7 +137,14 @@ int smpi_group_use(MPI_Group group)
 int smpi_group_unuse(MPI_Group group)
 {
   group->refcount--;
+  if (group->refcount <= 0) {
+    xbt_free(group->rank_to_index_map);
+    xbt_dict_free(&group->index_to_rank_map);
+    xbt_free(group);
+    return 0;
+  }
   return group->refcount;
+
 }
 
 int smpi_group_size(MPI_Group group)
@@ -123,3 +176,25 @@ int smpi_group_compare(MPI_Group group1, MPI_Group group2)
   }
   return result;
 }
+
+int smpi_group_incl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup)
+{
+  int i=0, index=0;
+  if (n == 0) {
+    *newgroup = MPI_GROUP_EMPTY;
+  } else if (n == smpi_group_size(group)) {
+    *newgroup = group;
+    if(group!= smpi_comm_group(MPI_COMM_WORLD)
+              && group != MPI_GROUP_NULL
+              && group != smpi_comm_group(MPI_COMM_SELF)
+              && group != MPI_GROUP_EMPTY)
+    smpi_group_use(group);
+  } else {
+    *newgroup = smpi_group_new(n);
+    for (i = 0; i < n; i++) {
+      index = smpi_group_index(group, ranks[i]);
+      smpi_group_set_mapping(*newgroup, index, i);
+    }
+  }
+  return MPI_SUCCESS;
+}