From c4c9d7513fc105ca18a455a6f8f35d65b5bff219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffan=20S=C3=B8lvsten?= Date: Fri, 11 Aug 2023 18:09:08 +0200 Subject: [PATCH] Remove sanity checking of TPIE's computations This sanity check probably has unintended consequences. Let us just trust TPIE instead and know that we never are going to call these functions with so large values that we will see the undefined behaviour of Clang. --- .../internal/data_structures/priority_queue.h | 4 +--- src/adiar/internal/data_structures/sorter.h | 4 +--- src/adiar/internal/memory.h | 22 ------------------- 3 files changed, 2 insertions(+), 28 deletions(-) diff --git a/src/adiar/internal/data_structures/priority_queue.h b/src/adiar/internal/data_structures/priority_queue.h index 9bc6abf24..8abc1f1b1 100644 --- a/src/adiar/internal/data_structures/priority_queue.h +++ b/src/adiar/internal/data_structures/priority_queue.h @@ -28,16 +28,14 @@ namespace adiar::internal public: static tpie::memory_size_type memory_usage(tpie::memory_size_type no_elements) { - adiar_assert(no_elements < pq_t::memory_fits(tpie_max_bytes)); return pq_t::memory_usage(no_elements); } static tpie::memory_size_type memory_fits(tpie::memory_size_type memory_bytes) { - adiar_assert(memory_bytes < tpie_max_bytes); const tpie::memory_size_type ret = pq_t::memory_fits(memory_bytes); - adiar_assert(pq_t::memory_usage(ret) <= memory_bytes, + adiar_assert(memory_usage(ret) <= memory_bytes, "memory_fits and memory_usage should agree."); return ret; } diff --git a/src/adiar/internal/data_structures/sorter.h b/src/adiar/internal/data_structures/sorter.h index 472d266af..4ec4d3641 100644 --- a/src/adiar/internal/data_structures/sorter.h +++ b/src/adiar/internal/data_structures/sorter.h @@ -35,17 +35,15 @@ namespace adiar::internal static tpie::memory_size_type memory_usage(tpie::memory_size_type no_elements) { - adiar_assert(no_elements < array_t::memory_fits(tpie_max_bytes)); return array_t::memory_usage(no_elements); } static tpie::memory_size_type memory_fits(tpie::memory_size_type memory_bytes) { - adiar_assert(memory_bytes < tpie_max_bytes); const tpie::memory_size_type ret = array_t::memory_fits(memory_bytes); - adiar_assert(array_t::memory_usage(ret) <= memory_bytes, + adiar_assert(memory_usage(ret) <= memory_bytes, "memory_fits and memory_usage should agree."); return ret; } diff --git a/src/adiar/internal/memory.h b/src/adiar/internal/memory.h index 2fdda9e6e..65c16b2b1 100644 --- a/src/adiar/internal/memory.h +++ b/src/adiar/internal/memory.h @@ -17,28 +17,6 @@ namespace adiar::internal { return tpie::get_memory_manager().available(); } - - ////////////////////////////////////////////////////////////////////////////// - /// \brief The largest value that TPIE can use without some computation for - /// internal memory breaking. - /// - /// TPIE's data structures provide the `memory_usage` and `memory_fits` - /// functions. These cast the `tpie::memory_size_type` (an alias for - /// `std::size_t`) into a `double`, do some computations and then cast them - /// back into a `tpie::memory_size_type`. - /// - /// Yet, on Clang with specific optimisations enabled some parts of this - /// computation is cast to an `unsigned long` (32 bits) which leads to - /// undefined behaviour. Due to this, `memory_usage(memory_fits(x))` is - /// unexpectedly larger than `x`. - /// - /// This value is an empirically derived value for a `tpie::array` and is - /// equivalent to 4 PiB for which these computations by TPIE are safe. - /// - /// \see priority_queue sorter - ////////////////////////////////////////////////////////////////////////////// - constexpr tpie::memory_size_type tpie_max_bytes = - std::numeric_limits::max() >> 12; } namespace adiar