From 9991fa404afccbad6bc1dfb68366c78d76195045 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Wed, 1 Nov 2017 15:13:39 +0100 Subject: [PATCH 1/1] Use a vector for index_to_rank_map_ too. --- src/smpi/include/smpi_group.hpp | 3 ++- src/smpi/mpi/smpi_group.cpp | 37 ++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/smpi/include/smpi_group.hpp b/src/smpi/include/smpi_group.hpp index ca6ad345d8..a76354814c 100644 --- a/src/smpi/include/smpi_group.hpp +++ b/src/smpi/include/smpi_group.hpp @@ -9,6 +9,7 @@ #include "smpi_f2c.hpp" #include +#include namespace simgrid{ namespace smpi{ @@ -21,7 +22,7 @@ class Group : public F2C{ * 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_; + std::vector index_to_rank_map_; int refcount_; public: explicit Group(); diff --git a/src/smpi/mpi/smpi_group.cpp b/src/smpi/mpi/smpi_group.cpp index 23c4809e80..bd774248ec 100644 --- a/src/smpi/mpi/smpi_group.cpp +++ b/src/smpi/mpi/smpi_group.cpp @@ -22,11 +22,9 @@ Group::Group() refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */ } -Group::Group(int n) : size_(n), rank_to_index_map_(size_) +Group::Group(int n) : size_(n), rank_to_index_map_(size_, MPI_UNDEFINED) { refcount_ = 1; - for (int i = 0; i < size_; i++) - rank_to_index_map_[i] = MPI_UNDEFINED; } Group::Group(MPI_Group origin) @@ -35,43 +33,44 @@ Group::Group(MPI_Group origin) size_ = origin->size(); refcount_ = 1; 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}); - } + index_to_rank_map_ = origin->index_to_rank_map_; } } Group::~Group() { - rank_to_index_map_.clear(); } void Group::set_mapping(int index, int rank) { - if (rank < size_) { + if (0 <= rank && rank < size_) { rank_to_index_map_[rank] = index; - if (index != MPI_UNDEFINED) - index_to_rank_map_.insert({index, rank}); + if (index != MPI_UNDEFINED) { + if ((unsigned)index >= index_to_rank_map_.size()) + index_to_rank_map_.resize(index + 1, MPI_UNDEFINED); + index_to_rank_map_[index] = rank; + } } } int Group::index(int rank) { - int index = MPI_UNDEFINED; - - if (0 <= rank && rank < size_) { + int index; + if (0 <= rank && rank < size_) index = rank_to_index_map_[rank]; - } + else + index = MPI_UNDEFINED; return index; } int Group::rank(int index) { - if (this == MPI_GROUP_EMPTY) - return MPI_UNDEFINED; - auto rank = index_to_rank_map_.find(index); - return rank == index_to_rank_map_.end() ? MPI_UNDEFINED : rank->second; + int rank; + if (0 <= index && (unsigned)index < index_to_rank_map_.size()) + rank = index_to_rank_map_[index]; + else + rank = MPI_UNDEFINED; + return rank; } void Group::ref() -- 2.20.1