From c29eb8ebfbbb8f236e6093049b3fd555aaf3d296 Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Mon, 23 May 2022 12:07:59 +0200 Subject: [PATCH] Reduce cost to set up dictionary for compute_index_owner --- .../base/mpi_compute_index_owner_internal.h | 14 ++++---- .../base/mpi_compute_index_owner_internal.cc | 35 ++++++++++++++----- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/deal.II/base/mpi_compute_index_owner_internal.h b/include/deal.II/base/mpi_compute_index_owner_internal.h index 20efb739f4..88ea9c88bd 100644 --- a/include/deal.II/base/mpi_compute_index_owner_internal.h +++ b/include/deal.II/base/mpi_compute_index_owner_internal.h @@ -427,13 +427,13 @@ namespace Utilities * out the owner of the index that was requested (using the guess in * `owner_index`, as we typically might look up on the same rank * several times in a row, which avoids the binary search in - * Dictionary::get_owning_rank_index(). Once we know the rank of the - * owner, we the vector entry with the rank of the request. Here, we - * utilize the fact that requests are processed rank-by-rank, so we - * can simply look at the end of the vector if there is already some - * data stored or not. Finally, we build ranges, again using that - * the index list is sorted and we therefore only need to append at - * the end. + * Dictionary::get_owning_rank_index()). Once we know the rank of + * the owner, we fill the vector entry with the rank of the + * request. Here, we utilize the fact that requests are processed + * rank-by-rank, so we can simply look at the end of the vector + * whether there is already some data stored or not. Finally, we + * build ranges, again using that the index list is sorted and we + * therefore only need to append at the end. */ void append_index_origin(const types::global_dof_index index, diff --git a/source/base/mpi_compute_index_owner_internal.cc b/source/base/mpi_compute_index_owner_internal.cc index 92b82a3dd8..f115482350 100644 --- a/source/base/mpi_compute_index_owner_internal.cc +++ b/source/base/mpi_compute_index_owner_internal.cc @@ -47,14 +47,11 @@ namespace Utilities this->use_vector = use_vector; this->size = size; - data.clear(); + data = {}; data_map.clear(); - if (use_vector) - { - data = {}; - data.resize(size, invalid_index_value); - } + // do not initialize the vector but only upon first request in + // `fill` } @@ -70,8 +67,27 @@ namespace Utilities if (use_vector) { - AssertDimension(data.size(), size); - std::fill(data.begin() + start, data.begin() + end, value); + if (data.empty() && end > start) + { + // in debug mode, we want to track whether we set all + // indices, so we first fill an invalid index and only later + // the actual ones, whereas we simply assign the given rank + // to the complete vector the first time we pass around in + // this function in release mode to avoid touching data + // unnecessarily (and overwrite the smaller pieces), as the + // locally owned part comes first +#ifdef DEBUG + data.resize(size, invalid_index_value); + std::fill(data.begin() + start, data.begin() + end, value); +#else + data.resize(size, value); +#endif + } + else + { + AssertDimension(data.size(), size); + std::fill(data.begin() + start, data.begin() + end, value); + } } else { @@ -131,6 +147,9 @@ namespace Utilities if (use_vector) { + if (data.empty()) + return false; + AssertDimension(data.size(), size); return data[index] != invalid_index_value; } -- 2.39.5