Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Move a map in smpi::Group from array to std::vector
authorChristian Heinrich <franz-christian.heinrich@inria.fr>
Tue, 24 Oct 2017 09:29:47 +0000 (11:29 +0200)
committerChristian Heinrich <franz-christian.heinrich@inria.fr>
Mon, 30 Oct 2017 12:26:15 +0000 (13:26 +0100)
See the source code comment why I'm not using std::map.

src/smpi/include/smpi_group.hpp
src/smpi/mpi/smpi_group.cpp

index 4637dea..ca6ad34 100644 (file)
@@ -16,7 +16,11 @@ namespace smpi{
 class Group : public F2C{
   private:
     int size_;
 class Group : public F2C{
   private:
     int size_;
-    int *rank_to_index_map_;
+    /* This is actually a map from int to int. We could use
+     * std::map here, but looking up a value there costs O(log(n)).
+     * For a vector, this costs O(1). We hence go with the vector.
+     */
+    std::vector<int> rank_to_index_map_;
     std::unordered_map<int, int> index_to_rank_map_;
     int refcount_;
   public:
     std::unordered_map<int, int> index_to_rank_map_;
     int refcount_;
   public:
index 6f837af..23c4809 100644 (file)
@@ -19,13 +19,11 @@ namespace smpi{
 Group::Group()
 {
   size_              = 0;       /* size */
 Group::Group()
 {
   size_              = 0;       /* size */
-  rank_to_index_map_ = nullptr; /* rank_to_index_map_ */
   refcount_          = 1;       /* refcount_: start > 0 so that this group never gets freed */
 }
 
   refcount_          = 1;       /* refcount_: start > 0 so that this group never gets freed */
 }
 
-Group::Group(int n) : size_(n)
+Group::Group(int n) : size_(n), rank_to_index_map_(size_)
 {
 {
-  rank_to_index_map_ = new int[size_];
   refcount_ = 1;
   for (int i              = 0; i < size_; i++)
     rank_to_index_map_[i] = MPI_UNDEFINED;
   refcount_ = 1;
   for (int i              = 0; i < size_; i++)
     rank_to_index_map_[i] = MPI_UNDEFINED;
@@ -35,11 +33,8 @@ Group::Group(MPI_Group origin)
 {
   if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
     size_              = origin->size();
 {
   if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
     size_              = origin->size();
-    rank_to_index_map_ = new int[size_];
     refcount_          = 1;
     refcount_          = 1;
-    for (int i = 0; i < size_; i++) {
-      rank_to_index_map_[i] = origin->rank_to_index_map_[i];
-    }
+    rank_to_index_map_ = origin->rank_to_index_map_;
 
     for (auto const& elm : origin->index_to_rank_map_) {
       index_to_rank_map_.insert({elm.first, elm.second});
 
     for (auto const& elm : origin->index_to_rank_map_) {
       index_to_rank_map_.insert({elm.first, elm.second});
@@ -49,7 +44,7 @@ Group::Group(MPI_Group origin)
 
 Group::~Group()
 {
 
 Group::~Group()
 {
-  delete[] rank_to_index_map_;
+  rank_to_index_map_.clear();
 }
 
 void Group::set_mapping(int index, int rank)
 }
 
 void Group::set_mapping(int index, int rank)