Skip to content

Commit

Permalink
Remove sanity checking of TPIE's computations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SSoelvsten committed Aug 11, 2023
1 parent 9db082e commit c4c9d75
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 28 deletions.
4 changes: 1 addition & 3 deletions src/adiar/internal/data_structures/priority_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions src/adiar/internal/data_structures/sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 0 additions & 22 deletions src/adiar/internal/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>` 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<tpie::memory_size_type>::max() >> 12;
}

namespace adiar
Expand Down

0 comments on commit c4c9d75

Please sign in to comment.