From 8adc09758d857a4964013fa30184c9eec770f2bf Mon Sep 17 00:00:00 2001 From: Christian Heinrich Date: Tue, 24 Oct 2017 11:29:47 +0200 Subject: [PATCH 1/1] [SMPI] Move a map in smpi::Group from array to std::vector See the source code comment why I'm not using std::map. --- src/smpi/include/smpi_group.hpp | 6 +++++- src/smpi/mpi/smpi_group.cpp | 11 +++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/smpi/include/smpi_group.hpp b/src/smpi/include/smpi_group.hpp index 4637dea46f..ca6ad345d8 100644 --- a/src/smpi/include/smpi_group.hpp +++ b/src/smpi/include/smpi_group.hpp @@ -16,7 +16,11 @@ namespace smpi{ 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 rank_to_index_map_; std::unordered_map index_to_rank_map_; int refcount_; public: diff --git a/src/smpi/mpi/smpi_group.cpp b/src/smpi/mpi/smpi_group.cpp index 6f837af73e..23c4809e80 100644 --- a/src/smpi/mpi/smpi_group.cpp +++ b/src/smpi/mpi/smpi_group.cpp @@ -19,13 +19,11 @@ namespace smpi{ 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 */ } -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; @@ -35,11 +33,8 @@ Group::Group(MPI_Group origin) { if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) { size_ = origin->size(); - rank_to_index_map_ = new int[size_]; 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}); @@ -49,7 +44,7 @@ Group::Group(MPI_Group origin) Group::~Group() { - delete[] rank_to_index_map_; + rank_to_index_map_.clear(); } void Group::set_mapping(int index, int rank) -- 2.20.1