Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_group.cpp
index 7507c65..f12c5cf 100644 (file)
@@ -1,12 +1,13 @@
-/* Copyright (c) 2010, 2013-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2010-2017. 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. */
 
-#include "private.h"
+#include "src/smpi/smpi_comm.hpp"
+#include "src/smpi/smpi_group.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
+
 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
 
@@ -23,22 +24,16 @@ Group::Group()
 
 Group::Group(int n) : size_(n)
 {
-  int i;
   rank_to_index_map_ = xbt_new(int, size_);
   index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
   refcount_ = 1;
-  for (i = 0; i < size_; i++) {
+  for (int i = 0; i < size_; i++) {
     rank_to_index_map_[i] = MPI_UNDEFINED;
   }
 }
 
 Group::Group(MPI_Group origin)
 {
-  char *key;
-  char *ptr_rank;
-  xbt_dict_cursor_t cursor = nullptr;
-  
-  int i;
   if(origin != MPI_GROUP_NULL
             && origin != MPI_GROUP_EMPTY)
     {
@@ -46,10 +41,13 @@ Group::Group(MPI_Group origin)
       rank_to_index_map_ = xbt_new(int, size_);
       index_to_rank_map_ = xbt_dict_new_homogeneous(xbt_free_f);
       refcount_ = 1;
-      for (i = 0; i < size_; i++) {
+      for (int i = 0; i < size_; i++) {
         rank_to_index_map_[i] = origin->rank_to_index_map_[i];
       }
 
+      char* key;
+      char* ptr_rank;
+      xbt_dict_cursor_t cursor = nullptr;
       xbt_dict_foreach(origin->index_to_rank_map_, cursor, key, ptr_rank) {
         int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
         *cp=*reinterpret_cast<int*>(ptr_rank);
@@ -64,13 +62,6 @@ Group::~Group()
   xbt_dict_free(&index_to_rank_map_);
 }
 
-void Group::destroy()
-{
-  if(this != MPI_COMM_WORLD->group()
-          && this != MPI_GROUP_EMPTY)
-  this->unuse();
-}
-
 void Group::set_mapping(int index, int rank)
 {
   int * val_rank;
@@ -112,20 +103,17 @@ int Group::rank(int index)
   return *ptr_rank;
 }
 
-int Group::use()
+void Group::ref()
 {
   refcount_++;
-  return refcount_;
 }
 
-int Group::unuse()
+void Group::unref(Group* group)
 {
-  refcount_--;
-  if (refcount_ <= 0) {
-    delete this;
-    return 0;
+  group->refcount_--;
+  if (group->refcount_ <= 0) {
+    delete group;
   }
-  return refcount_;
 }
 
 int Group::size()
@@ -139,13 +127,12 @@ int Group::compare(MPI_Group group2)
   int i;
   int index;
   int rank;
-  int sz;
 
   result = MPI_IDENT;
   if (size_ != group2->size()) {
     result = MPI_UNEQUAL;
   } else {
-    sz = group2->size();
+    int sz = group2->size();
     for (i = 0; i < sz; i++) {
       index = this->index(i);
       rank = group2->rank(index);
@@ -172,7 +159,7 @@ int Group::incl(int n, int* ranks, MPI_Group* newgroup)
     if(this!= MPI_COMM_WORLD->group()
               && this != MPI_COMM_SELF->group()
               && this != MPI_GROUP_EMPTY)
-    this->use();
+    this->ref();
   } else {
     *newgroup = new Group(n);
     for (i = 0; i < n; i++) {
@@ -197,7 +184,7 @@ int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
   if (size1 == 0) {
     *newgroup = MPI_GROUP_EMPTY;
   } else {
-    *newgroup = new simgrid::smpi::Group(size1);
+    *newgroup = new  Group(size1);
     size2 = this->size();
     for (int i = 0; i < size2; i++) {
       int proc1 = this->index(i);
@@ -224,7 +211,7 @@ int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
   if (size2 == 0) {
     *newgroup = MPI_GROUP_EMPTY;
   } else {
-    *newgroup = new simgrid::smpi::Group(size2);
+    *newgroup = new  Group(size2);
     int j=0;
     for (int i = 0; i < group2->size(); i++) {
       int proc2 = group2->index(i);
@@ -252,7 +239,7 @@ int Group::difference(MPI_Group group2, MPI_Group* newgroup)
   if (newsize == 0) {
     *newgroup = MPI_GROUP_EMPTY;
   } else {
-    *newgroup = new simgrid::smpi::Group(newsize);
+    *newgroup = new  Group(newsize);
     for (int i = 0; i < size2; i++) {
       int proc1 = this->index(i);
       int proc2 = group2->rank(proc1);
@@ -267,7 +254,7 @@ int Group::difference(MPI_Group group2, MPI_Group* newgroup)
 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
   int oldsize = size_;
   int newsize = oldsize - n;
-  *newgroup = new simgrid::smpi::Group(newsize);
+  *newgroup = new  Group(newsize);
   int* to_exclude=xbt_new0(int, size_);
   for (int i     = 0; i < oldsize; i++)
     to_exclude[i]=0;
@@ -306,7 +293,7 @@ int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
       }
     }
   }
-  *newgroup = new simgrid::smpi::Group(newsize);
+  *newgroup = new  Group(newsize);
   int j     = 0;
   for (int i = 0; i < n; i++) {
     for (int rank = ranges[i][0];                    /* First */
@@ -354,7 +341,7 @@ int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
   if (newsize == 0) {
     *newgroup = MPI_GROUP_EMPTY;
   } else {
-    *newgroup = new simgrid::smpi::Group(newsize);
+    *newgroup = new  Group(newsize);
     int newrank = 0;
     int oldrank = 0;
     while (newrank < newsize) {
@@ -389,5 +376,16 @@ int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
   return MPI_SUCCESS;
 }
 
+MPI_Group Group::f2c(int id) {
+  if(id == -2) {
+    return MPI_GROUP_EMPTY;
+  } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
+    char key[KEY_SIZE];
+    return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
+  } else {
+    return static_cast<MPI_Group>(MPI_GROUP_NULL);
+  }
+}
+
 }
 }